之前写到  通过封装的API  已经可以做到使用redis进行缓存天气信息

但是这一操作每次都由客户使用时才进行更新  不友好  所以应该自己实现半小时的定时存入redis  使用quartz框架

 

首先添加依赖

build.gradle中

// Quartz
	compile('org.springframework.boot:spring-boot-starter-quartz')

quartz的配置类

两个重要的bean    JobDetail和Trigger(触发)

package cn.zzu.spring.cloud.weather.config;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import cn.zzu.spring.cloud.weather.job.WeatherDataSyncJob;

/**
 * Quartz Configuration.
 * 
 */
@Configuration
public class QuartzConfiguration {

	private static final int TIME = 1800; // 更新频率

	//两个bean
	
	// JobDetail
	@Bean
	public JobDetail weatherDataSyncJobDetail() {
		return JobBuilder.newJob(WeatherDataSyncJob.class).withIdentity("weatherDataSyncJob")
		.storeDurably().build();
	}
	
	// Trigger
	@Bean
	public Trigger weatherDataSyncTrigger() {
		
		SimpleScheduleBuilder schedBuilder = SimpleScheduleBuilder.simpleSchedule()
				.withIntervalInSeconds(TIME).repeatForever();
		
		return TriggerBuilder.newTrigger().forJob(weatherDataSyncJobDetail())
				.withIdentity("weatherDataSyncTrigger").withSchedule(schedBuilder).build();
	}
}

 

创建天气job任务


import java.util.List;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import cn.zzu.spring.cloud.weather.service.CityDataService;
import cn.zzu.spring.cloud.weather.service.WeatherDataService;
import cn.zzu.spring.cloud.weather.vo.City;

/**
 * Weather Data Sync Job.
 * 天气数据job
 */
public class WeatherDataSyncJob extends QuartzJobBean {
	
	private final static Logger logger = LoggerFactory.getLogger(WeatherDataSyncJob.class);  
	
	@Autowired
	private CityDataService cityDataService;
	
	@Autowired
	private WeatherDataService weatherDataService;
	/* (non-Javadoc)
	 * @see org.springframework.scheduling.quartz.QuartzJobBean#executeInternal(org.quartz.JobExecutionContext)
	 */
	
	//天气数据job
	@Override
	protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
		logger.info("Weather Data Sync Job. Start!");
		// 获取城市ID列表
		List<City> cityList = null;
		
		try {
			cityList = cityDataService.listCity();
		} catch (Exception e) {
			logger.error("Exception!", e);
		}
		
		// 遍历城市ID获取天气
		for (City city : cityList) {
			String cityId = city.getCityId();
			logger.info("Weather Data Sync Job, cityId:" + cityId);
			
			weatherDataService.syncDateByCityId(cityId);
		}
		
		logger.info("Weather Data Sync Job. End!");
	}

}

该job将获取所有的城市  并且请求API将数据写入缓存  设置半小时一次

 

在weatherDataService中添加保存方法  syncDateByCityId(cityId)

 

将其实现

@Override
	public void syncDateByCityId(String cityId) {
		String uri = WEATHER_URI + "citykey=" + cityId;
		this.saveWeatherData(uri);
	}
	
	/**
	 * 把天气数据放在缓存
	 * @param uri
	 */
	private void saveWeatherData(String uri) {
		String key = uri;
		String strBody = null;
		ValueOperations<String, String>  redisUtil = stringRedisTemplate.opsForValue();

		// 调用服务接口来获取
 		ResponseEntity<String> respString = restTemplate.getForEntity(uri, String.class);

 		if (respString.getStatusCodeValue() == 200) {
			strBody = respString.getBody();
		}
		
		// 数据写入缓存
		redisUtil.set(key, strBody, TIME_OUT, TimeUnit.SECONDS);

	}

直接调用API然后存入缓存

这里得添加两个vo类  因为quartz执行的任务就是将城市的天气存入  所以需要city和cityList

package cn.zzu.spring.cloud.weather.vo;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * City.
 * 
 */
@XmlRootElement(name = "d")
@XmlAccessorType(XmlAccessType.FIELD)
public class City {
	@XmlAttribute(name = "d1")
	private String cityId;
	
