简体   繁体   中英

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. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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