弹簧批处理10秒后停工

问题描述 投票:1回答:1

我试图在后台运行一个作业,允许我通过一些条件或在超时后停止它。

我有两段代码。

一:

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

二:

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

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

public JobLauncher jobLauncher() {
    final SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
    jobLauncher.setJobRepository(jobRepository);
    final SimpleAsyncTaskExecutor simpleAsyncTaskExecutor = new SimpleAsyncTaskExecutor();
    jobLauncher.setTaskExecutor(simpleAsyncTaskExecutor);
    return jobLauncher;
}

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

在选项拖中,工作不运行我工作中的步骤。

请告知。

spring spring-batch
1个回答
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();
      }
    }
© www.soinside.com 2019 - 2024. All rights reserved.