我跟随this sample使用Boot进行Spring Batch。
运行main方法时,将执行作业。
这样,我无法弄清楚如何控制作业的执行。例如,您如何安排作业,访问作业执行或设置作业参数的方式。
我试图注册自己的JobLauncher
@Bean
public JobLauncher jobLauncher(JobRepository jobRepo){
SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher();
simpleJobLauncher.setJobRepository(jobRepo);
return simpleJobLauncher;
}
但是当我尝试在主要方法中使用它时:
public static void main(String[] args) {
ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);
JobLauncher jobLauncher = ctx.getBean(JobLauncher.class);
//try catch removed for readability
jobLauncher.run(ctx.getBean(Job.class), new JobParameters());
}
加载上下文后,将再次执行该作业,并且尝试手动运行该作业时,我得到了
JobInstanceAlreadyCompleteException
。有没有办法防止自 Action 业执行?
最佳答案
通过设置可以防止作业执行
spring.batch.job.enabled=false
在application.properties中。或者,您可以使用
spring.batch.job.names
来接受将要运行的作业名称的逗号分隔列表。取自这里:how to stop spring batch scheduled jobs from running at first time when executing the code?