如何调用tasklet的特定方法

How to call a specific method of a tasklet

在作业上下文中有一个 'method' 参数,因此可以直接调用文档中所述的 tasklet 方法 "If the tasklet is specified as a bean definition, then a method can be specified and a POJO will be adapted to the Tasklet interface. The method suggested should have the same arguments as Tasklet.execute (or a subset), and have a compatible return type (boolean, void or RepeatStatus)." 我声明了一个 bean

    <step id="carregaStep" next="iniciaStep">
        <tasklet ref="atividadesTasklet" method="carregaAtividades"/>
    </step>

我声明了一个扩展 Tasklet 的 bean 并实现了一个方法:

public RepeatStatus carregaAtividades(StepContribution contribution, ChunkContext chunkContext) throws Exception 

但是没有调用这个方法。

我尝试在 google 中搜索一个使用示例,但是这很困难,因为 'method' 在一个常用词中并且 google 无法搜索 pontuation,例如搜索 "method=" 和 "tasklet"。有人可以给我一个使用tasklet方法的例子吗?

<bean id="atividadesTasklet" class="br.mypackage.AtividadesTasklet" scope="step" />



public class AtividadesTasklet implements Tasklet{

    public RepeatStatus carregarAtividades(StepContribution contribution, ChunkContext chunkContext) throws Exception {
        return RepeatStatus.FINISHED;   
    }


    @Override
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
        return RepeatStatus.FINISHED;
    }   
}


    <step id="carregaAtividadesStep" >
        <tasklet ref="atividadesTasklet" method="carregarAtividades">

        </tasklet>
    </step>

这似乎是一个错误。您使用的配置样式:

<tasket ref="myTasklet" method="myMethod"/>

是配置 MethodInvokingTaskletAdapter 的 shorthand 方式。但是,该适配器不传递参数。如果您的方法不带任何参数,它就可以工作。我已经记录问题 BATCH-2397 来跟踪这个。

话虽这么说,如果您愿意实现一个与 Tasklet#execute 的签名相匹配的方法,那么我建议您只实现 Tasklet 接口并在第一时间跳过此开销地点。