Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring批处理:在afterJob中获取exitStatusDescription_Java_Spring_Spring Batch - Fatal编程技术网

Java Spring批处理:在afterJob中获取exitStatusDescription

Java Spring批处理:在afterJob中获取exitStatusDescription,java,spring,spring-batch,Java,Spring,Spring Batch,我是新来的。 我已使用此作业配置了一个新项目: @Configuration @EnableBatchProcessing @Log4j2 public class BatchConfiguration { /* ... */ @Bean public Job myJob(JobBuilderFactory jobBuilderFactory, Step stepInit, Step stepMain, Step stepReport) { retur

我是新来的。 我已使用此作业配置了一个新项目:

@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中,我使用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;
        }

        /* ... */
    }

}
如何在实现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() -> ""
    }

}

将步骤的退出状态设置为
失败
并返回
重复状态。已完成
对我来说没有意义。如果tasklet失败,我将设置其退出状态并抛出异常(另一个选项是使用异常消息并将其设置为
ERROR DESC

现在,如果要在侦听器中获取异常,可以执行以下操作:

@Override
public void afterJob(JobExecution jobExecution) {
    List<Throwable> allExceptions = jobExecution.getAllFailureExceptions();
    // get the exception and its message
}
@Override
public void afterJob(JobExecution jobExecution) {
    List<StepExecution> stepExecutions = jobExecution.getStepExectuions();
    // iterate and get the step execution  you want then get its exit status
}
@覆盖
公共无效后作业(作业执行作业执行){
List allExceptions=jobExecution.getAllFailureExceptions();
//获取异常及其消息
}
如果要获取特定步骤的存在状态,可以执行以下操作:

@Override
public void afterJob(JobExecution jobExecution) {
    List<Throwable> allExceptions = jobExecution.getAllFailureExceptions();
    // get the exception and its message
}
@Override
public void afterJob(JobExecution jobExecution) {
    List<StepExecution> stepExecutions = jobExecution.getStepExectuions();
    // iterate and get the step execution  you want then get its exit status
}
@覆盖
公共无效后作业(作业执行作业执行){
List stepExecutions=jobExecution.getStepExecutions();
//迭代并获取所需的步骤执行,然后获取其退出状态
}

你是对的,但是如果我抛出新异常,我仍然必须设置“chunkContext.getStepContext().getStepExecution().getJobExecution().setExitStatus”值来恢复afterJob方法中的错误描述,对吗?哦,等等,我想我误解了你最初的问题。。您已将步骤的退出状态设置为
ERROR DESC
,但您正在尝试从此处的作业执行中获取它
//jobsecution.getExitStatus().getExitDescription()->“
”。您确定要从作业执行而不是步骤执行中获取它吗?例如:
jobExecution.getStepExecutions()
,然后迭代步骤执行以查找
stepInit
的步骤,然后执行
stepExecution.getExitStatus.getExitDescription()
?这应该行得通。如果这是您正在寻找的,我将更新答案。对不起,我向您描述我的问题:如果在批处理执行期间(在任何步骤中)出现特殊情况,我需要停止执行,将状态设置为“失败”,并设置说明。哪一个是最好的解决方案?