camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > SpringBatch
Date Wed, 05 Jun 2013 13:18:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/SpringBatch">SpringBatch</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~hekonsek">Henryk
Konsek</a>
    </h4>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{code} <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">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. <br> <br>{code:java}
<br>from(&quot;direct:startBatch&quot;).to(&quot;spring-batch:myJob&quot;).to(&quot;mock:JobExecutions&quot;);
<br>... <br>MockEndpoint mockEndpoint = ...; <br>JobExecution jobExecution
= mockEndpoint.getExchanges().get(0).getIn().getBody(JobExecution.class); <br>BatchStatus
currentJobStatus = jobExecution.getStatus(); <br>{code} <br> <br></td></tr>
            <tr><td class="diff-unchanged" >h3. Support classes <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="SpringBatch-SpringBatchComponent"></a>Spring Batch
Component</h2>

<p>The <b>spring-batch:</b> component and support classes provide integration
bridge between Camel and <a href="http://www.springsource.org/spring-batch" class="external-link"
rel="nofollow">Spring Batch</a> infrastructure.</p>

<p>Maven users will need to add the following dependency to their <tt>pom.xml</tt>
for this component:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;dependency&gt;</span>
    <span class="code-tag">&lt;groupId&gt;</span>org.apache.camel<span
class="code-tag">&lt;/groupId&gt;</span>
    <span class="code-tag">&lt;artifactId&gt;</span>camel-spring-batch<span
class="code-tag">&lt;/artifactId&gt;</span>
    <span class="code-tag">&lt;version&gt;</span>x.x.x<span class="code-tag">&lt;/version&gt;</span>
    <span class="code-tag"><span class="code-comment">&lt;!-- use the same
version as your Camel core version --&gt;</span></span>
<span class="code-tag">&lt;/dependency&gt;</span>
</pre>
</div></div>

<h3><a name="SpringBatch-URIformat"></a>URI format</h3>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
spring-batch:jobName[?options]
</pre>
</div></div>
<p>Where <b>jobName</b> represents the name of the Spring Batch job located
in the Camel registry.</p>

<div class='panelMacro'><table class='warningMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/forbidden.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td>This component can
only be used to define producer endpoints, which means that you cannot use the Spring Batch
component in a <tt>from()</tt> statement.</td></tr></table></div>

<h3><a name="SpringBatch-Options"></a>Options</h3>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Default Value </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>jobLauncherRef</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Deprecated and will be removed in Camel 3.0!</b>
<b>Camel 2.10:</b> Use jobLauncher=#theName instead. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>jobLauncher</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 2.11.1:</b> Explicitly specifies a <tt>JobLauncher</tt>
to be used from the Camel <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a>.
</td>
</tr>
</tbody></table>
</div>


<h3><a name="SpringBatch-Usage"></a>Usage</h3>

<p>When Spring Batch component receives the message, it triggers the job execution.
The job will be executed using the <tt>org.springframework.batch.core.launch.JobLaucher</tt>
instance resolved according to the following algorithm:</p>
<ul>
	<li>if <tt>JobLauncher</tt> is manually set on the component, then use
it.</li>
	<li>if <tt>jobLauncherRef</tt> option is set on the component, then search
Camel <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a>
for the <tt>JobLauncher</tt> with the given name. <b>Deprecated and will
be removed in Camel 3.0!</b></li>
	<li>if there is <tt>JobLauncher</tt> registered in the Camel <a href="/confluence/display/CAMEL/Registry"
title="Registry">Registry</a> under <b>jobLauncher</b> name, then use
it.</li>
	<li>if none of the steps above allow to resolve the <tt>JobLauncher</tt>
and there is exactly one <tt>JobLauncher</tt> instance in the Camel <a href="/confluence/display/CAMEL/Registry"
title="Registry">Registry</a>, then use it.</li>
</ul>


<p>All headers found in the message are passed to the <tt>JobLauncher</tt>
as job parameters. <tt>String</tt>, <tt>Long</tt>, <tt>Double</tt>
and <tt>java.util.Date</tt> values are copied to the <tt>org.springframework.batch.core.JobParametersBuilder</tt>
- other data types are converted to Strings.</p>

<h3><a name="SpringBatch-Examples"></a>Examples</h3>

<p>Triggering the Spring Batch job execution:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"direct:startBatch"</span>).to(<span class="code-quote">"spring-batch:myJob"</span>);
</pre>
</div></div>

