tobiasbayer
tobiasbayer

Reputation: 10379

Writing to the StepExecutionContext in multi-threaded steps in Spring Batch

I am using Spring Batch and I've created a tasklet that is run by using a SimpleAsyncTaskExecutor. In this step, I am retrieving the StepExecutionContext with

@BeforeStep
public void saveStepExecution(StepExecution stepExecution) {
    this.stepExecution = stepExecution;
}

In the processing method of the tasklet, I try to update the context:

stepExecution.getExecutionContext().put("info", contextInfo);

This leads to ConcurrentModificationExceptions on the stepExecution. How can I avoid these and update my context in this multi-threaded environment?

Upvotes: 1

Views: 3643

Answers (1)

Chuck
Chuck

Reputation: 11

The step execution context is a shared resource. Are you really trying to put one "info" per thread? Depending on your context, there are many ways to solve this, since it is a threading issue, not Spring batch.

1) if there is one info per thread, have the thread put a threadlocal in the context (once), and then use the threadlocal to store the "info".

2) if context info is "global", then you should do the put in a synchronized block and check for its existence before putting.

Hope this helps.

Upvotes: 1

Related Questions