为什么我的 Spring 批处理作业在完成后没有退出

Why is my Spring Batch job not exiting after completion

我的批处理作业配置如下

@Bean("MyJob")
    public Job umpInpatientCensusRptBatchJob(...) throws IOException {
        return jobBuilderFactory.get( "MyJob" )
                .incrementer( new RunIdIncrementer() )
                .start( Step0 ).on( COMPLETE ).end()
                .from( Step0 ).on( CONTINUE )
                .to( Step1 )
                .next( Step2 )
                .next( Step3 )
                .end()
                .build();
    }

其中步骤 0、1 和 3 是微线程。我的工作正在完成并显示消息 Job: [FlowJob: [name=MyJob]] completed with the following parameters。但是,它不会退出 - 它挂在那里。当我在 IntelliJ 上本地 运行 时,我必须手动退出工作。

我还没有实现任何异步。每个 tasklet 在完成时也会显式返回 FINISHED 状态。

一个明显的问题是第一个 on() 中的单词 "COMPLETE"。 on(String pattern) 方法被赋予 "COMPLETE" 作为参数,而不是例如 "COMPLETED",如果尚未创建适当的自定义退出状态,则作业以 FAILED 状态完成。但是,我不知道为什么它会挂起,而不仅仅是在您的情况下失败。 您的作业配置的以下版本似乎 运行 没问题,只要所有 tasklet return 一个 FINISHED RepeatStatus:

@Bean("MyJob")
public Job umpInpatientCensusRptBatchJob(...) throws IOException {
    return jobBuilderFactory.get( "MyJob" )
            .incrementer( new RunIdIncrementer() )
            .start( Step0 ).on( "COMPLETED" ).end()
            .from( Step0 ).on( "CONTINUE" )
            .to( Step1 )
            .next( Step2 )
            .next( Step3 )
            .end()
            .build();
}

如果 Step0 的 tasklet returns RepeatStatus.CONTINUABLE 但是,Step0 将永远重复,因为这是 RepeatStatus 的目的。所以,用这个配置是不可能到达Step1的。 为了决定是否应该 运行 接下来的步骤,而不是使用 tasklet 重复状态(我不知道这是否可能),您可以在 Step0 上使用 StepExecutionListener 或向流程添加决策程序,在第 0 步之后:

Spring-Batch: how do I return a custom Job exit STATUS from a StepListener to decide next step