	@XmlAttribute(name = "d2")
	private String cityName;
	
	@XmlAttribute(name = "d3")
	private String cityCode;
	
	@XmlAttribute(name = "d4")
	private String province;

	public String getCityId() {
		return cityId;
	}

	public void setCityId(String cityId) {
		this.cityId = cityId;
	}

	public String getCityName() {
		return cityName;
	}

	public void setCityName(String cityName) {
		this.cityName = cityName;
	}

	public String getCityCode() {
		return cityCode;
	}

	public void setCityCode(String cityCode) {
		this.cityCode = cityCode;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}
}

注解表示  对应xml文档的根元素(@XmlRootElement(name = "d")) 文档的域(@XmlAttribute(name = "d1"))

package cn.zzu.spring.cloud.weather.vo;

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * City List.
 * 
 
 */
@XmlRootElement(name = "c")
@XmlAccessorType(XmlAccessType.FIELD)
public class CityList {
	@XmlElement(name = "d")
	private List<City> cityList;

	public List<City> getCityList() {
		return cityList;
	}

	public void setCityList(List<City> cityList) {
		this.cityList = cityList;
	}
}

将城市的的xml文件放在static下

新建一个工具类  将XML转为指定的POJO

package cn.zzu.spring.cloud.weather.util;

import java.io.Reader;
import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

/**
 * Xml Builder.
 * 
 */
public class XmlBuilder {

	/**
	 * 将XML转为指定的POJO
	 * @param clazz
	 * @param xmlStr
	 * @return
	 * @throws Exception
	 */
	public static Object xmlStrToOject(Class<?> clazz, String xmlStr) throws Exception {
		Object xmlObject = null;
		Reader reader = null;
		JAXBContext context = JAXBContext.newInstance(clazz);
		
		// XML 转为对象的接口
		Unmarshaller unmarshaller = context.createUnmarshaller();
		
		reader = new StringReader(xmlStr);
		xmlObject = unmarshaller.unmarshal(reader);
		
		if (null != reader) {
			reader.close();
		}
		
		return xmlObject;
	}
}

cityData接口

package cn.zzu.spring.cloud.weather.service;

import java.util.List;

import cn.zzu.spring.cloud.weather.vo.City;

/**
 * City Data Service.
 * 
 */
public interface CityDataService {

	/**
	 * 获取City列表
	 * @return
	 * @throws Exception
	 */
	List<City> listCity() throws Exception;
}
package cn.zzu.spring.cloud.weather.service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import cn.zzu.spring.cloud.weather.util.XmlBuilder;
import cn.zzu.spring.cloud.weather.vo.City;
import cn.zzu.spring.cloud.weather.vo.CityList;

/**
 * City Data Service.
 * 
 */
@Service
public class CityDataServiceImpl implements CityDataService {

	@Override
	public List<City> listCity() throws Exception {
		// 读取XML文件
		Resource resource = new ClassPathResource("citylist.xml");
		BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream(), "utf-8"));
		StringBuffer buffer = new StringBuffer();
		String line = "";
		
		while ((line = br.readLine()) !=null) {
			buffer.append(line);
		}
		
		br.close();
		
		// XML转为Java对象
		CityList cityList = (CityList)XmlBuilder.xmlStrToOject(CityList.class, buffer.toString());
		return cityList.getCityList();
	}

}

由于城市很多  以广东省为例子  贴一下

