我试图在客户端中创建一个while循环,以尝试维持与服务器的许多连接。 当通道处理程序收到服务器的某些响应(错误)时,应关闭通道并标记此通道将来失败。
Channel channel = this.channelPool.acquire().syncUninterruptibly().getNow();
// release channel when closed to unblock creating new channel.
channel.closeFuture().addListener((ChannelFutureListener) future -> {
// not success wait and retry
if(!future.isSuccess()){
trySleep();
}
// close normally, no error, release so can create another connection immediately
this.channelPool.release(future.channel());
});
这里有两个问题
此示例是保持一个连接的好方法。但它需要单身,这是我要避免的。 谢谢!
答案 0 :(得分:0)
只为使用ChannelAttribute传递信息而设计。
ctx.channel().attr(OneAttribute).set(true);
ctx.close();
然后
channel.closeFuture().addListener((ChannelFutureListener) future -> {
// not success wait and retry
if(future.channel().attr(OneAttribute) == some value){
trySleep();
}
// close normally, no error, release so can create another connection immediately
this.channelPool.release(future.channel());