如何在春季批次中找到停止的作业执行?

时间:2017-04-19 08:29:45

标签: spring spring-batch

找到停止的作业执行以便在春季批次中重新启动它们的最佳做法是什么?

我们有一个读取大量文件并处理它们的应用程序,目前我已经配置了一个shutdownhook,当使用kill信号停止应用程序时,shutdownhook会停止作业执行。

我必须做的下一步是,在加载应用程序上下文后,我必须重新启动所有已停止的作业,所以我的问题是最好的方法是什么? 找到停止的工作执行是否有其他选择?

我现在的代码是:

public class JobRestarterLauncher implements ApplicationListener<ContextRefreshedEvent>{
private final Logger logger = Logger.getLogger(this.getClass().getName());
@Autowired
private JobRestarter jobRestarter;
@Autowired
private JobExecutionDao jobExecutionDaoJdbcImpl;

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
    logger.info("Restarting stopped jobs");
    List<Long> stoppedJobExecutionIds = jobExecutionDaoJdbcImpl.getStoppedJobExecutionIds();
    for (Long stoppedJobExecutionId : stoppedJobExecutionIds) {
        try
        {
            jobRestarter.restartJob(stoppedJobExecutionId);
        }catch(Exception exception){
            logger.error("Failed to start jobExectionId [" + stoppedJobExecutionId + "]", exception);
        }
    }
}

}

public class JobRestarter {
private final Logger logger = Logger.getLogger(this.getClass().getName());
@Autowired
private JobOperator jobOperator;

@Transactional
public void restartJob(Long executionId) {
    try {
        Long restartExecutionId = jobOperator.restart(executionId);
        logger.info("Restarted job with executionId [" + restartExecutionId + "]");
    } catch (Exception exception) {
        logger.error("Failed to restart job with executionId [" + executionId + "]", exception);
    }
}

}

jobExecutionDaoJdbcImpl使用下一个查询从spring批处理元数据表中找到最后一个作业执行ID:

query

0 个答案:

没有答案