How do I get Spring Batch Job ContextId in ItemProcessor or ItemWriter?

Muneer Ahmed Syed picture Muneer Ahmed Syed · Jan 10, 2013 · Viewed 17.9k times · Source

I need to store Job ExecutionId as one of the fields of Entity. (I am using JpaItemWriter) One of topic here explains from StepExcecution, I can get StepContext -> JobExecution. In that case how to get StepExecution?

(I have no need to pass any data from one step to another, all I need is JobExecuionId)

Thanks for help, Muneer Ahmed

Answer

Awanish Kumar picture Awanish Kumar · Oct 8, 2015

We can set the scope as job using @Scope("job") of Itemprocessor and can easly get JobExecution using @Value("#{jobExecution}") expression as below.

@Service
@Scope("job")
public class XsltTransformer implements ItemProcessor<Record, Record> {

@Value("#{jobExecution}")
private JobExecution jobExecution;

}