<p>Triggering the Spring Batch job execution with the <tt>JobLauncher</tt>
set explicitly. </p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"direct:startBatch"</span>).to(<span class="code-quote">"spring-batch:myJob?jobLauncherRef=myJobLauncher"</span>);
</pre>
</div></div>

<p>Starting from the Camel <b>2.11.1</b> <tt>JobExecution</tt>
instance returned by the <tt>JobLauncher</tt> is forwarded by the <tt>SpringBatchProducer</tt>
as the output message. You can use the <tt>JobExecution</tt> instance to perform
some operations using the Spring Batch API directly.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"direct:startBatch"</span>).to(<span class="code-quote">"spring-batch:myJob"</span>).to(<span
class="code-quote">"mock:JobExecutions"</span>);
...
MockEndpoint mockEndpoint = ...;
JobExecution jobExecution = mockEndpoint.getExchanges().get(0).getIn().getBody(JobExecution.class);
BatchStatus currentJobStatus = jobExecution.getStatus();
</pre>
</div></div>

<h3><a name="SpringBatch-Supportclasses"></a>Support classes</h3>

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

<h4><a name="SpringBatch-CamelItemReader"></a>CamelItemReader</h4>

<p><tt>CamelItemReader</tt> can be used to read batch data directly from
the Camel infrastructure.</p>

<p>For example the snippet below configures Spring Batch to read data from JMS queue.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;bean id=<span class="code-quote">"camelReader"</span>
class=<span class="code-quote">"org.apache.camel.component.spring.batch.support.CamelItemReader"</span>&gt;</span>
  <span class="code-tag">&lt;constructor-arg ref=<span class="code-quote">"consumerTemplate"</span>/&gt;</span>
  <span class="code-tag">&lt;constructor-arg value=<span class="code-quote">"jms:dataQueue"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>

<span class="code-tag">&lt;batch:job id=<span class="code-quote">"myJob"</span>&gt;</span>
  <span class="code-tag">&lt;batch:step id=<span class="code-quote">"step"</span>&gt;</span>
    <span class="code-tag">&lt;batch:tasklet&gt;</span>
      <span class="code-tag">&lt;batch:chunk reader=<span class="code-quote">"camelReader"</span>
writer=<span class="code-quote">"someWriter"</span> commit-interval=<span class="code-quote">"100"</span>/&gt;</span>
    <span class="code-tag">&lt;/batch:tasklet&gt;</span>
  <span class="code-tag">&lt;/batch:step&gt;</span>
<span class="code-tag">&lt;/batch:job&gt;</span>
</pre>
</div></div>

<h4><a name="SpringBatch-CamelItemWriter"></a>CamelItemWriter</h4>

<p><tt>CamelItemWriter</tt> has similar purpose as <tt>CamelItemReader</tt>,
but it is dedicated to write chunk of the processed data.</p>

<p>For example the snippet below configures Spring Batch to read data from JMS queue.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;bean id=<span class="code-quote">"camelReader"</span>
class=<span class="code-quote">"org.apache.camel.component.spring.batch.support.CamelItemReader"</span>&gt;</span>
  <span class="code-tag">&lt;constructor-arg ref=<span class="code-quote">"consumerTemplate"</span>/&gt;</span>
  <span class="code-tag">&lt;constructor-arg value=<span class="code-quote">"jms:dataQueue"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>

<span class="code-tag">&lt;batch:job id=<span class="code-quote">"myJob"</span>&gt;</span>
  <span class="code-tag">&lt;batch:step id=<span class="code-quote">"step"</span>&gt;</span>
    <span class="code-tag">&lt;batch:tasklet&gt;</span>
      <span class="code-tag">&lt;batch:chunk reader=<span class="code-quote">"camelReader"</span>
writer=<span class="code-quote">"someWriter"</span> commit-interval=<span class="code-quote">"100"</span>/&gt;</span>
    <span class="code-tag">&lt;/batch:tasklet&gt;</span>
  <span class="code-tag">&lt;/batch:step&gt;</span>
<span class="code-tag">&lt;/batch:job&gt;</span>
</pre>
</div></div>

<h4><a name="SpringBatch-CamelItemProcessor"></a>CamelItemProcessor</h4>

<p><tt>CamelItemProcessor</tt> is the implementation of Spring Batch <tt>org.springframework.batch.item.ItemProcessor</tt>
interface. The latter implementation relays on <a href="http://camel.apache.org/request-reply.html"
class="external-link" rel="nofollow">Request Reply pattern</a> 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.</p>

