我配置了一个从表读取的作业,传递了一些String,然后使用@service将数据存储到Redis实例中。
工作已完成,如您所见:
INFO SimpleJobLauncher - Job: [FlowJob: [name=populateCacheWithCalendarData]] completed with the following
参数:[{time = 2016-06-22T08:46:09.001}]以及以下状态: [完成]
然后它就像一个循环中一次又一次地启动:
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);
}
}
我不知道为什么会这样做,因为经过一段时间它会停止。
提前致谢
答案 0 :(得分:0)
问题在于,*每秒,每分钟和儿子都会使调度程序启动作业。我替换它:
@Scheduled(cron = "0 0 */14 * * *")
我希望这可以帮助其他人