我试图并行处理弹簧批处理作业中的一些步骤。该作业的XML配置如下:
<batch:job id= "job" job-repository = "jobRepository">
<batch:split id="split" task-executor="taskExecutor">
<batch:flow>
<batch:step id = "step1">
<batch:tasklet transaction-manager = "txManager" >
<batch:chunk reader = "reader1"
processor = "processor1"
writer = "writer1"
commit-interval = "1" />
</batch:tasklet>
</batch:step>
</batch:flow>
<batch:flow>
<batch:step id = "step2">
<batch:tasklet transaction-manager = "txManager">
<batch:chunk reader = "reader2"
processor = "processor2"
writer = "writer2"
commit-interval = "1" />
</batch:tasklet>
</batch:step>
</batch:flow>
</batch:split>
</batch:job>
taskexecutor是一个简单的人。
在块中,我正在使用读者,处理器和作家。这些都取决于接缝(2.2.2)。
当步骤以单线螺纹模式运行时,它们都可以正常工作。但是,当它们并行运行时,它们会导致错误,因为没有可用的接缝上下文。显然,因为创建了一个新线程并且没有启动接缝生命周期(lifecycle.begincall())。
如何确保在处理块时开始生命周期?我真的不想在读者中启动生命周期并在作者中结束它,但是当任务完成时执行任务并结束时,应该启动它。
在执行步骤之前开始上下文:
<batch:step id="step1">
<batch:tasklet>
<batch:chunk ...>
</batch:chunk>
</batch:tasklet>
<batch:listeners>
<batch:listener ref="stepExecutionListener"/>
</batch:listeners>
</batch:step>
stepexecutionListener:
public abstract class MyStepExecutionListener implements StepExecutionListener {
@Override
public void beforeStep(StepExecution stepExecution) {
Lifecycle.beginCall();
}
/**
* Default no-op implementation.
* @return ExitStatus of step.
*/
@Override
public ExitStatus afterStep(StepExecution stepExecution) {
return stepExecution.getExitStatus();
}
}