Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Maven users will need to add the following dependency to their pom.xml for this component:

Code Block
xml
xml

<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

Code Block

spring-batch:jobName[?options]

...

Name

Default Value

Description

jobLauncherRef

null

Deprecated and will be removed in Camel 3.0! Camel 2.10: Use jobLauncher=#theName instead.

jobLauncher

null

Camel 2.11.1: Explicitly specifies a Explicitly specifies the name of the JobLauncher to be used from the Camel Registry.

jobFromHeaderfalseCamel 2.18: Explicitly defines if the jobName shouls be taken from the headers instead of the URI. The header has name: CamelSpringBatchJobName

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 the JobLauncher 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 one JobLauncher 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.

...

Triggering the Spring Batch job execution:

Code Block
java
java

from("direct:startBatch").to("spring-batch:myJob");

Triggering the Spring Batch job execution with the JobLauncher set explicitly.

Code Block
java
java

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.

Code Block
java
java
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();

Support classes

Apart from the Component, Camel Spring Batch provides also support classes, which can be used to hook into Spring Batch infrastructure.

...

For example the snippet below configures Spring Batch to read data from JMS queue.

Code Block
xml
xml

<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>

...

For example the snippet below configures Spring Batch to read data from JMS queue.

Code Block
xml
xml

<bean id="camelReadercamelwriter" class="org.apache.camel.component.spring.batch.support.CamelItemReaderCamelItemWriter">
  <constructor-arg ref="consumerTemplateproducerTemplate"/>
  <constructor-arg value="jms:dataQueue"/>
</bean>

<batch:job id="myJob">
  <batch:step id="step">
    <batch:tasklet>
      <batch:chunk reader="camelReadersomeReader" writer="someWritercamelwriter" commit-interval="100"/>
    </batch:tasklet>
  </batch:step>
</batch:job>

...

For example the snippet below performs simple processing of the batch item using the Direct endpoint and the Simple expression language .

Code Block
xml
xml

<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>

...

The example snippet below sends Spring Batch job execution events to the JMS queue.

Code Block
xml
xml

<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>