2014-03-03 13 views
6

如何實現必須讀取列表並重複列表中每個項目的一個或多個步驟的Spring Batch作業?爲列表中的每個項目重複一個Spring批處理流程步驟

我目前正在閱讀列表中的一個步驟,然後我把它放在工作環境中。但是作業上下文在數據庫中是存在的,如果它變得太大,就必須使用CLOB,而我無法訪問它。

因此,我正在尋找一種解決方案,不涉及在作業上下文中存儲整個列表。

當然,我可以簡單地把列表放在一個局部變量中。但我很好奇是否有更多類似Spring的批處理選項。

+1

你有沒有考慮鏈項目處理器? http://docs.spring.io/spring-batch/reference/html/readersAndWriters.html#chainingItemProcessors –

+0

我的工作應該是這樣的: 1.閱讀列表 2.從列表中取一個項目並對它做一些操作 3.閱讀器 - >處理器 - >寫入器 4.如果列表中有更多項目,請重複步驟2 所以我不能鏈接任何處理器。 –

+0

這聽起來像一個相當典型的工作過程。閱讀 - >進程 - >寫入。我沒有看到這個問題是什麼? –

回答

2

除了上面關於構造工作的意見(我傾向於同意)之外,如果您使用最新的3.0.0.M3,您可以創建一個JobScope'ed容器來容納集合當你循環完成各個步驟。從那裏您可以讀取/處理/寫入該容器而不是外部源。

+0

我不能使用Spring Batch 3,所以我沒有工作範圍。到目前爲止,我發現的最佳解決方案是將列表存儲在與作業執行ID相關的另一個集合中。關於調整工作,我沒有看到這可以提供什麼幫助。我有一個列表,我爲每個項目在本地複製遠程文件,然後使用STAX讀取器讀取它,然後處理並將每個項目寫入我的數據庫。我重複一遍。我有太多數據將其存儲在內存中或暫時保存在數據庫中。 –

2

正如Spring Batch-Repeat step for each item in a data list問題所述,Partitioning似乎是解決方案。

配置爲PartitionHandler:

 <batch:step id="anyNameJobMaster"> 
      <batch:partition step="nameOfActualStepToBeIterated" partitioner="partitioner"> 
       <batch:handler grid-size="10" task-executor="taskExecutor" /> 
      </batch:partition> 
     </batch:step> 

     <bean id="partitioner" class="org.example.PartitionerThatImplementsPartitioner" lazy-init="true" scope="step"/> 

PartitionHandler示例代碼:

public class PartitionerThatImplementsPartitioner implements Partitioner{ 
    @Override 
    public Map<String, ExecutionContext> partition(int gridSize) { 
     Map<String, ExecutionContext> map = new HashMap<String, ExecutionContext>(gridSize); 
     //Iteration goes here 
     //For Each Iteration, create ExecutionContext and add it to map. 
     map.put("somePartionKeyString", new ExecutionContext().put("ContextIdentifiere.g.FileName", "IteratedVale.g.FilePath")) 

    } 
} 

作業配置:

 <batch:step id="nameOfActualStepToBeIterated"> 
     <batch:tasklet transaction-manager="someTxManager"> 
      <batch:chunk processor="someProcessor" 
       writer="itemWriter" commit-interval="${commitInterval}"> 
       <batch:reader> 
        <bean id="itemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step"> 
         <property name="resource" value="#{stepExecutionContext['ContextIdentifiere.g.FileName']}" />      
        </bean> 
       </batch:reader> 
      </batch:chunk> 
     </batch:tasklet> 
相關問題