[netty] netty服务器端,主送发送指令,如果对方客户端未收到,如何设置重新发送??

public class MessageHandler extends ChannelInboundHandlerAdapter implements ChannelOutboundHandler {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        Map newMap = new HashMap<>();
        // 每隔三秒重发一次消息
        ctx.executor().scheduleAtFixedRate(() -> {
            if (map.size() > 0) 
            {
                map.forEach((k, v) -> {
                    ctx.writeAndFlush(v.data);
                    v.times = v.times + 1;
                    if (v.times < 3) 
                    {
                        // 保留未发送三次的。
                        newMap.put(v.id, v);
                    }
                });
            }
            map = newMap;
        }, 3, 3, TimeUnit.SECONDS);
    }
}

猜你喜欢

转载自www.cnblogs.com/wxxujian/p/12706420.html