pom.xml
<!-- 定時(shí)器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
設(shè)置配置類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();
}
}
添加定時(shí)任務(wù)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("定時(shí)啟動(dòng):" + DateUtil.now());
List<Index> indexes = indexService.fresh();
for (Index index : indexes) {
indexDataService.fresh(index.getCode());
}
System.out.println("定時(shí)結(jié)束:" + DateUtil.now());
}
}