==== 为啥引入Quartz Scheduler
· 实现数据同步,需要定时器
==== 开发环境
· JDK 8+
· Gradle 4+
· Redis 3.2.100
· Apache HttpClient 4.5.3
· Spring Boot Web Starter
· Spring Boot Data Redis Starter
· Spring Boot Quartz Starter
· Quartz Scheduler
==== 构建天气预报系统: 增加定时任务
1)micro-weather-0.2-redis =》 micro-weather-0.3-quartz
(后续简称gradle、src、.gitignore、build.gradle、gradlew、gradlew.bat为:两夹四件)
2)修改build.gradle,添加// Quartz依赖
compile('org.springframework.boot: spring-boot-starter-quartz')
3)导入项目到IDEA,编码
weather/job/:
· 实现数据同步,需要定时器
==== 开发环境
· JDK 8+
· Gradle 4+
· Redis 3.2.100
· Apache HttpClient 4.5.3
· Spring Boot Web Starter
· Spring Boot Data Redis Starter
· Spring Boot Quartz Starter
· Quartz Scheduler
==== 构建天气预报系统: 增加定时任务
1)micro-weather-0.2-redis =》 micro-weather-0.3-quartz
(后续简称gradle、src、.gitignore、build.gradle、gradlew、gradlew.bat为:两夹四件)
2)修改build.gradle,添加// Quartz依赖
compile('org.springframework.boot: spring-boot-starter-quartz')
3)导入项目到IDEA,编码
weather/job/:
package com.shijie.spring.cloud.weather.job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.quartz.QuartzJobBean;
/**
* Created by ShiJie on 2018/2/9 0009
*/
public class WeatherDataSyncJob extends QuartzJobBean {
private static final Logger log = LoggerFactory.getLogger(WeatherDataSyncJob.class);
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
log.info("Weather Data Sync Job");
}
}
weather/config/:
package com.shijie.spring.cloud.weather.config;
import com.shijie.spring.cloud.weather.job.WeatherDataSyncJob;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Created by ShiJie on 2018/2/9 0009
*/
@Configuration
public class QuartzConfiguration {
private static final int INTERVAL_SECONDS = 2;// TODO 更新周期(1800s)
// JobDetail 什么任务
@Bean
public JobDetail weatherDataSyncJobDetail(){
return JobBuilder.newJob(WeatherDataSyncJob.class)
.withIdentity("WeatherDataSyncJob").storeDurably().build();
}
// Trigger 何时触发
@Bean
public Trigger weatherDataSyncTrigger(){
SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(INTERVAL_SECONDS).repeatForever();
return TriggerBuilder.newTrigger().forJob(weatherDataSyncJobDetail())
.withIdentity("weatherDataSyncTrigger").withSchedule(scheduleBuilder).build();
}
}