使用多线程获取中国天气网天气信息

private PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); 
	
	private ExecutorService executorService = Executors.newFixedThreadPool(64);
	
	@PostConstruct
	private void initHttpClientConnectionManager() {
		cm.setMaxTotal(30);  //总的连接数
		cm.setDefaultMaxPerRoute(5); //每个host的最大连接数 
	}


CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();  
		RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).build();  
		List<String> list = new ArrayList<String>();
		try {
			ParseXml parseXml = new ParseXml();
			Map<String,String> map = parseXml.read();
			for (Entry<String, String> entry: map.entrySet()) {
			    String cityid = entry.getKey();
				String url = "http://113.108.239.115/weather/"+cityid+".shtml";
				list.add(url);
			}
			String[] array = new String[list.size()];
			String[] urisToGet = list.toArray(array);
			
			GetThread[] threads = new GetThread[urisToGet.length];  
			for (int i = 0; i < threads.length; i++) {  
			    HttpGet httpget = new HttpGet(urisToGet[i]);  
			    httpget.setConfig(requestConfig);
			    threads[i] = new GetThread(httpClient, httpget);  
			}  
			  
			for (int j = 0; j < threads.length; j++) {  
			   // new Thread(threads[j]).start();  
				executorService.execute(threads[j]);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		  
	}
	 class GetThread implements Runnable {  
		  
	    private final CloseableHttpClient httpClient;  
	    private final HttpContext context;  
	    private final HttpGet httpget;  
	    public GetThread(CloseableHttpClient httpClient, HttpGet httpget) {  
	        this.httpClient = httpClient;  
	        this.context = HttpClientContext.create();  
	        this.httpget = httpget;  
	    }  
	    
	    public void run() {  
	        try {  
	            CloseableHttpResponse response = httpClient.execute(httpget, context);  
	            try {  
	                HttpEntity entity = response.getEntity(); 
	                if(entity!=null){
	                	String html = EntityUtils.toString(entity);
	                	RealTimeWeather realtime = WeatherUtil.getCurrentDayInfo(html);
	                	String httpurl = httpget.toString();
	                	String cityid = httpurl.substring(httpurl.lastIndexOf("r/")+2, httpurl.lastIndexOf(".shtml"));
	                	ParseXml parseXml = new ParseXml();
	                	Map<String,String> map = parseXml.read();
	                	String city = map.get(cityid);
	                	WeatherUtil WeatherUtil = new WeatherUtil();
	                	List<WeatherInfo> weatherInfos = null;
	                	List<WeatherLife> weatherLifeList = null;
	                	if(realtime != null){
	                		weatherInfos = new ArrayList<WeatherInfo>();
	                		weatherLifeList = new ArrayList<WeatherLife>();
	                		weatherInfos = WeatherUtil.getFiveDayInfo(html);
	                		weatherLifeList = realtime.getList();
	                	}else{
		                	city = map.get(cityid);
	                		weatherInfos = new ArrayList<WeatherInfo>();
	                		realtime = BaiduWeatherUtil.GetWeater(city).getRealtimeWeather();
	        				weatherInfos = BaiduWeatherUtil.GetWeater(city).getWeatherInfoList();
	        				weatherLifeList = BaiduWeatherUtil.GetWeater(city).getWeatherLifeList();
	                	}
	                	Map<String, Object> info = weather.util.WeatherUtil.getWeatherData(cityid);
	                	if(info!=null){
	                		String sd = info.get("SD").toString();
		        			String ws = info.get("WS").toString();
		        			String wd = info.get("WD").toString();
		        			String tem = info.get("temp").toString();
		        			if(sd==null || "".equals(sd)){
		        				sd = "无";
		        			}
		        			realtime.setSd(sd);
		        			realtime.setWs(ws);
		        			realtime.setWd(wd);
		        			realtime.setTem(tem + "°C");
	                	}
	        			realtime.setCity(city);
	        			realtime.setCityId(cityid);
	        			//保存当天天气信息
	        			RealTimeWeather weatherdata = getweatherService.findWeatherByID(cityid);
	        			if (weatherdata != null) {
	        				getweatherService.delweather(cityid);
	        			}
	        			getweatherService.saveweather(realtime);
	        			
	        			// 保存5天/4天气信息
	        			List<WeatherInfo> weatherInfoList = getweatherService.findWeatherInfoByID(cityid);
	        			if (weatherInfoList != null && weatherInfoList.size() > 0) {
	        				for (WeatherInfo weatherInfo : weatherInfoList) {
	        					getweatherService.delweatherInfo(weatherInfo.getCityid());
	        				}
	        			}
	        			for(int i= 0;i<weatherInfos.size();i++){
	        				WeatherInfo weatherInfo = weatherInfos.get(i);
	        				weatherInfo.setCity(city);
	        				weatherInfo.setCityid(cityid);
	        				weatherInfo.setTime(i+1);
	        				getweatherService.saveweatherInfo(weatherInfo);
	        			}
	        			//保存生活指数 
	        			List<WeatherLife> weatherLifes = getweatherService.findWeatherLifeByID(cityid);
	        			if (weatherLifes != null && weatherLifes.size() > 0) {
	        				for (WeatherLife weatherLife : weatherLifes) {
	        					getweatherService.delweatherlift(weatherLife.getCityid());
	        				}
	        			}
	        			for(int i= 0;i<weatherLifeList.size();i++){
	        				WeatherLife weatherLife = weatherLifeList.get(i);
	        				weatherLife.setCity(city);
	        				weatherLife.setCityid(cityid);
	        				getweatherService.saveWeatherLife(weatherLife);
	        			}
	                }
				} finally {  
	                response.close();  
	            }  
	        } catch (ClientProtocolException ex) {  
	        } catch (IOException ex) {  
	        }  
	    }
	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值