<?xml version="1.0" encoding="UTF-8"?>
<c c1="0">
<d d1="101280101" d2="广州" d3="guangzhou" d4="广东"/>
<d d1="101280102" d2="番禺" d3="panyu" d4="广东"/>
<d d1="101280103" d2="从化" d3="conghua" d4="广东"/>
<d d1="101280104" d2="增城" d3="zengcheng" d4="广东"/>
<d d1="101280105" d2="花都" d3="huadu" d4="广东"/>
<d d1="101280201" d2="韶关" d3="shaoguan" d4="广东"/>
<d d1="101280202" d2="乳源" d3="ruyuan" d4="广东"/>
<d d1="101280203" d2="始兴" d3="shixing" d4="广东"/>
<d d1="101280204" d2="翁源" d3="wengyuan" d4="广东"/>
<d d1="101280205" d2="乐昌" d3="lechang" d4="广东"/>
<d d1="101280206" d2="仁化" d3="renhua" d4="广东"/>
<d d1="101280207" d2="南雄" d3="nanxiong" d4="广东"/>
<d d1="101280208" d2="新丰" d3="xinfeng" d4="广东"/>
<d d1="101280209" d2="曲江" d3="qujiang" d4="广东"/>
<d d1="101280210" d2="浈江" d3="chengjiang" d4="广东"/>
<d d1="101280211" d2="武江" d3="wujiang" d4="广东"/>
<d d1="101280301" d2="惠州" d3="huizhou" d4="广东"/>
<d d1="101280302" d2="博罗" d3="boluo" d4="广东"/>
<d d1="101280303" d2="惠阳" d3="huiyang" d4="广东"/>
<d d1="101280304" d2="惠东" d3="huidong" d4="广东"/>
<d d1="101280305" d2="龙门" d3="longmen" d4="广东"/>
<d d1="101280401" d2="梅州" d3="meizhou" d4="广东"/>
<d d1="101280402" d2="兴宁" d3="xingning" d4="广东"/>
<d d1="101280403" d2="蕉岭" d3="jiaoling" d4="广东"/>
<d d1="101280404" d2="大埔" d3="dabu" d4="广东"/>
<d d1="101280406" d2="丰顺" d3="fengshun" d4="广东"/>
<d d1="101280407" d2="平远" d3="pingyuan" d4="广东"/>
<d d1="101280408" d2="五华" d3="wuhua" d4="广东"/>
<d d1="101280409" d2="梅县" d3="meixian" d4="广东"/>
<d d1="101280501" d2="汕头" d3="shantou" d4="广东"/>
<d d1="101280502" d2="潮阳" d3="chaoyang" d4="广东"/>
<d d1="101280503" d2="澄海" d3="chenghai" d4="广东"/>
<d d1="101280504" d2="南澳" d3="nanao" d4="广东"/>
<d d1="101280601" d2="深圳" d3="shenzhen" d4="广东"/>
<d d1="101280701" d2="珠海" d3="zhuhai" d4="广东"/>
<d d1="101280702" d2="斗门" d3="doumen" d4="广东"/>
<d d1="101280703" d2="金湾" d3="jinwan" d4="广东"/>
<d d1="101280800" d2="佛山" d3="foshan" d4="广东"/>
<d d1="101280801" d2="顺德" d3="shunde" d4="广东"/>
<d d1="101280802" d2="三水" d3="sanshui" d4="广东"/>
<d d1="101280803" d2="南海" d3="nanhai" d4="广东"/>
<d d1="101280804" d2="高明" d3="gaoming" d4="广东"/>
<d d1="101280901" d2="肇庆" d3="zhaoqing" d4="广东"/>
<d d1="101280902" d2="广宁" d3="guangning" d4="广东"/>
<d d1="101280903" d2="四会" d3="sihui" d4="广东"/>
<d d1="101280905" d2="德庆" d3="deqing" d4="广东"/>
<d d1="101280906" d2="怀集" d3="huaiji" d4="广东"/>
<d d1="101280907" d2="封开" d3="fengkai" d4="广东"/>
<d d1="101280908" d2="高要" d3="gaoyao" d4="广东"/>
<d d1="101281001" d2="湛江" d3="zhanjiang" d4="广东"/>
<d d1="101281002" d2="吴川" d3="wuchuan" d4="广东"/>
<d d1="101281003" d2="雷州" d3="leizhou" d4="广东"/>
<d d1="101281004" d2="徐闻" d3="xuwen" d4="广东"/>
<d d1="101281005" d2="廉江" d3="lianjiang" d4="广东"/>
<d d1="101281006" d2="赤坎" d3="chikan" d4="广东"/>
<d d1="101281007" d2="遂溪" d3="suixi" d4="广东"/>
<d d1="101281008" d2="坡头" d3="potou" d4="广东"/>
<d d1="101281009" d2="霞山" d3="xiashan" d4="广东"/>
<d d1="101281010" d2="麻章" d3="mazhang" d4="广东"/>
<d d1="101281101" d2="江门" d3="jiangmen" d4="广东"/>
<d d1="101281103" d2="开平" d3="kaiping" d4="广东"/>
<d d1="101281104" d2="新会" d3="xinhui" d4="广东"/>
<d d1="101281105" d2="恩平" d3="enping" d4="广东"/>
<d d1="101281106" d2="台山" d3="taishan" d4="广东"/>
<d d1="101281107" d2="蓬江" d3="pengjiang" d4="广东"/>
<d d1="101281108" d2="鹤山" d3="heshan" d4="广东"/>
<d d1="101281109" d2="江海" d3="jianghai" d4="广东"/>
<d d1="101281201" d2="河源" d3="heyuan" d4="广东"/>
<d d1="101281202" d2="紫金" d3="zijin" d4="广东"/>
<d d1="101281203" d2="连平" d3="lianping" d4="广东"/>
<d d1="101281204" d2="和平" d3="heping" d4="广东"/>
<d d1="101281205" d2="龙川" d3="longchuan" d4="广东"/>
<d d1="101281206" d2="东源" d3="dongyuan" d4="广东"/>
<d d1="101281301" d2="清远" d3="qingyuan" d4="广东"/>
<d d1="101281302" d2="连南" d3="liannan" d4="广东"/>
<d d1="101281303" d2="连州" d3="lianzhou" d4="广东"/>
<d d1="101281304" d2="连山" d3="lianshan" d4="广东"/>
<d d1="101281305" d2="阳山" d3="yangshan" d4="广东"/>
<d d1="101281306" d2="佛冈" d3="fogang" d4="广东"/>
<d d1="101281307" d2="英德" d3="yingde" d4="广东"/>
<d d1="101281308" d2="清新" d3="qingxin" d4="广东"/>
<d d1="101281401" d2="云浮" d3="yunfu" d4="广东"/>
<d d1="101281402" d2="罗定" d3="luoding" d4="广东"/>
<d d1="101281403" d2="新兴" d3="xinxing" d4="广东"/>
<d d1="101281404" d2="郁南" d3="yunan" d4="广东"/>
<d d1="101281406" d2="云安" d3="yunan" d4="广东"/>
<d d1="101281501" d2="潮州" d3="chaozhou" d4="广东"/>
<d d1="101281502" d2="饶平" d3="raoping" d4="广东"/>
<d d1="101281503" d2="潮安" d3="chaoan" d4="广东"/>
<d d1="101281601" d2="东莞" d3="dongguan" d4="广东"/>
<d d1="101281701" d2="中山" d3="zhongshan" d4="广东"/>
<d d1="101281801" d2="阳江" d3="yangjiang" d4="广东"/>
<d d1="101281802" d2="阳春" d3="yangchun" d4="广东"/>
<d d1="101281803" d2="阳东" d3="yangdong" d4="广东"/>
<d d1="101281804" d2="阳西" d3="yangxi" d4="广东"/>
<d d1="101281901" d2="揭阳" d3="jieyang" d4="广东"/>
<d d1="101281902" d2="揭西" d3="jiexi" d4="广东"/>
<d d1="101281903" d2="普宁" d3="puning" d4="广东"/>
<d d1="101281904" d2="惠来" d3="huilai" d4="广东"/>
<d d1="101281905" d2="揭东" d3="jiedong" d4="广东"/>
<d d1="101282001" d2="茂名" d3="maoming" d4="广东"/>
<d d1="101282002" d2="高州" d3="gaozhou" d4="广东"/>
<d d1="101282003" d2="化州" d3="huazhou" d4="广东"/>
<d d1="101282004" d2="电白" d3="dianbai" d4="广东"/>
<d d1="101282005" d2="信宜" d3="xinyi" d4="广东"/>
<d d1="101282006" d2="茂港" d3="maogang" d4="广东"/>
<d d1="101282101" d2="汕尾" d3="shanwei" d4="广东"/>
<d d1="101282102" d2="海丰" d3="haifeng" d4="广东"/>
<d d1="101282103" d2="陆丰" d3="lufeng" d4="广东"/>
<d d1="101282104" d2="陆河" d3="luhe" d4="广东"/>
</c>

至此  完成了定时任务  启动试试

开始读取 xml文件中的城市信息 并且quartz的job也执行起来了

访问 有日志  显示是redis中的数据