【问题标题】:Generic Item Reader and Item Writer in spring-batchspring-batch 中的通用项目阅读器和项目编写器
【发布时间】:2013-04-17 07:29:29
【问题描述】:

如何编写通用项目阅读器和项目编写器,以便在工作的所有步骤中重复使用相同的阅读器和编写器。请帮忙?

提前致谢。

【问题讨论】:

  • 你能详细说明你的问题吗?你想达到什么目的?您的所有步骤是否都从同一源读取和写入?
  • 是的,所有步骤都从单个 Xml 文件读取并写入数据库表。

标签: spring-batch


【解决方案1】:

Spring Batch 作为ItemReaderItemProcessorItemWriter 适配器“开箱即用”,可以帮助使许多功能通用。本质上,ItemReaderAdapter 允许您调用现有对象和方法,而无需编写自己的阅读器。

这是一个配置示例

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:batch="http://www.springframework.org/schema/batch"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd">


    <batch:job id="sampleJob">
        <batch:step id="sampleJob.step1">
            <batch:tasklet>
                <batch:chunk reader="readerAdapter" writer="..." commit-interval="10"/>
            </batch:tasklet>
        </batch:step>
    </batch:job>

    <bean id="readerAdapter" class="org.springframework.batch.item.adapter.ItemReaderAdapter" scope="step">
        <property name="targetObject" ref="myService"/>
        <property name="targetMethod" value="get"/>
        <property name="arguments" value="#{jobParameters['parameterName']}"/>
    </bean>

    <bean id="myService"/>

</beans>

块中的每个“阶段”都有适配器。查看每个适配器上的 javadoc,如果您的服务返回正确类型的对象以参与块,它们将有助于获得更好的图片。

【讨论】:

    猜你喜欢
    • 2017-03-23
    • 2017-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    • 2021-06-23
    • 2016-07-19
    • 2018-06-06
    相关资源
    最近更新 更多