如果前一个作业仍然是 运行,如何防止在 spring 批次上执行预定作业?
How to prevent the job execution of a scheduled job on spring batch if the previous job is still running?
我正在尝试模拟在 reader 中执行 Thread.sleep() 的场景,但即使当前 运行 作业未完成,也会提交另一个作业线程池,我该如何防止这种情况发生?
@Scheduled(fixedRate = 100)
public void executeChunkJob() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
executeJob(chunkJob);
}
private void executeJob(Job jobToRun) throws JobParametersInvalidException, JobExecutionAlreadyRunningException,
JobRestartException, JobInstanceAlreadyCompleteException{
try {
JobParameters parameters = new JobParametersBuilder()
.addString("JobID", String.valueOf(System.currentTimeMillis())).toJobParameters();
JobExecution jobExecution = jobLauncher().run(jobToRun, parameters);
System.out.println("JOb Execution" + jobExecution.getExitStatus());
} catch(JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException | JobParametersInvalidException e) {
throw e;
} catch(OutOfMemoryError | Exception e) {
}
}
@Bean
public JobLauncher jobLauncher() throws Exception
{
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(jobRepository);
jobLauncher.setTaskExecutor(getCustomTaskExecutor());
jobLauncher.afterPropertiesSet();
return jobLauncher;
}
@Bean(name= "myExecutor")
public TaskExecutor getCustomTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(100);
executor.setQueueCapacity(100);
executor.initialize();
return executor;
}```
如果您以相同的工作名称开始工作,您可以找到工作的 运行 条件。
根据运行条件,你可以建立你的系统是否开始新的工作。
我们需要自动装配 JobExplorer,
@Autowired
JobExplorer jobExplorer;
方法片段供您参考,
for (JobExecution jobExecution : jobExecutionsSet) {
if (jobExecution.getStatus() != BatchStatus.STARTED || jobExecution.getStatus() != BatchStatus.STARTING) {
// start the new job
} else {
// prevent it from starting
}
}
我正在尝试模拟在 reader 中执行 Thread.sleep() 的场景,但即使当前 运行 作业未完成,也会提交另一个作业线程池,我该如何防止这种情况发生?
@Scheduled(fixedRate = 100)
public void executeChunkJob() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
executeJob(chunkJob);
}
private void executeJob(Job jobToRun) throws JobParametersInvalidException, JobExecutionAlreadyRunningException,
JobRestartException, JobInstanceAlreadyCompleteException{
try {
JobParameters parameters = new JobParametersBuilder()
.addString("JobID", String.valueOf(System.currentTimeMillis())).toJobParameters();
JobExecution jobExecution = jobLauncher().run(jobToRun, parameters);
System.out.println("JOb Execution" + jobExecution.getExitStatus());
} catch(JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException | JobParametersInvalidException e) {
throw e;
} catch(OutOfMemoryError | Exception e) {
}
}
@Bean
public JobLauncher jobLauncher() throws Exception
{
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(jobRepository);
jobLauncher.setTaskExecutor(getCustomTaskExecutor());
jobLauncher.afterPropertiesSet();
return jobLauncher;
}
@Bean(name= "myExecutor")
public TaskExecutor getCustomTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(100);
executor.setQueueCapacity(100);
executor.initialize();
return executor;
}```
如果您以相同的工作名称开始工作,您可以找到工作的 运行 条件。
根据运行条件,你可以建立你的系统是否开始新的工作。
我们需要自动装配 JobExplorer,
@Autowired
JobExplorer jobExplorer;
方法片段供您参考,
for (JobExecution jobExecution : jobExecutionsSet) {
if (jobExecution.getStatus() != BatchStatus.STARTED || jobExecution.getStatus() != BatchStatus.STARTING) {
// start the new job
} else {
// prevent it from starting
}
}