分区程序实现StepExecutionListener但未调用beforeStep

时间:2017-04-13 13:35:15

标签: java spring-batch partitioning

我有这个工作但是在运行它时,从不调用分区器中的beforeStep方法。我做错了什么?

@Configuration
public class SomeJob {

@Bean(name = "some-job")
public Job job() {
    return jobBuilderFactory.get(getName())
            .incrementer(new RunIdIncrementer())
            .start(stepOne())
            .next(stepTwo())
            .build();
}

private Step stepOne() {
    TaskletStep stepOne = stepBuilderFactory
            .get("step-one")
            .<...>chunk(CHUNK_SIZE)
            .reader(someReader())
            .writer(someWriter())
            .build();

    return stepBuilderFactory
            .get("partitionedStepOne")
            .partitioner(stepOne)
            .partitioner("partition", partitioner())
            .taskExecutor(someExecutor())
            .gridSize(SIZE)
            .build();
}


@Bean
@Scope(value = "step", proxyMode = ScopedProxyMode.TARGET_CLASS)
Partitioner partitioner() {
    return new OffsetBasedPartitioner(NAME, storedArticleMasterDataRepository);
}
} // end of job class
....

public class OffsetBasedPartitioner implements Partitioner, StepExecutionListener {

@Override
public void beforeStep(StepExecution stepExecution) {
  ...
}
...
}

如果可能,我会避免使用xml配置文件。

1 个答案:

答案 0 :(得分:0)

感谢@LucaBassoRicci提出的建议,我在步骤定义中将分区器添加为侦听器:

return stepBuilderFactory
        .get("partitionedStepOne")
        .partitioner(stepOne)
        .partitioner("partition", partitioner())
        .listener(partitioner())
        .taskExecutor(someExecutor())
        .gridSize(SIZE)
        .build();

并将所有其余部分保留如上,现在它可以工作:)