为什么 Spring 启动批处理作业 运行 只是一次?
Why Spring Boot Batch job is running just one time?
我正在使用 spring 启动。我有一个批处理作业,我已经用这些 classes 实现了:
我的主要 class 是:
@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);
}
}
我的批处理配置 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;
} ...
现在,我的问题是:
调度程序工作并且每十秒重复一次,
但作业仅在应用程序启动时执行(reader,处理器和编写器仅在启动时执行一次)并且似乎
SpringApplication.run(MyBatchConfig.class);
对重新运行工作没有影响。
我该怎么办?
提前致谢
您需要创建 JobLauncher
bean 并在调度程序中使用它来启动新的作业实例。
这是我能想到的,
1.You 将此 属性 放在 application.properties 中,这样您的批处理作业就不会通过从 main
方法调用 SpringApplication.run(...)
自动启动。
spring.batch.job.enabled=false
这只会初始化您的 Spring 批处理配置,而不是真正开始作业。
2.Put 注释 @EnableScheduling
在您的 Spring 启动批处理作业开始 class 即在您的代码中的 Application
class 上。
3.Remove @EnableScheduling
来自 JobScheduler
的注释 class 并从 runJob()
调用 , jobLauncher.run(job, jobParameters)
而不是调用 SpringApplication.run(MyBatchConfig.class)
。
JobLauncher
& Job
beans 可以自动连接到你的调度程序 class 因为上下文已经初始化。
希望对您有所帮助!!
我正在使用 spring 启动。我有一个批处理作业,我已经用这些 classes 实现了:
我的主要 class 是:
@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);
}
}
我的批处理配置 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;
} ...
现在,我的问题是:
调度程序工作并且每十秒重复一次, 但作业仅在应用程序启动时执行(reader,处理器和编写器仅在启动时执行一次)并且似乎
SpringApplication.run(MyBatchConfig.class);
对重新运行工作没有影响。
我该怎么办?
提前致谢
您需要创建 JobLauncher
bean 并在调度程序中使用它来启动新的作业实例。
这是我能想到的,
1.You 将此 属性 放在 application.properties 中,这样您的批处理作业就不会通过从 main
方法调用 SpringApplication.run(...)
自动启动。
spring.batch.job.enabled=false
这只会初始化您的 Spring 批处理配置,而不是真正开始作业。
2.Put 注释 @EnableScheduling
在您的 Spring 启动批处理作业开始 class 即在您的代码中的 Application
class 上。
3.Remove @EnableScheduling
来自 JobScheduler
的注释 class 并从 runJob()
调用 , jobLauncher.run(job, jobParameters)
而不是调用 SpringApplication.run(MyBatchConfig.class)
。
JobLauncher
& Job
beans 可以自动连接到你的调度程序 class 因为上下文已经初始化。
希望对您有所帮助!!