Chapter 318. Spring Batch Component
Available as of Camel version 2.10
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>
318.1. URI format
spring-batch:jobName[?options]
Where jobName represents the name of the Spring Batch job located in the Camel registry. Alternatively if a JobRegistry is provided it will be used to locate the job instead.
WARNING:This component can only be used to define producer endpoints, which means that you cannot use the Spring Batch component in a from()
statement.
318.2. Options
The Spring Batch component supports 3 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
jobLauncher (producer) | Explicitly specifies a JobLauncher to be used. | JobLauncher | |
jobRegistry (producer) | Explicitly specifies a JobRegistry to be used. | JobRegistry | |
resolveProperty Placeholders (advanced) | Whether the component should resolve property placeholders on itself when starting. Only properties which are of String type can use property placeholders. | true | boolean |
The Spring Batch endpoint is configured using URI syntax:
spring-batch:jobName
with the following path and query parameters:
318.2.1. Path Parameters (1 parameters):
Name | Description | Default | Type |
---|---|---|---|
jobName | Required The name of the Spring Batch job located in the registry. | String |
318.2.2. Query Parameters (4 parameters):
Name | Description | Default | Type |
---|---|---|---|
jobFromHeader (producer) | Explicitly defines if the jobName should be taken from the headers instead of the URI. | false | boolean |
jobLauncher (producer) | Explicitly specifies a JobLauncher to be used. | JobLauncher | |
jobRegistry (producer) | Explicitly specifies a JobRegistry to be used. | JobRegistry | |
synchronous (advanced) | Sets whether synchronous processing should be strictly used, or Camel is allowed to use asynchronous processing (if supported). | false | boolean |
318.3. Spring Boot Auto-Configuration
The component supports 4 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
camel.component.spring-batch.enabled | Enable spring-batch component | true | Boolean |
camel.component.spring-batch.job-launcher | Explicitly specifies a JobLauncher to be used. The option is a org.springframework.batch.core.launch.JobLauncher type. | String | |
camel.component.spring-batch.job-registry | Explicitly specifies a JobRegistry to be used. The option is a org.springframework.batch.core.configuration.JobRegistry type. | String | |
camel.component.spring-batch.resolve-property-placeholders | Whether the component should resolve property placeholders on itself when starting. Only properties which are of String type can use property placeholders. | true | Boolean |
318.4. 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. Deprecated and will be removed in Camel 3.0! -
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.
318.5. Examples
Triggering the Spring Batch job execution:
from("direct:startBatch").to("spring-batch:myJob");
Triggering the Spring Batch job execution with the JobLauncher
set explicitly.
from("direct:startBatch").to("spring-batch:myJob?jobLauncherRef=myJobLauncher");
Starting from the Camel 2.11.1 JobExecution
instance returned by the JobLauncher
is forwarded by the SpringBatchProducer
as the output message. You can use the JobExecution
instance to perform some operations using the Spring Batch API directly.
from("direct:startBatch").to("spring-batch:myJob").to("mock:JobExecutions"); ... MockEndpoint mockEndpoint = ...; JobExecution jobExecution = mockEndpoint.getExchanges().get(0).getIn().getBody(JobExecution.class); BatchStatus currentJobStatus = jobExecution.getStatus();
318.6. Support classes
Apart from the Component, Camel Spring Batch provides also support classes, which can be used to hook into Spring Batch infrastructure.
318.6.1. 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>
318.6.2. CamelItemWriter
CamelItemWriter
has similar purpose as CamelItemReader
, but it is dedicated to write chunk of the processed data.
For example the snippet below configures Spring Batch to read data from JMS queue.
<bean id="camelwriter" class="org.apache.camel.component.spring.batch.support.CamelItemWriter"> <constructor-arg ref="producerTemplate"/> <constructor-arg value="jms:dataQueue"/> </bean> <batch:job id="myJob"> <batch:step id="step"> <batch:tasklet> <batch:chunk reader="someReader" writer="camelwriter" commit-interval="100"/> </batch:tasklet> </batch:step> </batch:job>
318.6.3. CamelItemProcessor
CamelItemProcessor
is the implementation of Spring Batch org.springframework.batch.item.ItemProcessor
interface. The latter implementation relays on Request Reply pattern to delegate the processing of the batch item to the Camel infrastructure. The item to process is sent to the Camel endpoint as the body of the message.
For example the snippet below performs simple processing of the batch item using the Direct endpoint and the Simple expression language.
<camel:camelContext> <camel:route> <camel:from uri="direct:processor"/> <camel:setExchangePattern pattern="InOut"/> <camel:setBody> <camel:simple>Processed ${body}</camel:simple> </camel:setBody> </camel:route> </camel:camelContext> <bean id="camelProcessor" class="org.apache.camel.component.spring.batch.support.CamelItemProcessor"> <constructor-arg ref="producerTemplate"/> <constructor-arg value="direct:processor"/> </bean> <batch:job id="myJob"> <batch:step id="step"> <batch:tasklet> <batch:chunk reader="someReader" writer="someWriter" processor="camelProcessor" commit-interval="100"/> </batch:tasklet> </batch:step> </batch:job>
318.6.4. CamelJobExecutionListener
CamelJobExecutionListener
is the implementation of the org.springframework.batch.core.JobExecutionListener
interface sending job execution events to the Camel endpoint.
The org.springframework.batch.core.JobExecution
instance produced by the Spring Batch is sent as a body of the message. To distinguish between before- and after-callbacks SPRING_BATCH_JOB_EVENT_TYPE
header is set to the BEFORE
or AFTER
value.
The example snippet below sends Spring Batch job execution events to the JMS queue.
<bean id="camelJobExecutionListener" class="org.apache.camel.component.spring.batch.support.CamelJobExecutionListener"> <constructor-arg ref="producerTemplate"/> <constructor-arg value="jms:batchEventsBus"/> </bean> <batch:job id="myJob"> <batch:step id="step"> <batch:tasklet> <batch:chunk reader="someReader" writer="someWriter" commit-interval="100"/> </batch:tasklet> </batch:step> <batch:listeners> <batch:listener ref="camelJobExecutionListener"/> </batch:listeners> </batch:job>
318.7. Spring Cloud
Available as of Camel 2.19
Spring Cloud component
Maven users will need to add the following dependency to their pom.xml
in order to use this component:
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-spring-cloud</artifactId> <version>${camel.version}</version> <!-- use the same version as your Camel core version --> </dependency>
camel-spring-cloud
jar comes with the spring.factories
file, so as soon as you add that dependency into your classpath, Spring Boot will automatically auto-configure Camel for you.
318.7.1. Camel Spring Cloud Starter
Available as of Camel 2.19
To use the starter, add the following to your spring boot pom.xml file:
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-spring-cloud-starter</artifactId> <version>${camel.version}</version> <!-- use the same version as your Camel core version --> </dependency>
318.8. Spring Cloud Consul
Available as of Camel 2.22
318.9. Spring Cloud Zookeeper
Available as of Camel 2.22
318.10. Spring Cloud Netflix
Available as of Camel 2.19
The Spring Cloud Netflix component bridges Camel Cloud and Spring Cloud Netflix so you can leverage Spring Cloud Netflix service discovery and load balance features in Camel and/or you can use Camel Service Discovery implementations as ServerList source for Spring Cloud Netflix’s Ribbon load balabncer.
Maven users will need to add the following dependency to their pom.xml
in order to use this component:
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-spring-cloud-netflix</artifactId> <version>${camel.version}</version> <!-- use the same version as your Camel core version --> </dependency>
camel-spring-cloud-netflix
jar comes with the spring.factories
file, so as soon as you add that dependency into your classpath, Spring Boot will automatically auto-configure Camel for you.
You can disable Camel Spring Cloud Netflix with the following properties:
# Enable/Disable the whole integration, default true camel.cloud.netflix = true # Enable/Disable the integration with Ribbon, default true camel.cloud.netflix.ribbon = true
318.11. Spring Cloud Netflix Starter
Available as of Camel 2.19
To use the starter, add the following to your spring boot pom.xml file:
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-spring-cloud-netflix-starter</artifactId> <version>${camel.version}</version> <!-- use the same version as your Camel core version --> </dependency>