gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 18:36:56 26 4
gpt4 key购买 nike

问题是这样的:我只有一个步骤就能完成一个Spring Batch作业。此步骤称为多次。如果每次调用它,一切正常(无异常),则作业状态为“已完成”。如果至少在执行步骤之一中发生了问题(抛出异常),我已经配置了一个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随附的标准代码中。我想在某些时候返回我的个性化退出代码,例如“ COMPLETED WITH SKIPS”。现在,我尝试更新上面的代码以返回:

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找到任何东西。有人可以告诉我如何以这种方式更改Job退出代码吗?谢谢

最佳答案

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

带有JobListener的代码对我有用

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

关于java - Spring-Batch:如何从StepListener返回自定义Job退出代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7879550/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com