问题描述
我正在使用弹簧靴.我有一个使用这些类实现的批处理作业:
I'm using spring boot. I have a batch job which I've implemented with these classes :
我的主要课程是:
@SpringBootApplication
@ComponentScan("com.batch")
@PropertySource("classpath:application.properties")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
我的调度程序是:
@Component
@EnableScheduling
public class JobScheduler {
@Scheduled(fixedRate = 10000)
public void runJob() {
SpringApplication.run(MyBatchConfig.class);
}
}
我的批处理配置类是:
@Configuration
@EnableBatchProcessing
public class MyBatchConfig {
@Value("${database.driver}")
private String databaseDriver;
@Value("${database.url}")
private String databaseUrl;
@Value("${database.username}")
private String databaseUsername;
@Value("${database.password}")
private String databasePassword;
@Bean
public Job myJob(JobBuilderFactory jobs, Step s) {
Job job = jobs.get("myJob")
.incrementer(new RunIdIncrementer())
.flow(s)
.end()
.build();
return job;
}
@Bean
public Step myStep(StepBuilderFactory stepBuilderFactory, ItemReader<Account> reader,
ItemWriter<Person> writer, ItemProcessor<Account, Person> processor) {
TaskletStep step = stepBuilderFactory.get("myStep")
.<Account, Person>chunk(1)
.reader(reader)
.processor(processor)
.writer(writer)
.build();
step.setAllowStartIfComplete(true);
return step;
} ...
现在,我的问题是:
调度程序可以正常工作,并且每十秒钟重复一次,但是作业仅在应用程序启动时执行(读取器,处理器和写入器在启动时仅执行一次),看来
the scheduler works and it repeats every ten seconds,but the job executes only on application startup(reader, processor and writer just execute once in startup) and it seems that
SpringApplication.run(MyBatchConfig.class);
对重新运行作业没有影响.
has no effect on re-running the job.
我该怎么办?
预先感谢
推荐答案
这是我能想到的,
1.您将此属性放在application.properties中,因此您的批处理作业不会通过main
方法中的SpringApplication.run(...)
调用自动启动.
1.You put this property in application.properties so your batch job doesn't start automatically by call of SpringApplication.run(...)
from main
method.
spring.batch.job.enabled=false
这只会简单地初始化您的Spring Batch配置,而不会真正开始工作.
This will simply initialize your Spring Batch configuration and not actually start job.
2.在Spring Boot Batch Job起始类上添加注释@EnableScheduling
,即在代码中的Application
类上.
2.Put annotation @EnableScheduling
on your Spring Boot Batch Job starting class i.e. on Application
class in your code.
3.从JobScheduler
类中删除@EnableScheduling
批注,然后从runJob()
中调用jobLauncher.run(job, jobParameters)
,而不是调用SpringApplication.run(MyBatchConfig.class)
.
3.Remove @EnableScheduling
annotation from JobScheduler
class and call , jobLauncher.run(job, jobParameters)
from runJob()
instead of calling SpringApplication.run(MyBatchConfig.class)
.
JobLauncher
&由于上下文已初始化,因此Job
bean可以自动连接到您的调度程序类.
JobLauncher
& Job
beans can be auto wired to your scheduler class since context is already initialized.
希望有帮助!
这篇关于为什么Spring Boot Batch作业仅运行一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!