Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get JobParameter and JobExecutionContext in the ItemWriter?

Tags:

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.

like image 223
Smita Avatar asked Feb 19 '13 04:02

Smita


People also ask

How do I get jobExecutionContext in 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. 1. you need the bean being step scope, 2. you need setter for "context" property.

How do I get jobParameters in Spring Batch writer?

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]} .

How do I get StepExecution in Spring Batch?

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(); ... }

How do you pass data from reader to writer in Spring Batch?

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.


Video Answer


1 Answers

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.

like image 104
Adrian Shum Avatar answered Sep 23 '22 18:09

Adrian Shum