2014-03-04 66 views
0

我需要在ChunkContext中存儲變量。處理器之後的變量是可用的,我想在Writer中獲得它的值。在Spring批處理中存儲變量並訪問ChunkContext

處理器:

public class MyProcessor implements 
     ItemProcessor<ModelItem, ModelItem> { 

    private Logger logger = LoggerFactory.getLogger(MyProcessor.class); 

    private long averageWeightInChunk; 

    ... 


編劇:

public class MyWriter implements ItemWriter<PatentWrapper> { 

    private static final Logger logger = LoggerFactory 
      .getLogger(MyWriter .class); 

    private long averageWeightInChunk; 

    ... 


但我不知道如果我需要使用的afterChunk()或beforeChunk()一ChunkListener和/或者如果可以像這樣在xml中注入值:

<bean id="MyWriter" scope="step"> 
     <property name="averageWeightInChunk" value="#{chunkContext[awic]}" />  
    </bean> 
+0

設置averageWeightInChunk的值在Item Processor中的ModelItem中,並在Item Writer中讀取它。無論使用多少個物品處理器,這都可以很好地工作。在這裏閱讀[如何在ItemProcessor或ItemWriter中獲得Spring批處理作業ContextId?](http://stackoverflow.com/questions/14263583/how-do-i-get-spring-batch-job-contextid-in-itemprocessor -or-itemwriter)。 – Braj

回答

1

移動從處理器到作家averageWeightInChunk計算(類似於如在Grouping/summarizing spring-batch records所述)或 - 如果要十個分量邏輯到處理器 - 只是存儲averageWeightInChunk入步驟的執行上下文(#{chunkContext}不存在)

+0

應用程序邏輯應該保留在處理器中。我曾經想過用stepContext來做這件事,但據我所知:1)每個塊都必須重寫Value,2)averageWeight將會保存在jobrepo中(不是我想要的)。 Afaik ChunkContext正是我所需要的,但我找不到一個簡單的實現來使用它 – dimzak

+0

chunkContext不存在作爲spEL變量(僅在Tasklet中可訪問)或讀取器/處理器/寫入器生命週期中公開的存在。如果你絕對需要@Braj的「chunk context」檢查評論中的變量,那是最好的選擇 –