【发布时间】:2015-08-05 10:11:33
【问题描述】:
我使用的是 Netty 3.9
我有一个客户场景,我必须按此顺序进行操作
- 连接
- 发送 1 条消息
- 接收 1 条消息
- 断开连接
由于连接和断开连接是一项昂贵的操作,我想尽可能地重用 Netty 级别的对象。
所以我做了以下
ClientBootStrap bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory);
bootstrap.setPipelineFactory(new MyPipeLineFactory());
// set options : soLinger, keepAlive, tcpNoDelay,
ChannelFuture f = bootstrap.connect(remoteAddr,localAddr);
f.awaitUninterruptibly();
Channel ch = f.getChannel();
// use the Channel, send/recv message
// disconnect the channel
ch.disconnect().awaitUninterruptibly();
// save the 'ch' in a map
// Now reuse the same Channel Object
// retreive 'ch'
ch.connect(remoteAddr)
上面的行导致java.nio.channels.ClosedChannelException,可能是因为套接字已关闭。但是 Netty 中没有 api,我可以使用它来断开与服务器的连接,并且仍然保留套接字,以便通过再次连接将其重用于相同的远程地址。
这样可以复用Netty Channel吗?如果是的话怎么办?我需要在 bootsrtap 中设置任何选项吗?
【问题讨论】:
标签: netty