How to get JobParameter and JobExecutionContext in the ItemWriter?

java, spring-batch

Solution

Implementing StepExecutionListener is one way. In fact that's the only way in Spring Batch 1.x.

Starting from Spring Batch 2, you have another choice: You can inject whatever entries in Job Parameters and Job Execution Context to your item writer. Make your item writer with `step` scope, then make use of expression like `#{jobParameters['theKeyYouWant']}` or `#{jobExecutionContext['someOtherKey']}` for value injecting to you item writer.

Problem

I want to retrieve `JobParameter` and `JobExecutionContext` object in my `ItemWriter` class. How to proceed? I tried implementing `StepExecutionListener` through which I am just calling the parent class methods. But it is not succeeding. Thanks in advance.

Original source

Related problems