如何在spring批处理应用程序中设置退出状态 [英] How to set exit status in spring batch application
问题描述
我正在编写一个批处理应用程序,当我的应用程序逻辑识别问题并返回时,我想立即设置退出状态。
I am writing a batch application in which I would like to set the exit status immediately when ever my application logic identify an issue and return.
例如我是在我的编写者的写方法中设置以下状态。
For example I am setting the below status in my writer's write method.
stepExecution.getJobExecution().setExitStatus(ExitStatus.FAILED);
尽管执行了上述法规,但我在日志中得到的信息低于退出代码似乎是成功。
eventhough the above statment is executed, am getting below message in logs and the exit code is seems to be success.
批量执行成功!
我是否知道如何正确设置退出代码?
May I know how can I set the exit code properly?
有一些方法可以在步骤之后设置。
There are some ways to set after step.
喜欢:
@AfterStep
public ExitStatus afterStep(){
//Test condition
return new ExistStatus("CUSTOM EXIT STATUS");
}
但我不想坚持退出站点并设置退出状态在上面的方法中。
But I don't want to persist the exit stauts and set the exit status in the above method.
推荐答案
我有一个类似的问题,并在5分钟前解决了。
我有多个步骤做'stuff'和默认的失败步骤,当所有其他步骤抛出异常时调用。
I had a similar issue and just solved it 5 minutes ago. I have multiple steps doing 'stuff' and a default "failure step" that is called when all other steps throw an exception.
抛出一个步骤异常将被放弃在'spring-batch'逻辑中,但我需要它们失败才能重新启动它们。
因此,在尝试使用侦听器和强制状态后,我终于通过在调用失败步骤之后更新已放弃的步骤来使其工作。
The steps that throw an exception will be Abandoned in 'spring-batch' logic, but I need them to be Failed so that I can restart them. So, after trying with listeners and forcing statuses, I finally got it to work by updating the abandoned step(s) after the 'failure step' is called.
所以我的'失败步骤'将如下所示:
So my 'failure-step' will look like this :
public class EnleveDossiersRejetesEtMAJSteps implements Tasklet {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
/**some business logic code for my step, AND the code bellow*/
if (chunkContext.getStepContext() != null && chunkContext.getStepContext().getStepExecution() != null) {
Long jobExecutionId = chunkContext.getStepContext().getStepExecution().getJobExecutionId();
batchTablesService.updateJobStepStatuses(jobExecutionId, BatchStatus.ABANDONED, BatchStatus.FAILED);
}
/** end the step like expected */
return RepeatStatus.FINISHED;
}
}
我的'batchTablesService'是我创建的服务类,链接到简单检索所有步骤的DAO,然后对于所有放弃步骤,它将它们更新为失败。
如下所示:
My 'batchTablesService' is a Service class that I created, which links to a DAO that simply retrieves all steps, then for all 'Abandoned' steps, it updates them to 'Failed'. Like so :
@Override
public void updateJobStepStatuses(Long jobExecutionId, BatchStatus sInitial, BatchStatus sFinal) {
log.debug("-- call updateJobStepStatuses(" + jobExecutionId + "," + sInitial + "," + sFinal + ")");
List<BatchStepExecution> steps = getStepExecutions((int) (long) jobExecutionId, null);
for (BatchStepExecution step : steps) {
if (!step.getStatus().isEmpty() && step.getStatus().equalsIgnoreCase(sInitial.toString())) {
step.setStatus(sFinal.toString());
entityManager.merge(step);
}
}
}
祝你好运!
这篇关于如何在spring批处理应用程序中设置退出状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!