java Spring-Batch:如何从 StepListener 返回自定义作业退出代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7879550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring-Batch: how do I return a custom Job exit code from a StepListener
提问by Shivan Dragon
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:
问题是这样的:我有一个只需一步的 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;
}
}
}
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:
这工作正常,当满足条件时,“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;
}
}
}
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
正如文档中所述:http://static.springsource.org/spring-batch/reference/html/configureStep.html(5.3.2.1。批次状态与退出状态)。我什至试过
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.
果然,执行到达“if”块,执行代码,我的工作仍然以退出代码完成,完全忽略我通过侦听器设置的退出代码。
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
他们的文档中没有关于此的更多详细信息,我还没有使用 Google 找到任何内容。有人可以告诉我如何以这种方式更改工作退出代码吗?谢谢
回答by Michael Pralow
looks like you just can't alter the BatchStatus, but you can try it with the exitstatus
看起来你不能改变BatchStatus,但你可以用exitstatus 试试
this code with a JobListenerworks for me
这段带有JobListener 的代码对我有用
// JobListener with interface or annotation
public void afterJob(JobExecution jobExecution) {
jobExecution.setExitStatus(new ExitStatus("foo", "fooBar"));
}