Spring批处理:在afterJob中获取exitStatusDescription

huangapple go评论71阅读模式
英文:

Spring batch: get exitStatusDescription in afterJob

问题

我对Spring Batch还不熟悉
我已经使用这个作业配置了一个新项目

@Configuration
@EnableBatchProcessing
@Log4j2
public class BatchConfiguration {

    /* ... */

    @Bean
    public Job myJob(JobBuilderFactory jobBuilderFactory, Step stepInit, Step stepMain, Step stepReport) {
        return jobBuilderFactory.get("myJob").incrementer(new RunIdIncrementer()).start(stepInit).on(ExitStatus.FAILED.getExitCode()).fail().from(stepInit).on("*").to(stepMain).next(stepReport).end().listener(new BatchListener()).build();
    }

    @Bean
    public Step stepInit(StepBuilderFactory stepBuilderFactory) {
        return stepBuilderFactory.get("stepInit").tasklet(BatchInitialize.builder().build()).build();
    }

    /* ... */
}

在BatchInitialize的tasklet中我已经定义了一个带有错误描述的退出状态

@Builder
@AllArgsConstructor
@Log4j2
public class BatchInitialize implements Tasklet {

    @Override
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
        /* ... */

        if (CONDITION) {
            contribution.setExitStatus(new ExitStatus(ExitStatus.FAILED.getExitCode(), "ERROR DESC"));
            return RepeatStatus.FINISHED;
        }

        /* ... */
    }

}

在实现JobExecutionListener的类中我如何获取错误描述

@Builder
@AllArgsConstructor
@Log4j2
public class BatchListener implements JobExecutionListener {

    @Override
    public void beforeJob(JobExecution jobExecution) {
        //
    }

    @Override
    public void afterJob(JobExecution jobExecution) {
        // jobExecution.getExitStatus().getExitCode() -> FAILED
        // jobExecution.getExitStatus().getExitDescription() -> ""
    }
}
英文:

I am new to Spring batch.
I've configurated a new project with this job:

@Configuration
@EnableBatchProcessing
@Log4j2
public class BatchConfiguration {
/* ... */
@Bean
public Job myJob(JobBuilderFactory jobBuilderFactory, Step stepInit, Step stepMain, Step stepReport) {
return jobBuilderFactory.get("myJob").incrementer(new RunIdIncrementer()).start(stepInit).on(ExitStatus.FAILED.getExitCode()).fail().from(stepInit).on("*").to(stepMain).next(stepReport).end().listener(new BatchListener()).build();
}
@Bean
public Step stepInit(StepBuilderFactory stepBuilderFactory) {
return stepBuilderFactory.get("stepInit").tasklet(BatchInitialize.builder().build()).build();
}
/* ... */

}

In BatchInitialize tasklet I've defined an exit status with errorDesc:

@Builder
@AllArgsConstructor
@Log4j2
public class BatchInitialize implements Tasklet {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
/* ... */
if(CONDITION) {
contribution.setExitStatus(new ExitStatus(ExitStatus.FAILED.getExitCode(), "ERROR DESC"));
return RepeatStatus.FINISHED;
}
/* ... */
}
}

How can I get the error description in a class that implements JobExecutionListener?

@Builder
@AllArgsConstructor
@Log4j2
public class BatchListener implements JobExecutionListener {
@Override
public void beforeJob(JobExecution jobExecution) {
//
}
@Override
public void afterJob(JobExecution jobExecution) {
// jobExecution.getExitStatus().getExitCode() -> FAILED
// jobExecution.getExitStatus().getExitDescription() -> ""
}
}

答案1

得分: 2

将步骤的退出状态设置为FAILED并返回RepeatStatus.FINISHED对我来说没有意义。如果任务失败,我会设置其退出状态并抛出异常(另一种选项是使用异常的消息并将其设置为ERROR DESC)。

现在,如果您想在监听器中获取异常,可以这样做:

@Override
public void afterJob(JobExecution jobExecution) {
    List<Throwable> allExceptions = jobExecution.getAllFailureExceptions();
    // 获取异常及其消息
}

如果您想获取特定步骤的退出状态,可以这样做:

@Override
public void afterJob(JobExecution jobExecution) {
    List<StepExecution> stepExecutions = jobExecution.getStepExectuions();
    // 遍历并获取您想要的步骤执行,然后获取其退出状态
}
英文:

Setting the exit status of the step to FAILED and returning RepeatStatus.FINISHED does not make sense to me. If the tasklet failed, I would set its exit status and throw an exception instead (another option is to use the exception's message and set it to ERROR DESC).

Now if you want to get the exception in the listener, you can do something like:

@Override
public void afterJob(JobExecution jobExecution) {
List&lt;Throwable&gt; allExceptions = jobExecution.getAllFailureExceptions();
// get the exception and its message
}

If you want to get the exist status of a particular step, you can do something like:

@Override
public void afterJob(JobExecution jobExecution) {
List&lt;StepExecution&gt; stepExecutions = jobExecution.getStepExectuions();
// iterate and get the step execution  you want then get its exit status
}

huangapple
  • 本文由 发表于 2020年9月16日 17:43:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63917355.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定