org.apache.commons.httpclient学习
初始化连接管理器参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| private static String DEFAULT_CHARSET = "GBK"; private int defaultConnectionTimeout = 8000; private int defaultSoTimeout = 30000; private int defaultIdleConnTimeout = 60000; private int defaultMaxConnPerHost = 30; private int defaultMaxTotalConn = 80; private static final long defaultHttpConnectionManagerTimeout = 3 * 1000; private HttpConnectionManager connectionManager;
|
声明一个私有化构造方法用来初始化连接管理器,并引入连接池
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| private HttpProtocolHandler() { connectionManager = new MultiThreadedHttpConnectionManager(); connectionManager.getParams().setDefaultMaxConnectionsPerHost(defaultMaxConnPerHost); connectionManager.getParams().setMaxTotalConnections(defaultMaxTotalConn);
IdleConnectionTimeoutThread ict = new IdleConnectionTimeoutThread(); ict.addConnectionManager(connectionManager); ict.setConnectionTimeout(defaultIdleConnTimeout);
ict.start(); } `
|
IdleConnectionTimeoutThread是一个用List实现的连接池,
并且是后台服务线程。list不断的去循环当前的http连接管理器判断当前的连接管理器是否超时关闭。