我正在使用ChannelGroup来广播消息,遗憾的是我还没有找到解决方案来正确处理组中可能的慢接收器。
在网上搜索了很多,但仍然没有弄清楚如何利用channelWritabilityChanged()或ChannelGroupFutureListener来实现我想要的服务器行为:服务器可以一直保持正常客户端的广播,同时不要写入慢速接收器通道isWritable()成为现实。
基本服务器通道处理程序如下所示:
public class ServerChannelHandler extends SimpleChannelInboundHandler<String> {
private ChannelGroup group;
@Override
protected void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {
BizObject bizObject = doBizLogic(msg);
group.writeAndFlush(bizObject);
}
}
非常感谢您的帮助!
答案 0 :(得分:0)
我相信你应该按照你的描述实现自己的逻辑:
(伪代码)
For all channel in group
if channel.isWritable is true => immediate write and flush
possibility 1) (difficult) else through channel.channelPipeline() get the right handler
on future channelWritabilityChanged(), write what you need
you need to handle various things:
- the message to send,
- the possibility the writabilityChanged before your handler is ready to handle this
- ...
possibility 2) (easy) else add this channel in a new temporary group
call once the loop is over this group asynchronously as you did
- group.writeAndFlush(object) in an extra thread (so totally asynchronously)
- with a possible extra object (shared by the caller thread and the callee)
if you want to know when all messages are effectively sent