我想一个接一个地运行两个工作。是的,我确实在线搜索,但他们的解决方案涉及将第二个工作添加为第一个工作。但我有不同的要求。 我会在第一批作业执行完成后收到通知,第二批作业将在收到此通知后运行。 甚至可以在Spring启动中一个接一个地运行两个作业。 请帮忙!
答案 0 :(得分:0)
可能是最简单的解决方案是在FirstJob完成后为回调添加JobExecutionListener
。并在AfterJob
回调中启动第二个Job。由于您获得了FirstJob的executionContext,因此您可以根据Job应用作业运行策略。
public class KpJobListener implements JobExecutionListener {
private final static Logger LOG = LoggerFactory.getLogger(KpJobListener.class);
private Job job;
private JobLauncher jobLauncher;
public KpJobListener(final Job job, final JobLauncher jobLauncher) {
this.job = job;
this.jobLauncher = jobLauncher;
}
@Override
public void beforeJob(JobExecution jobExecution) {
LOG.info("Strarting job {}");
}
@Override
public void afterJob(JobExecution jobExecution) {
LOG.info("Job is completed job");
CompletableFuture.runAsync(()->{
try {
jobLauncher.run(job, new JobParametersBuilder().toJobParameters());
} catch (JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException
| JobParametersInvalidException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
}
}
示例Spring-batch.xml
<batch:job id="kpJob1" incrementer="jobIncrementer"
restartable="true">
<batch:listeners>
<batch:listener>
<bean class="com.kp.job.KpJobListener">
<constructor-arg name="job" ref="kpJob2" />
<constructor-arg name="jobLauncher" ref="jobLauncher" />
</bean>
</batch:listener>
</batch:listeners>
<batch:step id="kpJob1.step1" allow-start-if-complete="true">
<batch:tasklet>
<bean class="com.kp.job.step.task.KpTasklet" />
</batch:tasklet>
</batch:step>
</batch:job>
您可以针对消息传递服务增强此方法,这可能是整洁的设计。