春季批次在10秒后停止工作

时间:2016-12-29 08:24:41

标签: spring spring-batch

我正在尝试在后台运行一个作业,允许我在某种情况下或在超时发生后停止它。

我有两个代码块:

一:

php artisan migrate

二:

@ContextConfiguration(classes={EmbeddedISpringBatchConfiguration.class, MonitoredJobConfig.class})
@RunWith(SpringJUnit4ClassRunner.class)
public class RunMonitoredJob2 {

@Autowired
private JobLauncher jobLauncher;

@Autowired
private JobRepository jobRepository;

@Autowired
private Job job;

@Test
public void testLaunchJob() throws Exception {

    JobExecution execution = jobLauncher.run(job, new JobParameters());

    Thread.sleep(10000);

    execution.stop();
}

在选项一中,工作首先完成,然后才进入"睡眠"

在选项栏中,作业不会执行我的工作中的步骤。

请告知。

1 个答案:

答案 0 :(得分:0)

尝试使用调度程序,该调度程序将在特定的时间间隔后调用,而不是Thread.sleep

@Scheduled(fixedDelayString = "${cron.batch-timeout-check-delay}", initialDelayString = "${cron.batch-timeout-check-initial-delay}")
    public void execute() throws Exception{
      //get jobExceutionId of job to stop
      JobExceution jobExecution = jobExplorer.getJobExceution(jobExceutionId);
      boolean isTimeout = (new Date().getTime() - jobExecution.getCreateTime().getTime()) > 50000 //Here timout
      if(isTimeout){
        jobexceution.stop();
      }
    }