2011-06-30 423 views
0

我正在使用Spring Batch,並創建了一個使用SimpleAsyncTaskExecutor運行的tasklet。在這一步,我檢索StepExecutionContext在Spring批處理中的多線程步驟中寫入StepExecutionContext

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

在微進程的處理方法,我嘗試更新的背景:

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

這導致ConcurrentModificationException S於該stepExecution。 如何避免這些並在此多線程環境中更新我的上下文?

+0

請提供更多信息,例如其中spring批處理版本和tasklet實現的來源 –

+0

SpringBatch 2.1.8。你需要回答這個問題需要哪些細節? – tobiasbayer

+0

如果您使用'org.springframework.batch.core.step.tasklet.Tasklet'實現了一個tasklet,那麼您需要實現'RepeatStatus execute(StepContribution貢獻,ChunkContext chunkContext)'並且chunkContext裏面是一個可修改的stepExecutionContext,您可以使用BeforeStep註解獲得stepExecution,所以我不確定你的「tasklet」是什麼樣子,或者它甚至是「Tasklet」 –

回答

0

步驟執行上下文是共享資源。你真的試圖把每個線程一個「信息」?根據你的上下文,有很多方法可以解決這個問題,因爲它是一個線程問題,而不是Spring批處理。

1)如果每個線程有一個信息,讓線程在上下文中放置一個threadlocal(一次),然後使用threadlocal存儲「info」。 2)如果上下文信息是「全局」,那麼你應該把它放在一個同步塊中,並在放置之前檢查它的存在。

希望這會有所幫助。

相關問題