[英]Spring batch after job is status completed it is launched again and again
我配置了一个工作,该工作从表中读取,传递一些String,然后使用@service将数据存储到Redis实例中。
如您所见,该工作已完成:
INFO SimpleJobLauncher - Job: [FlowJob: [name=populateCacheWithCalendarData]] completed with the following
参数:[{time = 2016-06-22T08:46:09.001}]和以下状态:[COMPLETED]
然后像循环一样一次又一次地启动它:
INFO ScheduledTasks-运行调度的任务[populateCacheWithCalendarData] 25438:INFO SimpleJobLauncher-作业:[FlowJob:[name = populateCacheWithCalendarData]]使用以下参数启动:[{time = 2016-06-22T08:46:10}]
我有一个预定的任务,像这样配置:
@Slf4j
@Component
public class ScheduledTasks {
@Autowired
JobLauncher jobLauncher;
@Autowired
JobRegistry jobRegistry;
// scheduled every 14 hours
@Scheduled(cron = "* * */1 * * *")
public void doPopulateCacheWithCalendarDataJob()
throws NoSuchJobException, JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
// given
log.info("Running Scheduled task [ populateCacheWithCalendarData ]");
Job calendarJob = jobRegistry.getJob("populateCacheWithCalendarData");
JobParametersBuilder paramsBuilder = new JobParametersBuilder().addString("time", LocalDateTime.now().toString());
// then
jobLauncher.run(calendarJob, paramsBuilder.toJobParameters());
}
}
作业配置:
@Configuration
@EnableBatchProcessing
public class CalendarBatchConfiguration extends AbstractBatchConfiguration {
@Bean
public Job populateCacheWithCalendarData() {
return jobBuilderFactory.get("populateCacheWithCalendarData")
.incrementer(new RunIdIncrementer())
.flow(calendarStep1())
.end()
.build();
}
/**
* Look up hotel tickers and creates a csv file with them.
*
* @return ariStep1
*/
@Bean
public Step calendarStep1() {
return stepBuilderFactory.get("calendarStep1")
.<Hotel, String>chunk(100)
.reader(calendarReader())
.processor(calendarProcessor())
.writer(calendarWriter2())
.build();
}
@Bean
public JdbcCursorItemReader<Hotel> calendarReader() {
JdbcCursorItemReader<Hotel> reader = new JdbcCursorItemReader<>();
reader.setSql("SELECT identificador, es_demo FROM instanciasaplicaciones WHERE es_demo = 0 AND version = 6");
reader.setDataSource(this.dataSource);
reader.setRowMapper((resultSet, i) -> new Hotel(resultSet.getString("identificador"), resultSet.getString("es_demo")));
return reader;
}
@Bean
public HotelItemProcessor calendarProcessor() {
return new HotelItemProcessor();
}
@Bean
public CalendarItemWriter calendarWriter2() {
return new CalendarItemWriter();
}
}
处理器和编写器:
@Slf4j
public class CalendarItemProcessor implements ItemProcessor<String, String> {
@Override
public String process(String item) throws Exception {
log.info("Processing calendar hotel Ticker [" + item + "]");
return item;
}
}
@Slf4j
public class CalendarItemWriter implements ItemWriter<String> {
@Autowired
private CalendarService calendarService;
@Override
public void write(List<? extends String> hotelTickers) throws Exception {
log.info("Creating calendar entry in Cache for items... ", hotelTickers.toString());
hotelTickers.forEach(this::createOrUpdateCache);
}
/**
* Use service to store calendar values into the cache.
*
* @param hotelTicker hotelTicker
*/
private void createOrUpdateCache(String hotelTicker) {
// store calendar ari values
calendarService.createOrUpdateCalendarByHotelTicker(hotelTicker);
}
}
主要应用以防万一:
/**
* Main entry point for the Application.
*/
@EnableScheduling
@EnableTransactionManagement
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
我不知道为什么要这样做,因为一段时间后它停止了。
提前致谢
问题是*使调度程序每秒钟,每分钟一分钟地启动任务。 我将其替换为:
@Scheduled(cron = "0 0 */14 * * *")
我希望这可以对其他人有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.