【问题标题】:Spring-Batch: how do I return a custom Job exit code from a StepListenerSpring-Batch:如何从 StepListener 返回自定义作业退出代码
【发布时间】:2011-12-14 08:08:33
【问题描述】:

问题是这样的:我有一个一步一步的 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;
        }
    }

}

这很好用,当满足条件时,“if”块被执行并且作业以 FAILED 状态结束。但是请注意,我在这里返回的退出代码仍然是 Spring Batch 附带的标准退出代码之一。我想在某些时候返回我的个性化退出代码,例如“完成跳过”。现在我尝试更新上面的代码以返回:

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;
        }
    }

}

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

stepExecution.getJobExecution().setExitStatus("COMPLETED WITH SKIPS");

果然,执行到达“if”块,执行代码,但我的工作仍然以退出代码 COMPLETED 结束,完全忽略了我通过侦听器设置的退出代码。

他们的文档中没有更多详细信息,我也没有使用 Google 找到任何内容。有人可以告诉我如何以这种方式更改工作退出代码吗?谢谢

【问题讨论】:

    标签: java spring-batch


    【解决方案1】:

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

    这个带有JobListener 的代码对我有用

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

    【讨论】:

    • @MichaelLange 您能解释一下您在此处讨论的找到的替代方案(失败条件)吗:forum.springsource.org/…
    • @MichaelPralow 我知道你给出的答案已经很老了。但是按照@Shivan Dragon 的例子.. 我怎样才能在JobExecution 中获得stepExecution.getProcessorSkipCount()... 我的意思是.. 我需要检查StepExecution.. 你能帮我吗?
    猜你喜欢
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多