Spring Batch Component
The spring-batch: component and support classes provide integration bridge between Camel and Spring Batch infrastructure.
Maven users will need to add the following dependency to their pom.xml
for this component:
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-spring-batch</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency>
URI format
spring-batch:jobName[?options]
Where jobName represents the name of the Spring Batch job located in the Camel registry.
This component can only be used to define producer endpoints, which means that you cannot use the Spring Batch component in a from()
statement.
Options
Name |
Default Value |
Description |
---|---|---|
|
|
Camel 2.10: Explicitly specifies the name of the |
Usage
When Spring Batch component receives the message, it triggers the job execution. The job will be executed using the org.springframework.batch.core.launch.JobLaucher
instance resolved according to the following algorithm:
- if
JobLauncher
is manually set on the component, then use it. - if
jobLauncherRef
option is set on the component, then search Camel Registry for theJobLauncher
with the given name. - if there is
JobLauncher
registered in the Camel Registry under jobLauncher name, then use it. - if none of the steps above allow to resolve the
JobLauncher
and there is exactly oneJobLauncher
instance in the Camel Registry, then use it.
All headers found in the message are passed to the JobLauncher
as job parameters. String
, Long
, Double
and java.util.Date
values are copied to the org.springframework.batch.core.JobParametersBuilder
- other data types are converted to Strings.
Examples
Triggering Spring Batch job execution:
from("direct:startBatch").to("spring-batch:myJob");
Triggering Spring Batch job execution with the JabLauncher
set explicitly.
from("direct:startBatch").to("spring-batch:myJob?jobLauncherRef=myJobLauncher");
Support classes
Apart from the Component, Camel Spring Batch provides also support classes to hook into Spring Batch infrastructure.
CamelItemReader
CamelItemReader
can be used to read batch data directly from the Camel infrastructure.
For example the snippet below configures Spring Batch to read data from JMS queue.
<bean id="camelReader" class="org.apache.camel.component.spring.batch.support.CamelItemReader"> <constructor-arg ref="consumerTemplate"/> <constructor-arg value="jms:dataQueue"/> </bean> <batch:job id="myJob"> <batch:step id="step"> <batch:tasklet> <batch:chunk reader="camelReader" writer="someWriter" commit-interval="100"/> </batch:tasklet> </batch:step> </batch:job>
CamelItemWriter
CamelItemWriter
has similar purpose as CamelItemReader
, but it is dedicated to write chunk of processed
For example the snippet below configures Spring Batch to read data from JMS queue.
<bean id="camelReader" class="org.apache.camel.component.spring.batch.support.CamelItemReader"> <constructor-arg ref="consumerTemplate"/> <constructor-arg value="jms:dataQueue"/> </bean> <batch:job id="myJob"> <batch:step id="step"> <batch:tasklet> <batch:chunk reader="camelReader" writer="someWriter" commit-interval="100"/> </batch:tasklet> </batch:step> </batch:job>