【问题标题】:Spring Batch parallel Seam context, no application context activeSpring Batch并行Seam上下文,没有应用程序上下文活动
【发布时间】:2014-01-30 15:48:26
【问题描述】:

我正在尝试并行处理 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 完成时结束。

【问题讨论】:

    标签: java parallel-processing spring-batch seam2


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-28
      • 1970-01-01
      • 2012-07-19
      • 2012-05-18
      • 2011-11-10
      • 2011-05-16
      • 2012-05-07
      • 2023-02-08
      相关资源
      最近更新 更多