如何在Spring Batch分区步骤中动态分配步骤名称?

问题描述

我有一个由步骤A,B和C组成的Spring Batch作业。

步骤B和C合并为一个流程,我们称其为流程X。

步骤A是步骤管理器,其任务是执行分区以执行流X。

现在,从Spring Batch代码中我了解到,内置的StepExecutionSplitter将使用Partitioner提供的名称模板在每个流X执行中提供唯一的名称,例如:X:partition0,X:partition1,X:partition2等。

到目前为止很好,但是由于在我的情况下,流程X由步骤B和C组成,因此对于每个流程X的执行,B和C'的名称也必须是唯一的,因为否则,如果步骤失败,我们可能会得到错误的lastStepExecution

现在,我该如何将partitionName从步骤A /流X延续到步骤B和C,从而为B和C产生相似的命名:C:parition0,B:partition2等?

我尝试将分区名称作为步骤A分区程序返回的Map 的一部分,然后在步骤B和C声明中访问它,如下所示:

 @Bean
 @StepScope
 public Step stepB( @Value("#{stepExecutionContext[partitionName]}") String partitionName,tasklet sometasklet) {
return this.stepBuilderFactory.get("stepB" + partitionName)
    .tasklet(sometasklet).build();

}

显然,由于此问题,以上操作无效:

https://github.com/spring-projects/spring-batch/issues/1335

尝试了以上链接中的解决方案,但无济于事。

再次,我只是将分区命名“继承”到所有从属步骤名称方法

我确实认为该行为应该是认行为,因为我们必须能够区分分区环境中每个从属步骤的执行情况

这是我的配置:

 @Bean(name = "sampleJob")
 public Job sampleJob(Step manager) {
    return this.jobBuilderFactory.get("sampleJob")
        .start(manager).build();
  }

@Bean
Public Step manager(Partitioner somePartitioner,Step mainStep){
    Return stepBuilderFactory.get("manager").partitioner(somePartitioner,mainStep.getName()).step(mainStep).build();

@Bean
public Step mainStep(Step slave1,Step slave2){
    Flow someFlow = new flowBuilder<simpleFlow>().add(slave1).next(slave2).build();
   Return stepBuilderFactory.get("mainStep").add(someFlow).build();
}

@Bean
Public Step slave1(){
   Return stepBuilderFactory.get(/* Need to correctly include the partition name also here*/)
   // Reader,processor,writer config,etc
)

@Bean
Public Step slave2(){
   Return stepBuilderFactory.get(/* Need to correctly include the partition name also here*/)
   // Reader,etc
)

解决方法

默认的SimpleStepExecutionSplitter似乎不适合您的用例。

因此,您需要自定义PartitionNameProvider或(如果这对您不起作用)自定义StepExecutionSplitter