【问题标题】:executing multiple processing class with Spring batch processing使用 Spring 批处理执行多个处理类
【发布时间】:2014-10-28 11:02:15
【问题描述】:

我是春季批处理的新手。只是想要一些建议,以便我可以在阅读有关 Spring Batch 的信息时进行连接。

我的场景如下: 我写了 4 个 java 类,它们可以读取和修改 oracle 中的数据。 例如:Class1 和 Class2 会修改表 1,Class3 和 Class4 会修改表 2

我们如何使用 Spring 批处理并行执行这些类?

【问题讨论】:

    标签: java batch-processing spring-batch


    【解决方案1】:

    在不知道每个课程的作用的情况下,我所能提供的建议是有限度的。话虽如此,如果您只想使用 Spring Batch 并行执行每个类,Spring Batch 提供了一些工具来帮助您:

    1. Split - Spring Batch 中的拆分是流的划分,以便可以并行执行步骤。在您的情况下,我希望并行执行两个或四个流(取决于您是否需要按顺序执行 Class1 -> Class 2 但该序列与 Class3 -> Class4 并行执行,或者您是否可以运行所有四个类并行)。
    2. MethodInvokingTaskletAdapter - Spring Batch 提供的 Tasklet 实现允许您在事务范围内对指定 bean 执行方法。这允许您使用 Tasklet 包装现有类,以便 Spring Batch 轻松使用它们。

    有了上述概念,您可以将批处理作业配置为如下所示:

    <job id="job1">
        <split id="split1">
            <flow>
                <step id="split1Step1" next="split1Step2">
                    <tasklet ref="class1Tasklet"/>
                </step>
                <step id="split1Step2">
                    <tasklet ref="class2Tasklet"/>
                </step>
            </flow>
            <flow>
                <step id="split2Step1" next="split2Step2">
                    <tasklet ref="class3Tasklet"/>
                </step>
                <step id="split2Step2">
                    <tasklet ref="class4Tasklet"/>
                </step>
            </flow>
        </split>
    </job>
    
    <bean id="class1Tasklet" class="org.springframework.batch.core.step.tasklet.MethodInvokingTaskletAdapter">
        <property name="targetObject">
            <bean class="Class1"/>
        </property>
        <property name="targetMethod" value="someMethod"/>
    </bean>
    
    <bean id="class2Tasklet" class="org.springframework.batch.core.step.tasklet.MethodInvokingTaskletAdapter">
        <property name="targetObject">
            <bean class="Class2"/>
        </property>
        <property name="targetMethod" value="someMethod"/>
    </bean>
    
    <bean id="class3Tasklet" class="org.springframework.batch.core.step.tasklet.MethodInvokingTaskletAdapter">
        <property name="targetObject">
            <bean class="Class3"/>
        </property>
        <property name="targetMethod" value="someMethod"/>
    </bean>
    
    <bean id="class4Tasklet" class="org.springframework.batch.core.step.tasklet.MethodInvokingTaskletAdapter">
        <property name="targetObject">
            <bean class="Class4"/>
        </property>
        <property name="targetMethod" value="someMethod"/>
    </bean>
    

    您可以在此处的文档中阅读有关 MethodInvokingTaskletAdapter 的更多信息:http://docs.spring.io/spring-batch/apidocs/org/springframework/batch/core/step/tasklet/MethodInvokingTaskletAdapter.html

    【讨论】:

    • 假设您想使用一个类而不是多个类。例如:类 Car 和方法 start()stop()。第 1 步是 start(),第 2 步是 stop()。使用上面的方法,假设您需要在开始和停止之间传递一些数据。如何做到这一点以确保可重启性是可能的?
    猜你喜欢
    • 2019-01-30
    • 2014-12-03
    • 2013-10-21
    • 2018-12-04
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 2015-05-03
    • 1970-01-01
    相关资源
    最近更新 更多