如果原始作业失败,则执行单独的弹簧批作业

时间:2016-09-20 16:59:58

标签: java spring spring-batch

我有一个主要的弹簧批处理作业,它有多个步骤。如果由于某种原因该作业失败,我需要调用另一个作业来更新数据库中的表以及失败结果。

如何配置我的批处理流程呢?

我创建了一个类似的JobExecution,但我不知道如何从if / else语句中调用新作业:

@Bean 
    public JobExecutionListener dataListener() {
        JobCompletionNotificationListener dataNotificationListener = new JobCompletionNotificationListener(new JdbcTemplate(dataDataSource));
        dataNotificationListener.afterJob(jobExecution());
        return dataNotificationListener;
    }


@Bean
public JobExecution jobExecution() {

        JobExecution jobExecution = new JobExecution(jobExecution());

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

            // If job completed successfully, update current job log to success


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

            // If job failed, update current job log to failure

        }

        return jobExecution;
    }

1 个答案:

答案 0 :(得分:1)

您需要在作业中添加一个实现JobExecutionListener接口的侦听器类,并在检查条件public void afterJob(JobExecution jobExecution)后调用jobExecution.getStatus() == BatchStatus.FAILED方法中的逻辑。