Muneer Ahmed Syed
Muneer Ahmed Syed

Reputation: 171

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

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

Upvotes: 13

Views: 28010

Answers (7)

Velmurugan K
Velmurugan K

Reputation: 21

I had same issue. I used @StepScope in my processor Implementation

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

Upvotes: 1

Tanumoy Mitra
Tanumoy Mitra

Reputation: 41

You have to add the following:

@Value("#{stepExecution.jobExecution}")

private JobExecution jobExecution;

in your reader Writer or processor. It will certainly work.

Upvotes: 4

Rahul Raj
Rahul Raj

Reputation: 107

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

Upvotes: 3

Tony Edwards
Tony Edwards

Reputation: 427

I had a similar issue when I wanted to get the JobInstanceID. Below is how I got StepExecution and then retreived JobExecution. I hope this helps.

public class FoobarItemProcessor implements ItemProcessor<Foobar, Foobar> {

private JobExecution jobExecution;

@BeforeStep
public void beforeStep(StepExecution stepExecution) {
    jobExecution = stepExecution.getJobExecution();
}

Upvotes: 0

Awanish Kumar
Awanish Kumar

Reputation: 640

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;

}

Upvotes: 5

nekperu15739
nekperu15739

Reputation: 3698

If you want to use @BeforeStep in MyEntityProcessor you must declare it like a listener

<batch:listeners>
 <batch:listener ref="myEntityProcessor" />
</batch:listeners>

Upvotes: 1

incomplete-co.de
incomplete-co.de

Reputation: 2137

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;
}

}

Upvotes: 7

Related Questions