Spring 季批量停止工作

如何在 Spring 季批量停止工作?我尝试使用以下代码使用此方法:

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但是这个方法不起作用,工作继续执行 . 有解决方案吗

回答(2)

2 years ago

您可以使用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) .

2 years ago

我假设您想以某个名字停止工作 . 这是代码 .

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()));