Spring-Batch:如何从StepListener返回自定义作业退出代码 [英] Spring-Batch: how do I return a custom Job exit code from a StepListener

查看:1279
本文介绍了Spring-Batch:如何从StepListener返回自定义作业退出代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

}

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

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

}

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

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

果然,执行到达if块,执行代码,仍然是我的作业完成,退出代码为COMPLETED,完全忽略我通过监听器设置的退出代码。

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.

在他们的文档中没有更多详细信息,我没有发现任何使用Google的东西有人可以告诉我如何以这种方式更改Job退出代码? Thanx

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

推荐答案

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

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

此代码带有 JobListener 适用于我

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

这篇关于Spring-Batch:如何从StepListener返回自定义作业退出代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