How to get JobParameter and JobExecutionContext in the ItemWriter?

Smita picture Smita · Feb 19, 2013 · Viewed 69.2k times · Source

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.

Answer

Adrian Shum picture Adrian Shum · Feb 19, 2013

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.