我正在使用 Spring Batch 5。如果该批处理已经在运行,我需要再次停止运行该批处理。我试过如下
作业配置:
@Component
@RequiredArgsConstructor
@Slf4j
public class RtgsJob {
private final ProcessorStep1 batchProcessorStep1;
private final WriterStep1 batchWriterStep1;
private final PlatformTransactionManager transactionManager;
private final JobRepository jobRepository;
public Job createEft(){
return new JobBuilder("RTGS-STEP1", jobRepository)
.incrementer(new RunIdIncrementer())
.start(step1())
.build();
}
private Step step1(){
return new StepBuilder("step1", jobRepository)
.<input, output> chunk(10, transactionManager)
.reader(new ReaderStep1())
.processor(batchProcessorStep1)
.writer(batchWriterStep1)
.build();
}
}
作业计划运行作业:
@Component
@Slf4j
public class ScheduledJobBean {
@Autowired
JobLauncher jobLauncher;
@Autowired
RtgsJob eftJob;
@Autowired
private JobRepository jobRepository;
@Scheduled(cron = "*/30* * * * *")
public void perform() throws Exception
{
JobExecution lastJobExecutionStep1 = jobRepository.getLastJobExecution("RTGS-STEP1", new JobParametersBuilder().toJobParameters());
if (lastJobExecutionStep1 == null || lastJobExecutionStep1.getStatus().isLessThan(BatchStatus.STARTING)) {
log.info("{} batch step1 {} executionTransaction {} Started ");
try {
JobParameters jobParameters2 = new JobParametersBuilder()
.addLong("startAtRTGSStep1", System.currentTimeMillis())
.toJobParameters();
executionTransaction = jobLauncher.run(eftJob.createEft(),jobParameters2);
} catch (JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException |
JobParametersInvalidException ex) {
log.error("{} rtgs batch {} Step1 {} executionTransaction {} JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException | JobParametersInvalidException message: "+ex.getMessage());
ex.printStackTrace();
}catch (Exception ex) {
log.error("{} rtgs batch {} Step1 executionTransaction {} error: "+ex.getMessage());
ex.printStackTrace();
log.error(ex.getMessage());
}
}else{
log.info("{} rtgs batch step1 {} execution {} isRunning ");
}
}
}
我的工作在 30 秒后开始。如果一项作业正在运行,则在完成之前它不会再次启动。这意味着如果数据处理需要超过 30 秒,它将不会重新开始。按下完成后将在30秒内再次启动
但是如果一批已经在运行,上述配置批次会在 30 秒内再次启动。
我的配置有什么问题?
请帮助我
这是因为每次作业都使用不同的作业参数(即当前时间)启动,并且使用
jobRepository.getLastJobExecution("RTGS-STEP1", new JobParametersBuilder().toJobParameters())
检索上次作业执行的方式使用不同的参数集。
发生的情况是,您每次都创建一个新的作业实例,并且从不检查过去的作业实例当前是否正在运行。对您的情况有帮助的可能是
JobExplorer#findRunningJobExecutions
。