首页 文章

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

提问于
浏览
1

我正在尝试并行处理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

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

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

相关问题