简体   繁体   English

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

[英]Spring-Batch: how do I return a custom Job exit code from a StepListener

The issue is this: I have a Spring Batch job with a single step. 问题是这样的:我只有一个步骤就可以完成一个Spring Batch作业。 This step is called multiple times. 此步骤称为多次。 If every time it's called everything works ok (no Exceptions) the Job status is "COMPLETED". 如果每次都调用它,一切正常(无异常),则作业状态为“已完成”。 If something bad happends at least in one of the executions of the Step (an exception is thrown) I've configured a StepListener that changes the exit code to FAILED: 如果至少在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;
        }
    }

}

This works fine, when the condition is met the "if" block is exectued and the job finishes with status FAILED. 当满足条件时,将执行“ if”块,并且作业以FAILED状态结束,这可以正常工作。 Notice however that the exit code I return here is still amongst the standard ones that come with Spring Batch. 但是请注意,我在这里返回的退出代码仍在Spring Batch随附的标准代码中。 I would like to return my personalized exit code such as "COMPLETED WITH SKIPS" at certain points. 我想在某些时候返回我的个性化退出代码,例如“ COMPLETED WITH SKIPS”。 Now I've tried updating the above code to return just that: 现在,我尝试更新上述代码以返回:

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

}

as it is described in the docs: http://static.springsource.org/spring-batch/reference/html/configureStep.html (5.3.2.1. Batch Status vs. Exit Status). 如文档中所述: http ://static.springsource.org/spring-batch/reference/html/configureStep.html(5.3.2.1。批处理状态与退出状态)。 I've even tried 我什至尝试过

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

And sure enough, the execution arrives in the "if" block, executes the code, and still my job finishes with exit code COMPLETED, completly ignoring the exit code I've set via the listener. 可以肯定的是,执行到达“ if”块中,执行代码,但我的工作仍以退出代码COMPLETED完成,完全忽略了我通过侦听器设置的退出代码。

There's no more details on this in their docs, and I haven't found anything using Google. 他们的文档中没有关于此的更多详细信息,而且我还没有使用Google找到任何东西。 Can someone plz tell me how do I go about changing the Job exit code in this fashion? 有人可以告诉我如何以这种方式更改Job退出代码吗? Thanx 感谢名单

looks like you just can't alter the BatchStatus , but you can try it with the exitstatus 看起来您只是不能更改BatchStatus ,但是可以使用exitstatus尝试

this code with a JobListener works for me 此代码与JobListener对我有用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM