2013-03-07 83 views
0

我正在使用spring批處理來讀取平面文件。該文件具有相關記錄。即可以有父記錄和任何數量的我想讀取所有記錄並調用Web服務來存儲它的子記錄。我也想抓住關係並存儲它。一個挑戰是孩子的記錄可以在文件的任何地方。而且孩子也可以有很多孩子的記錄。我無法用春天的批次找到這個問題的解決方案。 請提供您的建議春季批處理讀取文件及相關記錄

更新:我沒有任何選擇使用數據庫作爲臨時數據存儲。

回答

0

我已經通過處理文件多次解決了這個問題。

在每一個傳球,我會試着去閱讀\過程與這樣ALG文件中的每一條記錄:

  • 如果記錄有父 - 檢查是否已經存儲父。如果沒有 - 我處理器跳過
  • 如果記錄保持不變(或者已經存儲,如果更新是不可能的) - 跳過它在處理器
  • 其他 - 店分貝

然後聲明循環和決勝局:

<batch:step id="processParentAndChilds" next="loop"> 
     <batch:tasklet> 
      <batch:chunk reader="processParentAndChildsReader" 
         commit-interval="1000"> 
       <batch:processor> 
        <bean class="processParentAndChildsProcessor"/> 
       </batch:processor> 
       <batch:writer> 
        <bean class="processParentAndChildsWriter"/> 
       </batch:writer> 
      </batch:chunk> 
     </batch:tasklet> 
    </batch:step> 
    <batch:decision decider="processParentAndChildsRetryDecider" id="loop"> 
     <batch:next on="NEXT_LEVEL" to="processprocessParentAndChilds"/> 
     <batch:next on="COMPLETED" to="goToNextSteps"/> 
    </batch:decision> 


public class ProcessParentAndChildsRetryDecider implements JobExecutionDecider{ 
@Override 
public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) { 
    // if no on record written - no sense to try again 
    if (stepExecution.getWriteCount() > 0) { 
     return new FlowExecutionStatus("NEXT_LEVEL"); 
    } else { 
     return FlowExecutionStatus.COMPLETED; 
    } 
} 

}

+0

感謝米凱爾。我用另一種方法。我有一個步驟,在該步驟中遍歷文件並創建一個內存映射(讀者讀取文件和處理器創建映射,Writer是一個不可操作的編寫器)。我保存了上下文,並在下一步中通讀地圖並創建域對象和所需的關係。我知道如果文件是巨大的,它會造成內存泄漏。我可以購買更多的內存進行此處理並解決內存問題。因爲我需要避免多次處理文件。不管怎麼說,還是要謝謝你 – 2013-03-15 06:02:47