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

一笑奈何 提交于 2019-11-30 00:22:39

问题


The issue is this: I have a Spring Batch job with a single step. 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:

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. Notice however that the exit code I return here is still amongst the standard ones that come with Spring Batch. I would like to return my personalized exit code such as "COMPLETED WITH SKIPS" at certain points. 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). 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.

There's no more details on this in their docs, and I haven't found anything using Google. Can someone plz tell me how do I go about changing the Job exit code in this fashion? Thanx


回答1:


looks like you just can't alter the BatchStatus, but you can try it with the exitstatus

this code with a JobListener works for me

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


来源:https://stackoverflow.com/questions/7879550/spring-batch-how-do-i-return-a-custom-job-exit-code-from-a-steplistener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!