2013-11-28 236 views
4

我建立一個包含以下過程的彈簧批量溶液:彈簧批量流/分流

步驟1:分割列表分成多個列表 步驟2:過程中的每個子列表 步驟3:合併子列表

生成的子列表可以並行處理,並且根據spring-batch文檔支持。令人遺憾的是,我只能找到以並行步驟開始的Spring批處理示例作業,而不是按順序開始的示例。

以下作業不會編譯。春天給我一個錯誤:「無法解決第二步」

<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> 

所以,我怎麼能配置作業首先運行一個單一的步驟,比運行多個並行的步驟,然後運行最後一步?

回答

2

我已經就這個問題跌跌撞撞地詢問如何分割的作品,也許這個答案到達一個位(一年)晚,但在這裏我去...

問題有「分裂」不是一步本身,而是你被命名(和引用),因爲它是:

<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聲明必須在編譯時設置將執行的並行步驟的確切數量,並且split的目的是在每個流中使用不同的步驟,而不是多次調用同一個步驟。

您應該檢查有關Scaling and Parallel processes的文檔,分區步驟似乎是您的要求的一個很好的候選人。

0

並行步驟將爲每個子列表指示不同的步驟,我不認爲這是您想要的。
單個Multi-threaded Step似乎更合適。
如文檔所述,您首先定義一個TaskExecutor bean,它將在單獨的線程中處理每個。由於TaskExecutors的使用相當簡單,您也可以自行調用TaskExecutor。在這種情況下,您的步驟可以是多線程的,無需Spring Batch需要了解它。

+0

謝謝你的答案,但它不能解決我的問題。每個子列表需要用不同的邏輯處理。 (不同的讀取和處理邏輯)我會看看多線程步驟。現在我已經通過在讀取和處理步驟中基於標記使用switch語句來解決我的問題。 – lulu

0

當然,您可以在工作中分一杯羹!以下是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>