小编典典

Spring Boot如何运行批处理作业

spring-boot

运行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当我尝试手动运行它时,我得到了。有没有办法防止自动作业执行?


阅读 361

收藏
2020-05-30

共1个答案

小编典典

通过设置可以防止作业执行

spring.batch.job.enabled=false

在application.properties中。或者,您可以使用spring.batch.job.names它以逗号分隔的要运行的作业名称列表。

2020-05-30