搜索

60.7. 支持类

download PDF

除了组件外,Camel Spring Batch 还提供可用于 hook Spring Batch 基础架构的支持类。

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

60.7.2. CamelItemWriter

CamelItemWriterCamelItemReader 类似,但专用于编写已处理的数据块。

例如,以下代码片段将 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>

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

60.7.4. CamelJobExecutionListener

CamelJobExecutionListenerorg.springframework.batch.core.JobExecutionListener 接口将作业执行事件发送到 Camel 端点的实现。

Spring Batch 生成的 org.springframework.batch.core.JobExecution 实例作为消息的正文发送。为了区分 before- 和 after-callbacks SPRING_BATCH_JOB_EVENT_TYPE 标头,设置为 BEFOREAFTER 值。

以下示例片断将 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>
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.