123.8. 支持类
除了组件外,Camel Spring Batch 还提供可用于 hook Spring Batch 基础架构的支持类。
123.8.1. CamelItemReader 复制链接链接已复制到粘贴板!
CamelItemReader 可用于直接从 Camel 基础架构读取批处理数据。
例如,以下代码片段将 Spring Batch 配置为从 JMS 队列中读取数据:
<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>
123.8.2. CamelItemWriter 复制链接链接已复制到粘贴板!
CamelItemWriter 与 CamelItemReader 类似,但专用于编写已处理的数据块。
例如,以下代码片段将 Spring Batch 配置为从 JMS 队列读取数据。
<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>
123.8.3. CamelItemProcessor 复制链接链接已复制到粘贴板!
CamelItemProcessor 是 Spring Batch org.springframework.batch.item.ItemProcessor 接口的实现。后一种实施转发 Request Reply 模式,将批处理项目的处理委托给 Camel 基础架构。要处理的项目作为消息的正文发送到 Camel 端点。
例如,以下代码片段使用直接端点和简单 表达式语言 对批处理项目执行简单的处理。http://camel.apache.org/direct.html
<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>
123.8.4. CamelJobExecutionListener 复制链接链接已复制到粘贴板!
CamelJobExecutionListener 是 org.springframework.batch.core.JobExecutionListener 接口将作业执行事件发送到 Camel 端点的实现。
Spring Batch 生成的 org.springframework.batch.core.JobExecution 实例作为消息的正文发送。为了区分 before- 和 after-callbacks SPRING_BATCH_JOB_EVENT_TYPE 标头,设置为 BEFORE 或 AFTER 值。
以下示例片断将 Spring Batch 作业执行事件发送到 JMS 队列。
<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>