130.8. 支持类
除了组件外,Camel Spring Batch 还还提供可用于 hook 到 Spring Batch 基础架构的支持类。
130.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>
130.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>
130.8.3. CamelItemProcessor 复制链接链接已复制到粘贴板!
CamelItemProcessor 是 Spring Batch org.springframework.batch.item.ItemProcessor 接口的实现。在 Request Reply 模式 上的后一种实施中继,将批处理项目的处理委派给 Camel 基础架构。要处理的项目作为消息的正文发送到 Camel 端点。
例如,以下代码片段使用 Direct 端点和 Simple 表达式语言 执行批处理项目的简单处理。
<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>
130.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>