Spring Batch并行Seam上下文,没有应用程序上下文活动

时间:2014-01-30 15:48:27

标签: java parallel-processing spring-batch seam2

我正在尝试并行处理Spring Batch作业中的一些步骤。作业的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是一个SimpleAsyncTaskExecutor。

我正在使用读卡器,处理器和编写器。这些都依赖于Seam(2.2.2)。

当步骤在单线程模式下运行时,它们都可以正常工作。但是当它们并行运行时,会导致错误,因为没有可用的Seam上下文。显然是因为创建了一个新的Thread并且没有启动Seam生命周期(Lifecycle.beginCall())。

如何在处理我的块时确保生命周期开始?我真的不想在我的读者中开始生命周期并在编写器中结束它,但它应该在tasklet执行时启动并在tasklet完成时结束。

1 个答案:

答案 0 :(得分:0)

在步骤执行之前启动上下文:

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