[英]Spring-Batch : How do I return custom exit code on invalid record with exception in spring batch
[英]Spring-Batch: how do I return a custom Job exit code from a StepListener
问题是这样的:我只有一个步骤就可以完成一个Spring Batch作业。 此步骤称为多次。 如果每次都调用它,一切正常(无异常),则作业状态为“已完成”。 如果至少在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;
}
}
}
当满足条件时,将执行“ 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"));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.