用java写一个http连接池,支持不同域名访问的连接池
时间: 2024-01-04 21:03:12 浏览: 125
网站开发JNDI与连接池.pptx
好的,下面是一个简单的Java HTTP连接池,支持不同域名访问的连接池。该连接池使用Apache HttpClient库来管理HTTP连接。请注意,这只是一个示例,您需要根据您的具体需求进行修改。
```java
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
public class HttpConnectionPool {
private static final int MAX_TOTAL_CONNECTIONS = 100;
private static final int MAX_CONNECTIONS_PER_ROUTE = 20;
private static final int CONNECTION_TIMEOUT = 5000; // in milliseconds
private static final int SOCKET_TIMEOUT = 5000; // in milliseconds
private PoolingHttpClientConnectionManager connectionManager;
private Map<String, CloseableHttpClient> httpClientMap;
public HttpConnectionPool() {
connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(MAX_TOTAL_CONNECTIONS);
connectionManager.setDefaultMaxPerRoute(MAX_CONNECTIONS_PER_ROUTE);
httpClientMap = new HashMap<>();
}
public String doGet(String baseUrl, Map<String, String> params) throws IOException, URISyntaxException {
String domain = new URIBuilder(baseUrl).getHost();
CloseableHttpClient httpClient = getHttpClient(domain);
HttpUriRequest request = new HttpGet(baseUrl);
if (params != null && !params.isEmpty()) {
URIBuilder uriBuilder = new URIBuilder(baseUrl);
for (Map.Entry<String, String> entry : params.entrySet()) {
uriBuilder.addParameter(entry.getKey(), entry.getValue());
}
request = new HttpGet(uriBuilder.build());
}
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(CONNECTION_TIMEOUT)
.setSocketTimeout(SOCKET_TIMEOUT)
.setProxy(new HttpHost(domain, 80))
.build();
request.setConfig(config);
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
String result = "";
if (entity != null) {
result = EntityUtils.toString(entity);
}
return result;
}
private CloseableHttpClient getHttpClient(String domain) {
CloseableHttpClient httpClient = httpClientMap.get(domain);
if (httpClient == null) {
httpClient = HttpClientBuilder.create()
.setConnectionManager(connectionManager)
.build();
httpClientMap.put(domain, httpClient);
}
return httpClient;
}
}
```
使用示例:
```java
HttpConnectionPool pool = new HttpConnectionPool();
Map<String, String> params = new HashMap<>();
params.put("key", "value");
String result = pool.doGet("https://www.example.com/path", params);
System.out.println(result);
```
在这个示例中,我们使用了一个HashMap来缓存已经创建的HttpClient。每当需要发送HTTP请求时,我们首先从请求的URL中提取出域名,然后从缓存中获取对应的HttpClient。如果缓存中没有对应的HttpClient,则创建一个新的HttpClient,并将其添加到缓存中。这样可以避免为每个请求都创建一个新的HttpClient,从而减少资源消耗。同时,我们使用了连接池来管理HTTP连接,确保连接得到重复利用,从而提高性能。
请注意,该示例中使用了HTTP代理,您需要根据您的具体情况进行修改。
阅读全文