编写的Webserivce调用天气预报 (及接口比较)

不想浪费了自己写的一个Utility,发布上来,以后可以用

下面是目前找的一些天气WebService的对比较: 只有Webxml那个ok点,但免费的有限:

 

fhs.6617.com     打不开     http://fhs.6617.com/getweather.asmx?WSDL


www.webxml.com.cn     稳定,免费的有数量及速度限制 250次/24小时 450ms/request    

http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl


www.ayandy.com     免费未测试,可以获取数据少    

http://www.ayandy.com/Service.asmx?op=getWeatherbyCityName


soft.wopos.com     打不开     http://soft.wopos.com/webservice/weather.asmx
webservice.k-zone.cn     打不开    

 

 

有了这些数据就可以用接口来写调用了.

下面是自己写的一个Utility,但后来发现不只是一个简单的Utility,老大是要我写一个Webservice天气服务器.不想浪费了这个Utility

里面有缓存的思想(主要解决请求数量限制).  队列请求(主要解决请求速度限制). 自动更新缓存(让缓存有一个过期时限)

 

  1. package cn.easier.webservice.weather;  
  2. import java.util.HashMap;  
  3. import java.util.LinkedList;  
  4. import java.util.Map;  
  5. import java.util.Queue;  
  6. import java.util.Set;  
  7. import org.apache.log4j.Logger;  
  8. import webxml.WeatherWebServiceStub;  
  9. import webxml.WeatherWebServiceStub.ArrayOfString;  
  10. import webxml.WeatherWebServiceStub.GetWeatherbyCityName;  
  11. import webxml.WeatherWebServiceStub.GetWeatherbyCityNameResponse;  
  12. /** 
  13.  * 天气接口 
  14.  *  
  15.  * @author Vange 
  16.  *  
  17.  */  
  18. public class WeatherUtils {  
  19.     /** 
  20.      * 数据缓存 
  21.      */  
  22.     private static Map<String, Object[]> data = new HashMap<String, Object[]>();  
  23.     private static Logger logger = Logger.getLogger(WeatherUtils.class);  
  24.     /** 
  25.      * 缓存过期期限,默认为一小时 
  26.      */  
  27.     private static long expired = 3600 * 1000// 1800 seconds, half a   
  28.     // hour   
  29.     /** 
  30.      * 更新所有缓存的间隔,默认为半小时 
  31.      */  
  32.     private static long updateCacheInterval = 1800 * 1000;  
  33.     /** 
  34.      * 请求间隔 
  35.      */  
  36.     private static int requestInterval = 500// the interval(millisecond) to   
  37.     // request the   
  38.     // weather server .   
  39.     /** 
  40.      * 请求队列 
  41.      */  
  42.     private static Queue<String> queue = new LinkedList<String>();  
  43.     private static Refresher refresher;  
  44.     static {  
  45.         refresher = new Refresher();  
  46.         refresher.start();  
  47.     }  
  48.     /** 
  49.      * 关闭自动更新线程 
  50.      */  
  51.     public static void closeAutoRefresh() {  
  52.         logger.info("close auto refresh");  
  53.         if (refresher != null) {  
  54.             refresher.setStop(true);  
  55.             refresher.interrupt();  
  56.         }  
  57.     }  
  58.     /** 
  59.      * 获取数据,根据城市名(如广州 ,不要带"市"字) 
  60.      *  
  61.      * @param cityName 
  62.      * @return 
  63.      */  
  64.     public static WeatherInfo getWeatherByName(String cityName) {  
  65.         logger.debug("want to get data of " + cityName);  
  66.         if (cityName == null)  
  67.             return null;  
  68.         // 读取缓存中数据   
  69.         if (data.containsKey(cityName)) {  
  70.             // 判断缓存是否已经过期   
  71.             Object[] cache = data.get(cityName);  
  72.             if (cache != null) {  
  73.                 WeatherInfo obj = (WeatherInfo) cache[0];  
  74.                 long lastUpdateTime = (Long) cache[1];  
  75.                 if (obj != null  
  76.                         && lastUpdateTime + expired > System  
  77.                                 .currentTimeMillis()) {  
  78.                     return obj;  
  79.                 }  
  80.             }  
  81.             logger.debug(cityName + " 's value is not exists");  
  82.         } else {  
  83.             logger.debug("the key " + cityName + " is not exists");  
  84.             // 如果没有,直接返回null   
  85.             // 把些请求,加入到请求队列中,和数据字典中   
  86.             data.put(cityName, null);  
  87.             synchronized (queue) {  
  88.                 queue.offer(cityName);  
  89.             }  
  90.             return null;  
  91.         }  
  92.         return null;  
  93.     }  
  94.     /** 
  95.      * 向接口请求天气数据 
  96.      */  
  97.     protected static void requestWeatherInfo() {  
  98.         if (queue.isEmpty())  
  99.             return;  
  100.         String requestCityName = null;  
  101.         synchronized (queue) {  
  102.             requestCityName = queue.poll();  
  103.         }  
  104.         if (requestCityName == null)  
  105.             return;  
  106.         requestWeatherInfo(requestCityName);  
  107.     }  
  108.     /** 
  109.      * 向接口请求天气数据 并处理对应的数据 
  110.      *  
  111.      * @param requestCityName 
  112.      */  
  113.     public static WeatherInfo requestWeatherInfo(String requestCityName) {  
  114.         logger.debug("REAL get info from Server : " + requestCityName);  
  115.         try {  
  116.             WeatherWebServiceStub stub = new WeatherWebServiceStub();  
  117.             GetWeatherbyCityName cityname = new GetWeatherbyCityName();  
  118.             cityname.setTheCityName(requestCityName);  
  119.             GetWeatherbyCityNameResponse ret = stub  
  120.                     .getWeatherbyCityName(cityname);  
  121.             ArrayOfString cityNameResult = ret.getGetWeatherbyCityNameResult();  
  122.             if (cityNameResult != null) {  
  123.                 // logger.debug("get date : " + cityNameResult);   
  124.                 String[] returns = cityNameResult.getString();  
  125.                 if (returns[1] == null || "".equals(returns[1])) {  
  126.                     logger.info("requestCityName is NOT supported ! "  
  127.                             + requestCityName);  
  128.                     // 没有相关数据   
  129.                     return null;  
  130.                 }  
  131.                 // 读取相关数据   
  132.                 WeatherInfo info = new WeatherInfo();  
  133.                 info.setCityName(returns[1]);  
  134.                 info.setDateAndWeather(returns[6]);  
  135.                 info.setTemperature(returns[5]);  
  136.                 info.setWind(returns[7]);  
  137.                 data.put(requestCityName, new Object[] { info,  
  138.                         System.currentTimeMillis() });  
  139.                   
  140.                 if (logger.isDebugEnabled())  
  141.                     logger.debug("ADD CACHE :" + requestCityName);  
  142.                 return info;  
  143.             }  
  144.             return null;  
  145.         } catch (Exception e) {  
  146.             e.printStackTrace();  
  147.             logger.error(e.getMessage());  
  148.         }  
  149.         return null;  
  150.     }  
  151.     /** 
  152.      * 请求更新所有缓存 
  153.      */  
  154.     protected static void updateAllWeatherInfo() {  
  155.         logger.debug("update all the weather info");  
  156.         Set<String> keys = data.keySet();  
  157.         synchronized (queue) {  
  158.             for (String key : keys) {  
  159.                 queue.offer(key);  
  160.             }  
  161.         }  
  162.     }  
  163.     /** 
  164.      * 定时刷新器 
  165.      *  
  166.      * @author Vange 
  167.      *  
  168.      */  
  169.     private static class Refresher extends Thread {  
  170.         /** 
  171.          * 停止标识 
  172.          */  
  173.         private boolean stop = false;  
  174.         private long lastUpdateCache = 0L;  
  175.         @Override  
  176.         public void run() {  
  177.             super.run();  
  178.             while (true) {  
  179.                 try {  
  180.                     if (stop)  
  181.                         break;  
  182.                     // 更新每一个数据   
  183.                     sleep(requestInterval);  
  184.                     requestWeatherInfo();  
  185.                     // 更新所有缓存   
  186.                     if (System.currentTimeMillis() > lastUpdateCache  
  187.                             + updateCacheInterval) {  
  188.                         lastUpdateCache = System.currentTimeMillis();  
  189.                         updateAllWeatherInfo();  
  190.                     }  
  191.                 } catch (Exception e) {  
  192.                     logger.error(e.getMessage());  
  193.                 }  
  194.             }  
  195.         }  
  196.         /** 
  197.          * 设置结束标识 
  198.          *  
  199.          * @param b 
  200.          */  
  201.         public void setStop(boolean b) {  
  202.             this.stop = b;  
  203.         }  
  204.     }  
  205. }  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值