为什么在下面的代码中需要类型转换?

时间:2019-05-10 13:17:34

标签: casting spring-batch fault-tolerance

我要按以下步骤进行片段处理。

@Bean
@JobScope
public Step readStep(StepBuilderFactory sbf, 
ItemReader reader,ItemWriter writer, TaskExecutor taskExecutor, 
RefFileReaderComponentFactory componentFactory, String file){
        final Step step = sbf.get(file)
                             .chunk(100)
                             .reader(reader)
                             .faultTolerant()
                             .skipPolicy(new SkipPolicy()) //dummy skip policy for representation purpose
                             .writer(writer)
                             .faultTolerant()
                             .skipPolicy(new SkipPolicy())
                             .listener(new NewStepExecutionListener()) //This dummy listener is implementation of the StepExecutionListener
                             //.listener(new NewItemWrtieListener) //This dummy listener is implementation of the ItemWriteListener
                             .taskExecutor(taskExecutor)
                             .build();
        return  step;
}

但是上面的代码会引发编译时错误-The method taskExecutor(TaskExecutor) is undefined for the type StepBuilderHelper

有趣的是,仅当我放置NewStepExecutionListener侦听器时才会显示此错误,但是如果我注释掉它并取消注释NewItemWrtieListener则它会正常工作。

发布类型转换后,代码如下:

@Bean
@JobScope
public Step readStep(StepBuilderFactory sbf, 
ItemReader reader,ItemWriter writer, TaskExecutor taskExecutor, 
RefFileReaderComponentFactory componentFactory, String file){
        final Step step = ((AbstractTaskletStepBuilder<SimpleStepBuilder<Object, Object>>) sbf.get(file)
                             .chunk(100)
                             .reader(reader)
                             .faultTolerant()
                             .skipPolicy(new SkipPolicy()) //dummy skip policy for representation purpose
                             .writer(writer)
                             .faultTolerant()
                             .skipPolicy(new SkipPolicy())
                             .listener(new NewStepExecutionListener())) //This dummy listener is implementation of the StepExecutionListener
                             //.listener(new NewItemWrtieListener) //This dummy listener is implementation of the ItemWriteListener
                             .taskExecutor(taskExecutor)
                             .build();
        return  step;
}

我试图找出背后的原因,但无法理解为什么更改侦听器类型需要进行步骤处理的类型转换。

有人可以解释为什么吗?

1 个答案:

答案 0 :(得分:0)

方法调用的顺序很重要,因为某些方法可能会更改构建器类型。例如,当您调用faultTolerant()时,构建器类型从SimpleStepBuilder更改为FaultTolerantStepBuilder,添加了新方法来配置容错能力(跳过,重试等)。

因此,您需要检查每个方法的顺序和返回类型,并确保返回的构建器类型定义了您要调用的下一个方法。