Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

spring-batch

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

like image 720
Muneer Ahmed Syed Avatar asked Jan 10 '13 17:01

Muneer Ahmed Syed


2 Answers

I would suggest you use a processor that updates your Entity with value. If your processors directly implements ItemProcessor<T> then you will not automatically get the StepExecution. To get the StepExecution, do 1 of the following; - implement StepExecutionListener and set it as a variable from the beforeStep method - create a method called [something](StepExecution execution) and annotate with @BeforeStep

once you've injected the StepExecution via a listener, you can then get the jobExecutionId and set it into your entity

public class MyEntityProcessor implements ItemProcessor<MyEntity, MyEntity> {

private long jobExecutionId;

@BeforeStep
public void beforeStep(StepExecution stepExecution) {
    jobExecutionId = stepExecution.getJobExecutionId();
}

@Override
public MyEntity process(MyEntity item) throws Exception {
    //set the values
    item.setJobExecutionId(jobExecutionId);
    //continue
    return item;
}

}
like image 191
incomplete-co.de Avatar answered Sep 30 '22 14:09

incomplete-co.de


Scope as job using @Scope("job") of and get JobExecution using @Value("#{jobExecution}")

like image 31
Rahul Raj Avatar answered Sep 30 '22 13:09

Rahul Raj