JBatch (aka JSR 352) is the new standard to write Java and JavaEE batches. It is mainly based on reader/processor/writer and batchlet (task) concepts. This is not that far from camel which is based on consumers and processors.
So how can both work together?
First thing we can want to do is to reuse camel components in a JBatch batch: camel has a lot of components and it can avoid you to write your own reader/writer.
What would it look like?
<job id="camel-reader" xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="1.0"> <step id="step1"> <chunk> <reader ref="camelReader"> <properties> <property name="endpoint" value="direct:reader"/> </properties> </reader> <processor ref="camelProcessor"> <properties> <property name="endpoint" value="direct:processor"/> </properties> </processor> <writer ref="camelWriter"> <properties> <property name="endpoint" value="direct:writer"/> </properties> </writer> </chunk> </step> </job>
Of course if you need it for each “jbatch component” maybe you don’t use the right tool or you need to write a JBatch version.
For processor it works not that bad but for readers and writers there is something fundamentally different:
- writers have no real batch semantic (depend on the component)
- readers doesn’t follow the JBatch idea (return null when nothing to read anymore) and are more oriented to infinite processes. That’s why you need to configure most of the time a timeout (supposed to a default of 1s in the previous example) to force the JBatch integration to return null and let the process finish gracefully
However this can be hurtless drawbacks when you avoid to write a very complicated component.
The other interesting thing to do between Camel and JBatch is to be able to start a batch from a camel request. A simple Camel component easily allows to do so this way:
from("....").to("jbatch:my-super-batch").to(...)...;
Note: JBatch API is not synchronous by default. While it is ok most of the time you can want to wait for the batch to be done before going to next Camel endpoint. To do so adding a property is enough – “synchronous” which would represent the time to wait before checking if the job is done):
from("....").to("jbatch:my-super-batch?synchronous=100").to(...)...;
The good new is this integration is done in BatchEE (a JBatch impl which will be pretty soon imported in Apache incubator projects) in batchee-camel module: https://github.com/rmannibucau/batchee/tree/master/extensions/camel.