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.
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. 1. you need the bean being step scope, 2. you need setter for "context" property.
JobParameters can be used for identification or even as reference data during the job run. They have reserved names, so to access them we can use Spring Expression Language. For example to access a property 'abc' on job parameters: we can access it using the syntax #{jobParameters[abc]} .
A chunk oriented tasklet can directly access the StepExecution from the ChunkContext : @Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { StepExecution stepExecution = chunkContext. getStepContext(). getStepExecution(); ... }
In your Reader and Writer you need to implement ItemStream interface and use ExecutionContext as member variable. Here i have given example with Processor instead of Writer but same is applicable for Writer as well . Its working fine for me and i am able to take values from reader to processor.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With