如何在春季批量停止工作?我尝试使用以下代码使用此方法:
public class jobListener implements JobExecutionListener{
@Override
public void beforeJob(JobExecution jobExecution) {
jobExecution.setExitStatus(ExitStatus.STOPPED);
}
@Override
public void afterJob(JobExecution jobExecution) {
// TODO Auto-generated method stub
}
}
我也试过COMPLETED,FAILED,但这种方法不起作用,工作继续执行。任何解决方案?
答案 0 :(得分:1)
您可以使用JobOperator和JobExplorer来停止工作以外的工作(请参阅https://docs.spring.io/spring-batch/reference/html/configureJob.html#JobOperator)。方法是stop(long executionId)
您必须使用JobExplorer来找到要停止的正确executionId。
同样,在作业流配置中,您可以根据退出状态将步骤执行后的作业配置为停止(参见https://docs.spring.io/spring-batch/trunk/reference/html/configureStep.html#stopElement)。
答案 1 :(得分:0)
我假设您要停止使用给定名称的工作。 这是代码。
String jobName = jobExecution.getJobInstance().getJobName(); // in most cases
DataSource dataSource = ... //@Autowire it in your code
JobOperator jobOperator = ... //@Autowire it in your code
JobExplorerFactoryBean factory = new JobExplorerFactoryBean();
factory.setDataSource(dataSource);
factory.setJdbcOperations(new JdbcTemplate(dataSource));
JobExplorer jobExplorer = factory.getObject();
Set<JobExecution> jobExecutions = jobExplorer.findRunningJobExecutions(jobName);
jobExecutions.forEach(jobExecution -> jobOperator.stop(jobExecution.getId()));