Welcome to Yumao′s Blog.
通用多線程HttpClient類
, 2012年12月10日 , Java Language , 评论 在〈通用多線程HttpClient類〉中留言功能已關閉 ,

最近寫的項目中有頻繁的用到HttpClient
反正日誌也沒有什麼時間寫
那就貼點寫好的代碼
當作備份好了 P

import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.log4j.Logger;

import com.hisunsray.commons.res.Config;


public class ThreadHttpClient {
	private static Logger logger = Logger.getLogger(ThreadHttpClient.class);
	private static ThreadHttpClient threadhttpclient;
	private DefaultHttpClient httpclient;
	public static void load(){
		threadhttpclient = new ThreadHttpClient();
	}
	public static ThreadHttpClient getInstance() {
		if(threadhttpclient == null){
			load();
		}
		return threadhttpclient;
	}
	public void initHttp() {
		logger.info("初始化HttpClient多线程处理");
		//设置最大连接数
		PoolingClientConnectionManager cm = new PoolingClientConnectionManager();
		int maxTotal = Integer.parseInt(Config.getProperty("THREADSAFEHTTP_MAXTOTAL"));
		cm.setMaxTotal(maxTotal);
		this.httpclient = new DefaultHttpClient(cm);
	}	
	public void shutHttp(){
		if(null != httpclient){
			logger.info("关闭HttpClient多线程处理");
			httpclient.getConnectionManager().shutdown(); 
			//设置读数据超时时间
			//httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, "10000");
			//设置连接超时时间
			//httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, "12000");
		}
	}
	public DefaultHttpClient getHttpclient() {
		return httpclient;
	}
	public void setHttpclient(DefaultHttpClient httpclient) {
		this.httpclient = httpclient;
	}
}

关键字:,

评论已关闭