1

I have a need to store a variable in ChunkContext. The variable is available after the Processor and I want to get it's value in my Writer.

Processor:

public class MyProcessor implements
        ItemProcessor<ModelItem, ModelItem> {

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

    private long averageWeightInChunk;

    ...


Writer:

public class MyWriter implements ItemWriter<PatentWrapper> {

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

    private long averageWeightInChunk;

    ...


But then I have no idea if I need to use the afterChunk() or beforeChunk() of a ChunkListener and/or if it's possible to just inject the value in the xml like this:

<bean id="MyWriter" scope="step">
        <property name="averageWeightInChunk" value="#{chunkContext[awic]}" />       
    </bean>
dimzak
  • 2,511
  • 8
  • 38
  • 51
  • Set the value of averageWeightInChunk in ModelItem in Item Processor and read it in Item Writer. This will work perfectly no matter how many Item Processors are used. Read here about [How do I get Spring Batch Job ContextId in ItemProcessor or ItemWriter?](http://stackoverflow.com/questions/14263583/how-do-i-get-spring-batch-job-contextid-in-itemprocessor-or-itemwriter). – Braj Mar 04 '14 at 15:48

1 Answers1

1

Move averageWeightInChunk calculation from processor to writer (similar as described in Grouping/summarizing spring-batch records) or - if you want to mantain logic into processor - just store averageWeightInChunk into step execution context (#{chunkContext} doesn't exist)

Community
  • 1
  • 1
Luca Basso Ricci
  • 17,829
  • 2
  • 47
  • 69
  • Application logic should stay in the Processor. I've thought of doing it with stepContext but as I understand:1)Value will have to be overwritten with each chunk, 2)averageWeight will be persisted in jobrepo(not what I want). Afaik ChunkContext is exactly what I need but I can not find a simple implementation to use it – dimzak Mar 04 '14 at 15:22
  • chunkContext doesn't exists exposed as spEL variable (only in Tasklet is accessible) or into reader/processor/writer lifecycle. If you absolutely need variable in "chunk context" check comment of @Braj, is the best choice – Luca Basso Ricci Mar 04 '14 at 16:03