tcp Http Client 4.5未使用java中的池连接管理器重用同一连接
下面的代码使用的是ApacheHTTP客户端4.5版本的池连接管理器。如果我发出50个请求,我会在netstat中看到50个不同的tcp端口被使用,但在任何时候都最多使用5个连接。我在wire shark中也看到了filter tcp.flags.syn==1 && tcp.flags.ack==0
它在filter中创建了50个数据包,所以它使用不同的连接,而不是使用相同的连接,那么为什么我不能这样做呢
我的代码:
import javax.net.ssl.SSLContext;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.glassfish.jersey.SslConfigurator;
import org.json.JSONException;
import org.json.JSONObject;
public class App3Conn {
private static CloseableHttpClient client;
static String target="https://example.com";
static PoolingHttpClientConnectionManager cm ;
static{
SslConfigurator sslConfig = SslConfigurator.newInstance()
.securityProtocol("TLS")
.keyStoreFile("/Users/file")
.keyStorePassword("passw")
.keyStoreType("JKS")
.trustStoreFile("/Users/file");
SSLContext sslCtx = sslConfig.createSSLContext();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslCtx,NoopHostnameVerifier.INSTANCE);
Registry<ConnectionSocketFactory> r = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslsf)
.build();
cm = new PoolingHttpClientConnectionManager(r);
cm.setMaxTotal(15);
cm.setDefaultMaxPerRoute(5);
client = HttpClients.custom().setSSLSocketFactory(sslsf).setConnectionManager(cm).build();
}
public static void main(String a[]) throws JSONException, ClientProtocolException, IOException
{
JSONObject jsonMessage = new JSONObject();
JSONObject jsonResponse;
jsonMessage.put("keyID", "keyID" );
StringEntity se = new StringEntity(jsonMessage.toString());
CloseableHttpResponse response2;
HttpPost httpPost = new HttpPost(target);
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("Connection", "keep-alive");
int i;
for(i=0;i<50;i++)
{
response2 = client.execute(httpPost);
HttpEntity entity2 = response2.getEntity();
String result = EntityUtils.toString(entity2);
EntityUtils.consume(entity2);
jsonResponse = new JSONObject(result);
String text = jsonResponse.getString("result");
response2.close();
}
}
}
# 1 楼答案
我以前见过这个。这取决于Apache连接池的工作方式。不透明的
state
成员与池条目相关联,并且在请求(租用)池条目时必须与请求的状态相匹配当进行连接租用尝试时,请求的
state
是null
。然而,当连接被放回池中时,state
被设置为SSL对等方的X500Principal
对象。这发生在DefaultUserTokenHandler
类中。幸运的是,当我们创建HttpClient
类时,这是可以重写的。下面是一个例子:一定要检查
DefaultUserTokenHandler
是否丢失了您所依赖的任何东西,如果您创建的HttpClient
可以通过相同的HTTP路由连接到多个SSL对等方,请不要使用此方法。在我的例子中,该客户端用于连接到单个SSL服务器# 2 楼答案
如果要重用池连接,请不要关闭响应。 不需要回答。close(); 回答close()关闭所有输入/输出资源,最后关闭tcp连接(套接字)