我有一个运行良好但已按计划配置的Spring Batch应用程序,我如何在启动时运行一次并在完成后结束执行?
这是配置:
@Scheduled(fixedRate = 500000)
public void runJob() {
try {
JobParameters jobParameters = new JobParametersBuilder().addLong(
"time", System.currentTimeMillis()).toJobParameters();
jobLauncher.run(processJob, jobParameters);
} catch (Exception e) {
e.printStackTrace();
}
}
@Bean
public Job processJob() {
return jobBuilderFactory.get("processJob")
.incrementer(new RunIdIncrementer()).listener(listener())
.flow(orderStep1()).end().build();
}
和应用程序主类:
@SpringBootApplication(scanBasePackages= "com.companyName")
@EnableBatchProcessing
@EnableScheduling
public class ExpansionDBApplication{
public static void main(String[] args) throws Exception {
SpringApplication.run(ExpansionDBApplication.class, args);
}
}
编辑: 删除注释@Scheduled和@EnableScheduling后,作业无法启动,并且出现2个警告提示:
2019-01-03 09:20:54.438 WARN 14476 --- [ restartedMain] o.s.c.a.ConfigurationClassEnhancer : @Bean method ScopeConfiguration.stepScope is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean javadoc for complete details.
2019-01-03 09:20:54.444 WARN 14476 --- [ restartedMain] o.s.c.a.ConfigurationClassEnhancer : @Bean method ScopeConfiguration.jobScope is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean javadoc for complete details.
它们是指以下字段:
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job processJob;
答案 0 :(得分:1)
我有一个运行良好但已按计划配置的Spring Batch应用程序,我如何在启动时运行一次并在完成后结束执行?
好吧,只需删除调度配置(@EnableScheduling
和@Scheduled(fixedRate = 500000)
),它将在启动时运行,并在完成后结束。
答案 1 :(得分:1)
您还可以通过程序参数覆盖运行方法并控制作业的执行。
当您配置了多个作业时,这很有用。
@SpringBootApplication(scanBasePackages = "com.companyName")
@EnableBatchProcessing
class ExpansionDBApplication implements CommandLineRunner {
@Autowired
private JobLauncher jobLauncher;
@Autowired
private ListableJobLocator listableJobLocator;
public static void main(String[] args) {
SpringApplication app = new SpringApplication(ExpansionDBApplication.class);
app.run(args);
}
@Override
public void run(String... args) throws JobExecutionException {
SimpleCommandLinePropertySource source = new SimpleCommandLinePropertySource(args);
String jobName = source.getProperty("job.name");
if (jobName == null || jobName.isEmpty()) {
executeAllJobs();
} else {
executeJob(jobName);
}
}
private void executeJob(String jobName) throws JobExecutionException {
JobParameters jobParameters = new JobParametersBuilder()
.addString("uid", UUID.randomUUID().toString())
.toJobParameters();
jobLauncher.run(listableJobLocator.getJob(jobName), jobParameters);
}
private void executeAllJobs() throws JobExecutionException {
for (String jobName : listableJobLocator.getJobNames()) {
executeJob(jobName);
}
}
}