netty维护连接失败如何通知channelfuture
netty maintain connections and how to notify fail to channelfuture
我正在尝试在客户端中创建一个 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());
});
这里有两个问题
- 使用while循环和FixedChannelPool来维护从客户端到服务器的连接数是否正确?
- 如何让channelFuture失败? CloseFuture class 调用 setClosed() 时总是设置成功
https://github.com/netty/netty/blob/4.1/transport/src/main/java/io/netty/channel/AbstractChannel.java#L1159
这个例子是保持一个连接的好方法。但它需要单身人士,这是我想避免的。
谢谢!
想出如何使用 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());
我正在尝试在客户端中创建一个 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());
});
这里有两个问题
- 使用while循环和FixedChannelPool来维护从客户端到服务器的连接数是否正确?
- 如何让channelFuture失败? CloseFuture class 调用 setClosed() 时总是设置成功 https://github.com/netty/netty/blob/4.1/transport/src/main/java/io/netty/channel/AbstractChannel.java#L1159
这个例子是保持一个连接的好方法。但它需要单身人士,这是我想避免的。 谢谢!
想出如何使用 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());