我在Spring Batch中同时执行多个作业时遇到问题。一次只能执行一个作业,而其他作业要等到当前正在运行的作业才能完成执行。
用例:
Job_1 -> should run every second
Job_2 -> should run every 5 seconds and only one instance of the job should run at a time.
Both Job 1 & 2 will run like a loop and will not complete.( null won't be returned from the reader)
问题: 这两个作业没有并行运行,并且只有JOB_1一直在运行。 JOB_2等待JOB_1完成,然后开始执行。
JOB配置:
带有SimpleAsyncTaskExecutor()的JOB_1:
@Scheduled(fixedDelay = 1000L)
public void Job1Scheduler() {
JobParameters jobParameters = new JobParametersBuilder().addLong("dumyJob1", System.currentTimeMillis())
.toJobParameters();
try {
Job1Launcher.run(JobOne, jobParameters);
} catch (Exception e) {
e.printStackTrace();
}
}
@Bean(name = "Job1Launcher")
public JobLauncher jobLauncher1(JobRepository jobRepository) throws Exception {
SimpleJobLauncher jobLauncher1 = new SimpleJobLauncher();
jobLauncher1.setJobRepository(jobRepository);
jobLauncher1.setTaskExecutor(new SimpleAsyncTaskExecutor());
jobLauncher1.afterPropertiesSet();
return jobLauncher1;
}
@Bean
public Step dummyJob1Step() {
return stepBuilderFactory.get("dummyJob1Step")
.<String, String> chunk(1)
.reader(DummyReader())
.writer(DummyWriter())
.build();
}
@Bean
public Job JobOne() {
return jobBuilderFactory.get("JobOne").incrementer(new RunIdIncrementer())
.flow(dummyJob1Step())
.end()
.build();
}
@Bean
public ItemWriter<? super String> DummyWriter() {
// TODO Auto-generated method stub
return new DummyWriter();
}
@Bean
public ItemReader<? extends String> DummyReader() {
// TODO Auto-generated method stub
return new DummyReader();
}
带有SyncTaskExecutor()的JOB_2:
@Scheduled(fixedDelay = 5000L)
public void Job2Scheduler() {
JobParameters jobParameters = new JobParametersBuilder().addLong("dumyJob2", System.currentTimeMillis())
.toJobParameters();
try {
Job2Launcher.run(JobTwo, jobParameters);
} catch (Exception e) {
e.printStackTrace();
}
}
@Bean(name = "Job2Launcher")
public JobLauncher jobLauncher2(JobRepository jobRepository) throws Exception {
SimpleJobLauncher jobLauncher2 = new SimpleJobLauncher();
jobLauncher2.setJobRepository(jobRepository);
jobLauncher2.setTaskExecutor(new SyncTaskExecutor()); // Single JOB_2 should run at a time
jobLauncher2.afterPropertiesSet();
return jobLauncher2;
}
**Job 2's step & flow configuration will be same as Job 1**
请让我知道在配置中同时执行两个作业时错过了什么?
谢谢。