我跟着this sample进行了Spring Batch with Boot .
运行main方法时,将执行作业 . 这样我无法弄清楚如何控制作业执行 . 例如,您如何安排作业,或访问作业执行或设置作业参数 .
我试着注册我自己的JobLauncher
@Bean
public JobLauncher jobLauncher(JobRepository jobRepo){
SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher();
simpleJobLauncher.setJobRepository(jobRepo);
return simpleJobLauncher;
}
但是当我尝试在main方法中使用它时:
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
. 有没有办法阻止自动执行作业?
2 回答
可以通过设置来防止作业执行
在application.properties中 . 或者您可以使用
spring.batch.job.names
它将以逗号分隔的作业名称列表进行运行 .取自这里:how to stop spring batch scheduled jobs from running at first time when executing the code?
您可以使用rest controller POST启用Job的执行: