Spring-Batch:如何从StepListener返回自定义作业退出代码

Shi*_*gon 2 java spring-batch

问题是这样的:我有一个Spring Batch作业.此步骤被多次调用.如果每次调用它一切正常(没有异常),则作业状态为"已完成".如果至少在Step的一个执行中发生了一些不好的事情(抛出异常),我已经配置了一个StepListener,它将退出代码更改为FAILED:

public class SkipCheckingListener extends StepExecutionListenerSupport {

    public ExitStatus afterStep(StepExecution stepExecution) {
        String exitCode = stepExecution.getExitStatus().getExitCode();
        if (stepExecution.getProcessorSkipCount() > 0) {
            return new ExitStatus(ExitStatus.FAILED);
        }
        else {
            return null;
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

这样可以正常工作,当满足条件时,"if"块被激活并且作业以状态FAILED结束.但请注意,我返回的退出代码仍然是Spring Batch附带的标准代码.我想在某些时候返回我的个性化退出代码,例如"已完成SKIPS".现在我已经尝试更新上面的代码来返回:

public class SkipCheckingListener extends StepExecutionListenerSupport {

    public ExitStatus afterStep(StepExecution stepExecution) {
        String exitCode = stepExecution.getExitStatus().getExitCode();
        if (stepExecution.getProcessorSkipCount() > 0) {
            return new ExitStatus("COMPLETED WITH SKIPS");
        }
        else {
            return null;
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

正如文档中所述:http://static.springsource.org/spring-batch/reference/html/configureStep.html(5.3.2.1.批处理状态与退出状态).我甚至试过了

stepExecution.getJobExecution().setExitStatus("COMPLETED WITH SKIPS");
Run Code Online (Sandbox Code Playgroud)

果然,执行到达"if"块,执行代码,然后我的工作仍以退出代码COMPLETED结束,完全忽略我通过监听器设置的退出代码.

在他们的文档中没有更多关于此的详细信息,我还没有找到任何使用Google的内容.有人可以告诉我如何以这种方式更改Job退出代码?感谢名单

Mic*_*low 7

看起来你无法改变BatchStatus,但你可以尝试使用exitstatus

带有JobListener的代码对我有用

// JobListener with interface or annotation
public void afterJob(JobExecution jobExecution) {
    jobExecution.setExitStatus(new ExitStatus("foo", "fooBar"));
}
Run Code Online (Sandbox Code Playgroud)