[英]Spring-batch flow / split after a step
我正在構建一個包含以下過程的 spring-batch 解決方案:
第 1 步:將一個列表拆分為多個列表 第 2 步:處理每個子列表 第 3 步:合並子列表
生成的子列表可以並行處理,根據 spring-batch 文檔,這是支持的。 遺憾的是,我只能找到以並行步驟開始的 spring-batch 示例作業,而不是順序開始的示例。
以下作業將無法編譯。 Spring 給了我一個錯誤:'無法解決 step2'
<batch:job id="webServiceJob2">
<batch:step id="step1" next="step2"></batch:step>
<batch:split id="step2" next="step3"></batch:split>
<batch:step id="step3"></batch:step>
</batch:job>
那么如何將作業配置為首先運行一個步驟,而不是並行運行多個步驟,然后運行最后一個步驟?
我偶然發現了這個問題,詢問拆分是如何工作的,也許這個答案來晚了一點(一年),但我走了……
存在“拆分”的問題本身並不是一個步驟,而是您按原樣命名(和引用)它:
<batch:job id="webServiceJob2">
<batch:step id="step1" next="step2"></batch:step>
<batch:split id="step2" next="step3"></batch:split> <!-- This is not a step -->
<batch:step id="step3"></batch:step>
</batch:job>
正確的語法是:
<batch:job id="webServiceJob2">
<batch:step id="step1" next="step2"></batch:step>
<batch:split id="split_step2" next="step3">
<flow>
<step id="step2_A_1" ... next="step2_A_2"/>
<step id="step2_A_2" ... />
</flow>
<flow>
<step id="step2_B_1" ... />
</flow>
</batch:split>
<batch:step id="step3"></batch:step>
</batch:job>
但這不是您想要實現的,因為通過split
聲明,您必須在編譯時設置將執行的並行步驟的確切數量,並且拆分的目的是在每個流程中使用不同的步驟,而不是多次調用相同的一。
您應該查看有關Scaling and Parallel processes的文檔,分區步驟似乎很適合您的要求。
當然,你可以在工作中分裂! 這是 Spring Batch In Action (2012) 中的示例。
<batch:job id="importProductsJob">
<batch:step id="decompress" next="readWrite">
<batch:tasklet ref="decompressTasklet"/>
</batch:step>
<batch:split id="readWrite" next="moveProcessedFiles">
<batch:flow>
<batch:step id="readWriteBookProduct"/>
</batch:flow>
<batch:flow>
<batch:step id="readWriteMobileProduct"/>
</batch:flow>
</batch:split>
<batch:step id="moveProcessedFiles">
<batch:tasklet ref="moveProcessedFilesTasklet" />
</batch:step>
</batch:job>
並行步驟將指示每個子列表的不同步驟,我認為這不是您想要的。
單個多線程步驟似乎更合適。
如文檔所述,您首先定義一個TaskExecutor
bean,它將在單獨的線程中處理每個塊。 由於 TaskExecutor 使用起來相當簡單,您也可以自己調用 TaskExecutor。 在這種情況下,您的步驟可以是多線程的,而 Spring Batch 不需要知道它。
執行以下操作有望對您有所幫助:
<job id="job">
<step id="step_0" next="split_1">
<tasklet ref="taskletStep_4"/>
</step>
<split id="split_1" next="step_5" task-executor="taskExecutor">
<flow>
<step id="step_1" next="step_2">
<tasklet ref="taskletStep_1"/>
</step>
<step id="step_2" next="step_3">
<tasklet ref="taskletStep_2"/>
</step>
<step id="step_3">
<tasklet ref="taskletStep_3"/>
</step>
</flow>
<flow>
<step id="step_4">
<tasklet ref="taskletStep_4"/>
</step>
</flow>
</split>
<step id="step_5">
<tasklet ref="taskletStep_5"/>
</step>
</job>
<beans:bean id="taskletStep_1" class="com.test.batch.parallelstep.step.SimpleStep1" />
<beans:bean id="taskletStep_2" class="com.test.batch.parallelstep.step.SimpleStep2" />
<beans:bean id="taskletStep_3" class="com.test.batch.parallelstep.step.SimpleStep3" />
<beans:bean id="taskletStep_4" class="com.test.batch.parallelstep.step.SimpleStep4" />
<beans:bean id="taskletStep_5" class="com.test.batch.parallelstep.step.SimpleStep5" />
<beans:bean id="taskExecutor" class="org.springframework.core.task.SimpleAsyncTaskExecutor" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.