首页 文章

使用Spring批处理执行多个处理类

提问于
浏览
0

我是 Spring 季批处理的新手 . 只是想要一些建议,以便我可以在阅读 Spring 季批次时进行连接 .

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

我们如何通过spring批处理来并行化这些类的执行?

1 回答

  • 0

    在不知道每个 class 的作用的情况下,我可以提供的建议有多么有限 . 话虽这么说,如果你想要做的就是使用Spring Batch并行执行每个类,Spring Batch提供了一些工具来帮助解决这个问题:

    • 拆分 - Spring Batch中的拆分是流的划分,以便可以并行执行步骤 . 在你的情况下,我希望并行执行两个或四个流(取决于你是否需要按顺序执行Class1 - > Class 2,但是该序列与Class3并行 - > Class4,或者如果你可以运行所有四个并行的类) .

    • 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

相关问题