有一个@Aspect
可确保不会重新启动Spring Boot中的作业。它在JobLauncherSynchronizer.java中实现并记录在here中。它在spring批处理管理工件中。
我的工作是通过@Bean
创建的,如下所示:
@Bean
@Qualifier("synchronisationLauncher")
public JobLauncher synchronisationLauncher() throws Exception {
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(jobRepository);
jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
jobLauncher.afterPropertiesSet();
return jobLauncher;
}
我的问题:
如何将@Aspect
集成到JobLauncher中?
我天真地尝试更新它,并在上面的方法中设置了属性,不足为奇的是效果并不理想。我能够启动多个实例。
我的启动代码是:
// dependencies from constructor.
// the launcher is qualified to 'synchronisationLauncher'
private JobLauncher launcher;
private Job mergeJob;
private JobQueryHelper jobQueryHelper;
@PostMapping("uri")
public ResponseEntity<APIResponse> SynchroniseRetailers()
throws JobParametersInvalidException,
JobExecutionAlreadyRunningException,
JobRestartException,
JobInstanceAlreadyCompleteException {
JobParametersBuilder builder = new JobParametersBuilder()
.addLong("run.id", jobQueryHelper.findLastBatchExecutionIndex() + 1);
launcher.run(mergeJob, builder.toJobParameters());
return buildEmptyResponse();
}
答案 0 :(得分:0)
您可以做的是重写SimpleJobLauncher#run
方法并在运行作业之前添加仔细检查:
@Bean
public JobLauncher jobLauncher(JobRepository jobRepository, JobExplorer jobExplorer) {
SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher() {
@Override
public JobExecution run(Job job, JobParameters jobParameters) throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException {
Set<JobExecution> running = jobExplorer.findRunningJobExecutions(job.getName());
if (!running.isEmpty()) {
throw new JobExecutionAlreadyRunningException("An instance of this job is already active: " + job.getName());
}
return super.run(job, jobParameters);
}
};
simpleJobLauncher.setJobRepository(jobRepository);
simpleJobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
simpleJobLauncher.afterPropertiesSet();
return simpleJobLauncher;
}
请注意,这类似于您共享方面的代码,但不需要任何种类的AOP。正如我在评论中提到的那样,此检查已在super.run(job, jobParameters)
中完成,因此我们仅在此处添加了一个双重检查。
希望这会有所帮助。