【问题标题】:Spring batch Partitioning with multiple steps in parallel?Spring批处理分区并行多个步骤?
【发布时间】:2014-04-29 06:23:14
【问题描述】:

我已经为单个步骤实现了弹簧批处理分区,其中一个主步骤将其工作委托给多个从线程,然后并行执行。如下图所示。(参考Spring docs) 现在,如果我有多个要并行执行的步骤怎么办?如何在批量配置中配置它们?我目前的配置是

 <batch:job id="myJob" restartable="true" job-repository="jobRepository" >
        <batch:listeners>
            <batch:listener ref="myJoblistener"></batch:listener>
        </batch:listeners>

        <batch:step id="my-master-step">
            <batch:partition step="my-step" partitioner="my-step-partitioner" handler="my-partitioner-handler">
            </batch:partition>
        </batch:step>
    </batch:job>

    <batch:step id="my-step" >
        <batch:tasklet ref="myTasklet" transaction-manager="transactionManager" >
        </batch:tasklet>
        <batch:listeners>
            <batch:listener ref="myStepListener"></batch:listener>
        </batch:listeners> 
    </batch:step>

我的架构图应该如下图所示:

我不确定是否可以使用弹簧批处理。任何想法或我都无法实现它。谢谢。

【问题讨论】:

标签: java spring spring-mvc parallel-processing spring-batch


【解决方案1】:

您可以尝试以下方法。

 <batch:job id="myJob" restartable="true" job-repository="jobRepository" >
        <batch:listeners>
            <batch:listener ref="myJoblistener"></batch:listener>
        </batch:listeners>

        <batch:step id="my-master-step">
            <batch:partition step="my-step" partitioner="my-step-partitioner" handler="my-partitioner-handler">
            </batch:partition>
        </batch:step>
    </batch:job>

    <batch:step id="my-step" >
        <batch:job ref="MyChildJob" job-launcher="jobLauncher"
                job-parameters-extractor="jobParametersExtractor" />
        <batch:listeners>
            <batch:listener ref="myStepListener"></batch:listener>
        </batch:listeners> 
    </batch:step>

    <batch:job id="MyChildJob" restartable="false"
        xmlns="http://www.springframework.org/schema/batch">
        <batch:step id="MyChildStep1" next="MyChildStep2">
            <batch:tasklet ref="MyChildStep1Tasklet" transaction-manager="transactionManager" >
            </batch:tasklet>
        </batch:step>

        <batch:step id="MyChildStep2" next="MyChildStep3">
            <batch:tasklet ref="MyChildStep2Tasklet" transaction-manager="transactionManager" >
            </batch:tasklet>
        </batch:step>

        <batch:step id="MyChildStep3">
            <batch:tasklet ref="MyChildStep3Tasklet" transaction-manager="transactionManager" >
            </batch:tasklet>
        </batch:step>

    </batch:job>

【讨论】:

  • 我不认为它会起作用...通过查看您的配置,每个 tasklet 都在运行不同的作业..这是不可取的..我需要有一个上述的批处理作业行为..谢谢
  • 我已经配置了相同的方式并且在生产中工作正常。如果您不需要将其作为单独的作业,请将其配置为流程。该流程包含所有三个步骤。
  • 嗨@DanglingPiyush。你解决了你的问题吗。我有同样的要求。你能帮我吗
【解决方案2】:

我有类似的要求并使用以下要求解决了它

<batch:job id="cycleJob">
        <batch:step id="zStep" next="gStep">
            <batch:partition partitioner="zPartitioner">
                <batch:step>
                    <batch:tasklet throttle-limit="1">
                        <batch:chunk processor="itemProcessor" reader="zReader" writer="itemWriter" commit-interval="1">
                        </batch:chunk>
                    </batch:tasklet>
                </batch:step>
                <batch:handler task-executor="taskExecutor" grid-size="${maxThreads}" />
            </batch:partition>
        </batch:step>
        <batch:step id="gStep" parent="zStep" next="yStep">
            <batch:partition partitioner="gPartitioner">
                <batch:step>
                    <batch:tasklet throttle-limit="1">
                        <batch:chunk processor="itemProcessor" reader="gReader" writer="itemWriter" commit-interval="1">
                        </batch:chunk>
                    </batch:tasklet>
                </batch:step>
                <batch:handler task-executor="taskExecutor" grid-size="${maxThreads}" />
            </batch:partition>
        </batch:step>
</batch:job>

【讨论】:

    【解决方案3】:

    迟到的答案,但我终于找到了我最初来这里时寻找的解决方案,使用 flow 而不是子作业。所以我想我也应该在这里发布。

        <job id="myJob">
            <step id="my-master-step">
                <partition partitioner="my-step-partitioner">
                    <handler task-executor="my-partitioner-handler" />
                    <step>
                        <!-- For each partition, we run the complete flow -->
                        <flow parent="mainFlow" />
                    </step>
                </partition>
            </step>
        </job>
        
        <!-- The flow consists of several sequential steps (2 here) -->
        <flow id="mainFlow">
            <step id="MyChildStep1" next="MyChildStep2">
                <!-- Here you can have a tasklet or a chunk of course -->
                <tasklet ref="MyChildStep1Tasklet" />
            </step>
            <step id="MyChildStep2">
                <!-- Same here -->
                <tasklet ref="MyChildStep2Tasklet" />
            </step>
        </flow>
        
        <bean id="MyChildStep1Tasklet" class="..." />
        
        <bean id="MyChildStep1Tasklet" class="..." />
    

    我没有测试过并行运行它,但我看不出它为什么不能工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-07
      • 2014-08-25
      • 1970-01-01
      • 1970-01-01
      • 2019-07-30
      • 1970-01-01
      • 1970-01-01
      • 2016-10-11
      相关资源
      最近更新 更多