我正在尝试在JAVA中创建一个非I / O阻止Web服务客户端。我正在使用CXF的AsyncHTTPConduit。
但是,当有两个请求时,似乎它将创建一个新连接而不是重用它。它将创建新的动态端口以发送Web服务。
源代码: public Dispatch getDispatcher(字符串别名,字符串名称空间,字符串wsdlLocation,字符串serviceName,字符串portName)引发IOException,GeneralSecurityException {
Dispatch<Source> dispatcher = getDispatcher(wsdlLocation, namespace, serviceName, portName);
// Create our custom SSLSocketFactory
SSLSocketFactory socketFactory = new SSLSocketFactoryGenerator(alias, keyStoreLocation, trustStoreLocation)
.getSSLSocketFactory();
Client client = ((org.apache.cxf.jaxws.DispatchImpl) dispatcher).getClient();
client.getRequestContext().put("use.async.http.conduit", true);
client.getRequestContext().put(AsyncHTTPConduit.USE_ASYNC, AsyncHTTPConduitFactory.UseAsyncPolicy.ALWAYS);
client.getRequestContext().put("org.apache.cxf.transport.http.async.MAX_CONNECTIONS", 501);
HTTPConduit conduit = (HTTPConduit) client.getConduit();
System.out.println("testtest: "+conduit.getClass().getName());
TLSClientParameters tlsClientParameters = new TLSClientParameters();
tlsClientParameters.setSSLSocketFactory(socketFactory);
conduit.setTlsClientParameters(tlsClientParameters);
conduit.getClient().setConnection(ConnectionType.KEEP_ALIVE);
conduit.getClient().setReceiveTimeout(10000000L);
return dispatcher;
}
呼叫调度程序以发送Web服务:
Source requestSource = (new StreamSource(new StringReader(request)));
MyHandler handler = new MyHandler();
Future<?> f = dispatch.invokeAsync(requestSource, handler);
Thread.sleep(3000);
MyHandler handler2 = new MyHandler();
Source requestSource2 = (new StreamSource(new StringReader(request)));
Future<?> f2 = dispatch.invokeAsync(requestSource2, handler2);
日志文件:
08:24:35.491 [I / O调度程序1]调试org.apache.http.impl.nio.conn.ManagedNHttpClientConnectionImpl-http-outgoing-0 127.0.0.1:13629<->127.0.0.1:8080[ACTIVE ] [rw:w]:写入542个字节 08:24:35.491 [I / O调度程序1]调试org.apache.http.wire-http-outgoing-0 >>“ POST / fghet / sd / qqwsdfdd?wsdl HTTP / 1.1 [\ r] [\ n]” 08:24:35.491 [I / O调度程序1]调试org.apache.http.wire-http-outgoing-0 >>“接受: / [\ r] [\ n]” 08:24:35.491 [I / O调度程序1]调试org.apache.http.wire-http-outgoing-0 >>“用户代理:Apache CXF 3.1.4.redhat-1 [\ r] [\ n] ” 08:24:35.491 [I / O调度程序1]调试org.apache.http.wire-http-outgoing-0 >>“连接:保持活动[\ r] [\ n]” 08:24:35.491 [I / O调度程序1]调试org.apache.http.wire-http-outgoing-0 >>“ Content-Length:323 [\ r] [\ n]” 08:24:35.491 [I / O调度程序1] DEBUG org.apache.http.wire-http-outgoing-0 >>“ Content-Type:application / soap + xml; charset = UTF-8 [\ r] [\ n]” 08:24:35.491 [I / O调度程序1]调试org.apache.http.wire-http-outgoing-0 >>“主机:localhost:8080 [\ r] [\ n]” 08:24:35.491 [I / O调度程序1]调试org.apache.http.wire-http-outgoing-0 >>“ [\ r] [\ n]” 08:24:35.491 [I / O调度程序1] DEBUG org.apache.http.wire-http-outgoing-0 >>“ AAAAAAA003YYYYYYYY” 08:24:35.491 [I / O调度程序1]调试org.apache.http.impl.nio.conn.ManagedNHttpClientConnectionImpl-http-outgoing-0 127.0.0.1:13629<->127.0.0.1:8080[ACTIVE][r :w]:事件已清除[w] 08:24:38.468 [main]调试org.apache.http.impl.nio.client.MainClientExec-[exchange:2]开始执行 08:24:38.469 [main]调试org.apache.http.client.protocol.RequestAddCookies-CookieSpec已选择:默认 08:24:38.469 [main]调试org.apache.http.client.protocol.RequestAuthCache-未在上下文中设置身份验证缓存 08:24:38.469 [main]调试org.apache.http.impl.nio.client.InternalHttpAsyncClient-[exchange:2]请求连接{}-> http://localhost:8080 08:24:38.469 [main]调试org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager-连接请求:[route:{}-> http://localhost:8080][total保持活动状态:0;分配的路线:1000个中的1个;已分配的总数:5000个中的1个] 08:24:38.470 [I / O调度程序2] DEBUG org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager-租借的连接:[id:http-outgoing-1] [路由:{}-> {{3} }保持生命:0;分配的路线:1000个中的2个;分配的总数:5000个中的2个] 08:24:38.470 [I / O调度程序2]调试org.apache.http.impl.nio.client.InternalHttpAsyncClient-[交换:2]分配的连接:CPoolProxy {http-outgoing-1 [ACTIVE]} 08:24:38.470 [I / O调度程序2] DEBUG org.apache.http.impl.nio.conn.ManagedNHttpClientConnectionImpl-http-outgoing-1 127.0.0.1:13630<->127.0.0.1:8080[ACTIVE][r :]:设置属性http.nio.exchange-handler 08:24:38.470 [I / O调度程序2]调试org.apache.http.impl.nio.conn.ManagedNHttpClientConnectionImpl-http-outgoing-1 127.0.0.1:13630<->127.0.0.1:8080[ACTIVE][rw :]:事件集[w] 08:24:38.470 [I / O调度程序2]调试org.apache.http.impl.nio.conn.ManagedNHttpClientConnectionImpl-http-outgoing-1 127.0.0.1:13630<->127.0.0.1:8080[ACTIVE][rw :]:设置超时0 08:24:38.470 [I / O调度程序2]调试org.apache.http.impl.nio.client.InternalIODispatch-http-outgoing-1 [ACTIVE]:已连接 08:24:38.470 [I / O调度程序2]调试org.apache.http.impl.nio.conn.ManagedNHttpClientConnectionImpl-http-outgoing-1 127.0.0.1:13630<->127.0.0.1:8080[ACTIVE][rw :]:设置属性http.nio.http-exchange-state 08:24:38.470 [I / O调度程序2]调试org.apache.http.impl.nio.client.InternalHttpAsyncClient-开始连接路由 08:24:38.470 [I / O调度程序2]调试org.apache.http.impl.nio.client.MainClientExec-已建立连接路由 08:24:38.470 [I / O调度程序2]调试org.apache.http.impl.nio.client.MainClientExec-[交换:2]尝试1执行请求 08:24:38.470 [I / O调度程序2] DEBUG org.apache.http.impl.nio.client.MainClientExec-目标身份验证状态:未挑战 08:24:38.470 [I / O调度程序2] DEBUG org.apache.http.impl.nio.client.MainClientExec-代理身份验证状态:未挑战