Spring Boot如何运行批处理作业 [英] How Spring Boot run batch jobs
问题描述
我跟着此示例 for Spring Batch with Boot。
运行main方法时,作业是执行。
这种方式我无法弄清楚如何控制作业执行。例如,您如何安排作业,或访问作业执行或设置作业参数。
我试图注册我自己的JobLauncher
@Bean
public JobLauncher jobLauncher(JobRepository jobRepo){
SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher();
simpleJobLauncher.setJobRepository(jobRepo);
返回simpleJobLauncher;
}
但是当我尝试在main方法中使用它时:
公共静态无效的主要(字串[] args){
ConfigurableApplicationContext CTX = SpringApplication.run(Application.class,参数);
JobLauncher jobLauncher = ctx.getBean(JobLauncher.class);
//为了便于阅读,请删除try catch
jobLauncher.run(ctx.getBean(Job.class),new JobParameters());
}
加载上下文后我再次执行作业 JobInstanceAlreadyCompleteException
当我尝试手动运行它时。
有没有办法防止自动执行作业?
可以通过设置来阻止作业执行
spring.batch.job.enabled = false
$ application.properties中的b
$ b
。或者您可以使用 spring.batch.job.names
它将以逗号分隔的作业名称列表进行运行。
'p>从这里摘自:如何在执行代码时第一次停止弹出批量预定作业?
I followed this sample for Spring Batch with Boot.
When you run the main method the job is executed. This way I can't figure out how one can control the job execution. For example how you schedule a job, or get access to the job execution, or set job parameters.
I tried to register my own JobLauncher
@Bean
public JobLauncher jobLauncher(JobRepository jobRepo){
SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher();
simpleJobLauncher.setJobRepository(jobRepo);
return simpleJobLauncher;
}
but when I try to use it in the main method:
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());
}
The job is again executed when the context is loaded and I got JobInstanceAlreadyCompleteException
when I try to run it manually.
Is there a way to prevent the automatic job execution?
The jobs execution can be prevented by setting
spring.batch.job.enabled=false
in application.properties. Or you can use spring.batch.job.names
it takes a comma-delimited list of job names that will be run.
Taken from here: how to stop spring batch scheduled jobs from running at first time when executing the code?
这篇关于Spring Boot如何运行批处理作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!