У меня есть приложение 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);
}
}
Edit:
После удаления аннотаций @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;