HttpClientHelper_PushClient

this is httpClinetHelper

private static HttpClient httpClient;

    public static HttpClient gethttpClient() {
        if (httpClient == null)
            initHttpClient();
        return httpClient;
    }
    
    /** 
    * @Title: initHttpClient 
    * @Description: httpclient
    * @throws 
    */ 
    private static synchronized void initHttpClient() {
        // 创建httpclient连接池
        PoolingHttpClientConnectionManager httpClientConnectionManager = new PoolingHttpClientConnectionManager();
        httpClientConnectionManager.setMaxTotal(100); // 设置连接池线程最大数量
        httpClientConnectionManager.setDefaultMaxPerRoute(20); // 设置单个路由最大的连接线程数量
        // 创建http request的配置信息
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectionRequestTimeout(30 * 1000)
                .setSocketTimeout(30 * 1000).build();
        // 设置重定向策略
        LaxRedirectStrategy redirectStrategy = new LaxRedirectStrategy();
        // 初始化httpclient客户端
        httpClient = HttpClients.custom()
                .setConnectionManager(httpClientConnectionManager)
                .setDefaultRequestConfig(requestConfig)
                .setRedirectStrategy(redirectStrategy).build();
        LOG.info("initHttpClient success......");
    }

this is PushClient

HttpClient httpClient = HttpClientHelper.gethttpClient();
	
	
	
	
	/** 
	* @Title: getJSON 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param url
	* @return
	* @throws ClientProtocolException
	* @throws IOException
	* @throws 
	*/ 
	public String getJSON(String url) throws ClientProtocolException, IOException {
		HttpGet get = new HttpGet(url);
		HttpResponse response = null;
		LOG.info("HttpClient->get:"+url);
		String result = null;
		response = httpClient.execute(get);
		result = IOUtils.toString(new InputStreamReader(response
					.getEntity().getContent(), "utf-8"));
		LOG.info("HttpClient->get:"+result);
		return result;
	}
	
	/** 
	* @Title: postJSON 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param object
	* @param url
	* @return
	* @throws ClientProtocolException
	* @throws IOException
	* @throws 
	*/ 
	public String postJSON(Object object, String url) throws ClientProtocolException, IOException {
		String result = null;
		HttpResponse response = null;
		try{
			HttpPost httpPost = new HttpPost(url);
			String json = JSON.toJSONString(object);
			LOG.info("HttpClient->post:"+url);
			if(null != json){
				LOG.info("HttpClient->post:"+json);
				StringEntity entity = new StringEntity(json,"utf-8");//解决中文乱码问题    
		        entity.setContentEncoding("UTF-8");    
		        entity.setContentType("application/json");    
		        httpPost.setEntity(entity);
		     } 
			response = httpClient.execute(httpPost);
			result = IOUtils.toString(new InputStreamReader(response
						.getEntity().getContent(), "utf-8"));
			LOG.info("HttpClient->post:"+result);
			return result;
		}catch (Exception e){
			throw e;
		}finally{
			if(response!=null){
				EntityUtils.consumeQuietly(response.getEntity());
			}
			 //关闭空闲超过30秒的连接
			httpClient.getConnectionManager().closeIdleConnections(30, TimeUnit.SECONDS);
		}
	}
	

	
	/** 
	* @Title: postData 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param map
	* @param url
	* @return
	* @throws ClientProtocolException
	* @throws IOException
	* @throws 
	*/ 
	@SuppressWarnings("deprecation")
    public String postData(Map<String,String> map, String url) throws ClientProtocolException, IOException {
		HttpPost httpPost = new HttpPost(url);
		HttpResponse response = null;
		String result = null;

		if(null != map && map.size()>0){
			List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();

			Set<String> keySet = map.keySet();
			for(String key : keySet) {
				nvps.add(new BasicNameValuePair(key, map.get(key)));
			}
			httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
		}
		response = httpClient.execute(httpPost);
		result = IOUtils.toString(new InputStreamReader(response
				.getEntity().getContent(), "utf-8"));
		LOG.info("HttpClient->post:"+result);
		return result;
	}
	
	public String post(Object o, String url) throws ClientProtocolException, IOException {
		org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
		PostMethod method = new PostMethod(url);
		int statusCode;
		String responseCharSet ="";
		String responseString = "";
		try {
			String json = o.toString();
			StringRequestEntity entity = new StringRequestEntity(json,"application/json","utf-8");//解决中文乱码问题
			method.setRequestEntity(entity);
			//method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
			statusCode = httpClient.executeMethod(method);
			if (statusCode != HttpStatus.SC_OK) {
				LOG.error(">>>>>>>>>>>>>> Method failed: " + method.getStatusLine());
				LOG.error(">>>>>>>>>>>>>> Http服务链路异常:服务器状态码为" + statusCode);
			}
			responseCharSet = method.getResponseCharSet();
			responseString = method.getResponseBodyAsString();
			if (responseCharSet.equals("ISO-8859-1")) {
				responseString = new String(responseString.getBytes(responseCharSet), "UTF-8");
			}

		} catch (Throwable e) {
			LOG.error(">>>>>>>>>>>>>>  Http服务链路异常:" + e.getMessage() + e);
		} finally {
			method.releaseConnection();
		}
		return responseString;
	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HttpClient close_wait不释放是指当HttpClient执行完请求后,关闭连接后却不能及时释放连接,出现了close_wait状态,可能会导致资源的浪费和程序性能的降低。这种情况通常发现在服务器的TCP连接数一直在增加,却一直处于close_wait状态,并且ClientSocketImpl的状态一直是CLOSED_WAIT,造成资源的浪费和服务器处理能力的下降。 造成HttpClient close_wait不释放的原因可能有以下几点: 1. 连接池没有及时释放连接。HttpClient有一个默认的连接池管理器,如果连接池同时被多个线程调用,可能会出现连接池没有及时释放连接的情况,导致close_wait状态的产生。 2. Http客户端没有及时关闭连接。有些Http客户端请求完成后不主动释放连接,导致连接一直处于close_wait状态。 3. 服务器没有及时关闭连接。如果服务器没有在超时时间内关闭连接,那么连接就会一直处于close_wait状态。 解决HttpClient close_wait不释放的方法有以下几点: 1. 使用HttpClient连接池管理器时,需要设置合理的连接超时时间和请求超时时间,及时关闭闲置连接或者过期的连接。 2. 在Http客户端请求完成后,及时关闭连接,避免连接一直处于close_wait状态。 3. 调整服务器的超时时间,及时关闭连接。 4. 调整服务器的TCP/IP协议参数,设置合理的TIME_WAIT时间。 总之,为了避免出现close_wait状态的连接,需要在使用HttpClient连接池管理器时,合理调整连接超时时间和请求超时时间,并在请求完成后及时释放连接,避免资源的浪费和程序性能的降低。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值