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);
}
}