搜索

319.6. 支持类

download PDF

除了组件外,Camel Spring Batch 还支持类,它们可用于 hook 到 Spring Batch 基础架构中。

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

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

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

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