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) {
}
}
}