Apache Commons Pool 创建一个 Http连接池 并使用
时间: 2023-06-14 08:07:53 浏览: 130
thriftpool:Thrift RPC连接池,java实现,基于apache commons pool
Apache Commons Pool 是一个通用的对象池实现库,可以用于创建和管理任何类型的对象池。在创建Http连接池时,我们可以使用Apache Commons Pool来管理连接池中的Http连接对象。
以下是使用Apache Commons Pool创建Http连接池的示例代码:
1. 首先,我们需要定义一个HttpConnectionFactory类,用于创建Http连接对象:
```
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.apache.http.HttpHost;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
public class HttpConnectionFactory implements org.apache.commons.pool2.PooledObjectFactory<HttpClient> {
private PoolingHttpClientConnectionManager connManager;
public HttpConnectionFactory(PoolingHttpClientConnectionManager connManager) {
this.connManager = connManager;
}
@Override
public PooledObject<HttpClient> makeObject() throws Exception {
RequestConfig.Builder requestBuilder = RequestConfig.custom();
requestBuilder = requestBuilder.setConnectTimeout(5000);
requestBuilder = requestBuilder.setConnectionRequestTimeout(5000);
requestBuilder = requestBuilder.setSocketTimeout(5000);
HttpClient client = HttpClientBuilder.create()
.setDefaultRequestConfig(requestBuilder.build())
.setConnectionManager(connManager)
.build();
return new DefaultPooledObject<>(client);
}
@Override
public void destroyObject(PooledObject<HttpClient> p) throws Exception {
HttpClient client = p.getObject();
client.close();
}
@Override
public boolean validateObject(PooledObject<HttpClient> p) {
HttpClient client = p.getObject();
HttpGet httpGet = new HttpGet("/");
try {
client.execute(httpGet);
return true;
} catch (Exception e) {
return false;
}
}
@Override
public void activateObject(PooledObject<HttpClient> p) throws Exception {
}
@Override
public void passivateObject(PooledObject<HttpClient> p) throws Exception {
}
}
```
2. 接下来,我们需要创建一个连接池配置类:
```
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
public class HttpPoolConfig extends GenericObjectPoolConfig<HttpClient> {
public HttpPoolConfig() {
setTestWhileIdle(true);
setMinIdle(5);
setMaxIdle(10);
setMaxTotal(50);
setBlockWhenExhausted(true);
setMaxWaitMillis(5000);
}
}
```
3. 然后,我们可以在我们的主类中创建Http连接池:
```
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.http.HttpHost;
import org.apache.http.client.HttpClient;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
public class Main {
public static void main(String[] args) {
Registry<ConnectionSocketFactory> reg = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.build();
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(reg);
connManager.setMaxTotal(50);
connManager.setDefaultMaxPerRoute(20);
HttpConnectionFactory factory = new HttpConnectionFactory(connManager);
HttpPoolConfig config = new HttpPoolConfig();
GenericObjectPool<HttpClient> pool = new GenericObjectPool<>(factory, config);
// 从连接池获取Http连接对象
HttpClient httpClient = null;
try {
httpClient = pool.borrowObject();
// 使用Http连接对象发送请求
// ...
} catch (Exception e) {
e.printStackTrace();
} finally {
if (httpClient != null) {
// 将Http连接对象归还给连接池
try {
pool.returnObject(httpClient);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
```
在上面的代码中,我们首先创建了一个连接池配置类 HttpPoolConfig,然后使用 HttpConnectionFactory 类创建了一个连接工厂,并将连接工厂和连接池配置传递给了 GenericObjectPool 类,从而创建了一个 Http 连接池。
接着,我们使用 borrowObject() 方法从连接池中获取一个 Http 连接对象,并使用该对象发送请求。最后,我们使用 returnObject() 方法将 Http 连接对象归还给连接池。
阅读全文