Spring批处理多线程vs分区



我有一个需求,我想在下面的场景中使用Spring批处理框架。

我有一个按交易日期列划分的表。我想用Spring批处理框架的reader, processor和writer来处理这个表的记录。我想做的是根据交易日期为读取、写入和处理创建单独的线程。假设有4个交易日期,我想创建4个单独的线程,每个线程对应一个交易日期。在每个线程中,读取器将从交易日期的表中读取记录,在处理器中充实记录,然后在writer中发布/写入。

我是Spring批处理的新手,所以我需要通过使用Spring批处理多线程或分区来设计正确的方法。

也许你可以像这样使用本地分区:

<batch:job id="MyBatch" xmlns="http://www.springframework.org/schema/batch">
    <batch:step id="masterStep">
        <batch:partition step="slave" partitioner="splitPartitioner">
            <batch:handler grid-size="4" task-executor="taskExecutor"  />
        </batch:partition>
    </batch:step>
</batch:job>

然后使用org.springframework.batch.core.partition.support.Partitioner接口创建splitPartitioner。然后在分区方法中,按照你喜欢的方式分割源数据,你创建的每个ExecutionContext都将由它自己的线程执行。

可以使用本地分区主从方法来解决您的问题。在Spring配置中按如下方式编写主、从步骤:

<batch:job id="tradeProcessor">
    <batch:step id="master">
        <partition step="slave" partitioner="tradePartitioner">
            <handler grid-size="4" task-executor="taskExecutor" />
        </partition>
    </batch:step>       
</batch:job>
<batch:step id="slave">
    <batch:tasklet>
        <batch:chunk reader="dataReader" writer="dataWriter"
                          processor="dataProcessor" commit-interval="10">
        </batch:chunk>
    </batch:tasklet>
</batch:step>

有关详细信息,您可以参考这里讨论的简单示例。

最新更新