我需要在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>
設置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