我想创建一个重用通道的连接池,但我想不通
执行这个测试
public void test() {
ClientBootstrap client = new ClientBootstrap(new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
client.setPipelineFactory(new ClientPipelineFactory());
// Connect to server, wait till connection is established, get channel to write to
Channel channel = client.connect(new InetSocketAddress("192.168.252.152", 8080)).awaitUninterruptibly().getChannel();
{
// Writing request to channel and wait till channel is closed from server
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "test");
String xml = "xml document here";
ChannelBuffer buffer = ChannelBuffers.copiedBuffer(msgXml, Charset.defaultCharset());
request.addHeader(HttpHeaders.Names.CONTENT_LENGTH, buffer.readableBytes());
request.addHeader(HttpHeaders.Names.CONTENT_TYPE, "application/xml");
request.setContent(buffer);
channel.write(request).awaitUninterruptibly().getChannel().getCloseFuture().awaitUninterruptibly();
channel.write(request).awaitUninterruptibly().getChannel().getCloseFuture().awaitUninterruptibly();
}
client.releaseExternalResources();
}
我在第二个 channel.write(request) 中得到了 ClosedChannelException ....
是否存在重用渠道的方法?还是保持通道畅通?
提前致谢