Show Table of Contents
Chapter 151. SJMS Batch
SJMS Batch Component
Available as of Camel 2.16
SJMS Batch is a specialized component for highly performant, transactional batch consumption from a JMS queue. It can be thought of as a hybrid of a consumer-only component and an aggregator.
A common use case in Camel is to consume messages from a queue and aggregate them before sending the aggregated state to another endpoint. In order to ensure that data is not lost if the system performing the processing fails, it is typically consumed within a transaction from the queue, and once aggregated stored in a persistent
AggregationRepository, such as the one found in the JDBC Component.
The behavior of the aggregator pattern involves fetching data from the
AggregationRepositorybefore an incoming message is aggregated, and writing back the result afterwards. By nature, the reads and writes take progressively longer as the number of aggregated artifacts increases. A rough example of this using arbitrary time units that demonstrates the impact of this is as follows:
|Item||Read Time||Write Time||Total Time|
In contrast, consumption performance using the SJMS Batch component is linear. Each message is consumed and aggregated using an
AggregationStrategybefore the next one is fetched. As all of the consumption and aggregation is performed in a single JMS transaction no external storage is required to persist the intermediate state - this avoids the read and write costs described above. In practice, this yields multiple orders of magnitude higher throughput.
Once a completion condition is met, either by size or period since first message, the aggregated
Exchangeis passed into the route. During the processing of this
Exchange, if an exception is thrown or the system shuts down, all of the original consumed messages end up back on the queue (or are placed on the dead-letter queue depending on the broker configuration).
Unlike using a regular aggregator, there is no facility for an aggregation condition, meaning that it is not possible to batch consume into multiple groups of messages at the same time. All consumed messages are aggregated together into a single batch. To get around this, a common design approach is to first sort the different groups of messages to be aggregated onto different queues, and batch consume from each one separately.
Multiple JMS consumer support is available which allows you to consume in parallel using the one route, and at the same time use facilities like JMS message groups to group related messages.
Maven users will need to add the following dependency to their
pom.xmlfor this component:
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-sjms</artifactId> <version>2.17.0.redhat-630xxx</version> <!-- use the same version as your Camel core version --> </dependency>
SJMS Batch is a subcomponent of SJMS, and resides in the same library.
destinationNameis a JMS queue. By default, the
destinationNameis interpreted as a queue name.
You can include the optional
queue:prefix, if you prefer:
Topic consumption is not supported, as there is no advantage to using batch consumption within that context. Topic messages are usually non-persistent, and loss is acceptable. If consumed within a transaction that fails, a topic message will likely not be redelivered by the broker. A plain SJMS consumer endpoint can be used in conjunction with a regular non-persistence backed aggregator in this scenario.
Component Options and Configurations
The SJMS Batch Component supports the following configuration options:
A reference to an
The size of the batch to aggregate.
Care should be taken to ensure that this is not larger than the JMS consumer's prefetch buffer, or the maximum page size for a queue on the broker; either of these could cause the consumer to hang if no timeout is used.
A value of 0 or less indicates that
The maximum time to wait from the receipt of the first message before emitting the Exchange.
A value of 0 or less indicates that
Notice you cannot use both completion timeout and completion interval at the same time, only one can be configured.
||Camel 2.17: The completion interval in millis, which causes batches to be completed in a scheduled fixed rate every interval. The batch may be empty if the timeout triggered and there was no messages in the batch. Notice you cannot use both completion timeout and completion interval at the same time, only one can be configured.|
||Camel 2.17: If using completion timeout or interval, then the batch may be empty if the timeout triggered and there was no messages in the batch. If this option is true and the batch is empty then an empty message is added to the batch so an empty message is routed.|
The maximum length of a call to
This value is effectively the poll time between batches.
||Camel 2.17: If using the completionInterval option a background thread is created to trigger the completion interval. Set this option to provide a custom thread pool to be used rather than creating a new thread for every consumer.|
completionSizeendpoint attribute is used in conjunction with
completionTimeout, where the first condition to be met will cause the aggregated
Exchangeto be emitted down the route.