Java 如何在 spring 批处理应用程序中设置退出状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18063815/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:09:07  来源:igfitidea点击:

How to set exit status in spring batch application

javaspringspring-batch

提问by Kodaganti

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.

例如,我在作者的 write 方法中设置以下状态。

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.

尽管执行了上述语句,但我在日志中收到以下消息并且退出代码似乎是成功的。

"Batch Execution Successful!"

“批量执行成功!”

May I know how can I set the exit code properly?

我可以知道如何正确设置退出代码吗?

There are some ways to set after step.

有一些方法可以设置步骤后。

Like:

喜欢:

    @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.

但我不想坚持退出状态并在上述方法中设置退出状态。

回答by Sajal Dutta

You should properly intercept job execution-

您应该正确拦截作业执行-

public void afterJob(JobExecution jobExecution){

    if( jobExecution.getStatus() == BatchStatus.COMPLETED ){

        jobExecution.setExitStatus(new ExitStatus("CUSTOM SUCCESS STATUS"));

    } else if(jobExecution.getStatus() == BatchStatus.FAILED){

       jobExecution.setExitStatus(new ExitStatus("CUSTOM FAILURE STATUS"));

    }

}

回答by pegas

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.

我有一个类似的问题,并在 5 分钟前解决了它。我有多个步骤做“东西”和一个默认的“失败步骤”,当所有其他步骤抛出异常时调用。

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.

抛出异常的步骤将在 'spring-batch' 逻辑中被放弃,但我需要它们失败,以便我可以重新启动它们。因此,在尝试使用侦听器并强制设置状态后,我终于通过在调用“失败步骤”后更新已放弃的步骤来使其工作。

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

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 :

我的“batchTablesService”是我创建的一个服务类,它链接到一个 DAO,它只检索所有步骤,然后对于所有“放弃”步骤,它将它们更新为“失败”。像这样:

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

Good luck!

祝你好运!

回答by Ajay Kumar

No custom interfaces or classes are required. Take a look at this -

不需要自定义接口或类。看看这个 -

https://github.com/spring-projects/spring-boot/issues/14075

https://github.com/spring-projects/spring-boot/issues/14075