<p>For example the snippet below performs simple processing of the batch item using
the <a href="http://camel.apache.org/direct.html" class="external-link" rel="nofollow">Direct
endpoint </a> and the <a href="http://camel.apache.org/simple.html" class="external-link"
rel="nofollow">Simple expression language </a>.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;camel:camelContext&gt;</span>
  <span class="code-tag">&lt;camel:route&gt;</span>
    <span class="code-tag">&lt;camel:from uri=<span class="code-quote">"direct:processor"</span>/&gt;</span>
    <span class="code-tag">&lt;camel:setExchangePattern pattern=<span class="code-quote">"InOut"</span>/&gt;</span>
    <span class="code-tag">&lt;camel:setBody&gt;</span>
      <span class="code-tag">&lt;camel:simple&gt;</span>Processed ${body}<span
class="code-tag">&lt;/camel:simple&gt;</span>
    <span class="code-tag">&lt;/camel:setBody&gt;</span>
  <span class="code-tag">&lt;/camel:route&gt;</span>
<span class="code-tag">&lt;/camel:camelContext&gt;</span>

<span class="code-tag">&lt;bean id=<span class="code-quote">"camelProcessor"</span>
class=<span class="code-quote">"org.apache.camel.component.spring.batch.support.CamelItemProcessor"</span>&gt;</span>
  <span class="code-tag">&lt;constructor-arg ref=<span class="code-quote">"producerTemplate"</span>/&gt;</span>
  <span class="code-tag">&lt;constructor-arg value=<span class="code-quote">"direct:processor"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>

<span class="code-tag">&lt;batch:job id=<span class="code-quote">"myJob"</span>&gt;</span>
  <span class="code-tag">&lt;batch:step id=<span class="code-quote">"step"</span>&gt;</span>
    <span class="code-tag">&lt;batch:tasklet&gt;</span>
      <span class="code-tag">&lt;batch:chunk reader=<span class="code-quote">"someReader"</span>
writer=<span class="code-quote">"someWriter"</span> processor=<span class="code-quote">"camelProcessor"</span>
commit-interval=<span class="code-quote">"100"</span>/&gt;</span>
    <span class="code-tag">&lt;/batch:tasklet&gt;</span>
  <span class="code-tag">&lt;/batch:step&gt;</span>
<span class="code-tag">&lt;/batch:job&gt;</span>
</pre>
</div></div>


<h4><a name="SpringBatch-CamelJobExecutionListener"></a>CamelJobExecutionListener</h4>

<p><tt>CamelJobExecutionListener</tt> is the implementation of the <tt>org.springframework.batch.core.JobExecutionListener</tt>
interface sending job execution events to the Camel endpoint.</p>

<p>The <tt>org.springframework.batch.core.JobExecution</tt> instance produced
by the Spring Batch is sent as a body of the message. To distinguish between before- and after-callbacks
<tt>SPRING_BATCH_JOB_EVENT_TYPE</tt> header is set to the <tt>BEFORE</tt>
or <tt>AFTER</tt> value.</p>

<p>The example snippet below sends Spring Batch job execution events to the JMS queue.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;bean id=<span class="code-quote">"camelJobExecutionListener"</span>
class=<span class="code-quote">"org.apache.camel.component.spring.batch.support.CamelJobExecutionListener"</span>&gt;</span>
  <span class="code-tag">&lt;constructor-arg ref=<span class="code-quote">"producerTemplate"</span>/&gt;</span>
  <span class="code-tag">&lt;constructor-arg value=<span class="code-quote">"jms:batchEventsBus"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>

<span class="code-tag">&lt;batch:job id=<span class="code-quote">"myJob"</span>&gt;</span>
  <span class="code-tag">&lt;batch:step id=<span class="code-quote">"step"</span>&gt;</span>
    <span class="code-tag">&lt;batch:tasklet&gt;</span>
      <span class="code-tag">&lt;batch:chunk reader=<span class="code-quote">"someReader"</span>
writer=<span class="code-quote">"someWriter"</span> commit-interval=<span class="code-quote">"100"</span>/&gt;</span>
    <span class="code-tag">&lt;/batch:tasklet&gt;</span>
  <span class="code-tag">&lt;/batch:step&gt;</span>
  <span class="code-tag">&lt;batch:listeners&gt;</span>
    <span class="code-tag">&lt;batch:listener ref=<span class="code-quote">"camelJobExecutionListener"</span>/&gt;</span>
  <span class="code-tag">&lt;/batch:listeners&gt;</span>
<span class="code-tag">&lt;/batch:job&gt;</span>
</pre>
</div></div>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/SpringBatch">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=27848800&revisedVersion=12&originalVersion=11">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/SpringBatch?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message