pom.xml
<!-- 定时器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
设置配置类QuartzConfiguration
@Configuration
public class QuartzConfiguration {
// 一分钟的间隔
private static final int interval = 1;
@Bean
public JobDetail weatherDataSyncJobDetail() {
return JobBuilder.newJob(IndexDataSyncJob.class).withIdentity("indexDataSyncJob")
.storeDurably().build();
}
@Bean
public Trigger weatherDataSyncTrigger() {
SimpleScheduleBuilder schedBuilder = SimpleScheduleBuilder.simpleSchedule()
.withIntervalInMinutes(interval).repeatForever();
return TriggerBuilder.newTrigger().forJob(weatherDataSyncJobDetail())
.withIdentity("indexDataSyncTrigger").withSchedule(schedBuilder).build();
}
}
添加定时任务IndexDataSyncJob
public class IndexDataSyncJob extends QuartzJobBean {
@Autowired
private IndexService indexService;
@Autowired
private IndexDataService indexDataService;
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
System.out.println("定时启动:" + DateUtil.now());
List<Index> indexes = indexService.fresh();
for (Index index : indexes) {
indexDataService.fresh(index.getCode());
}
System.out.println("定时结束:" + DateUtil.now());
}
}
网友评论