com.godmao.mqbroker.handler.TopicSubscribeMessageHandler Maven / Gradle / Ivy
package com.godmao.mqbroker.handler;
import com.godmao.mqbroker.Broker;
import com.godmao.mqbroker.Static;
import com.godmao.mqbroker.message.TopicSubscribeMessage;
import com.godmao.mqbroker.message.TopicSyncMessage;
import com.godmao.netty.channel.ChannelService;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* 主题订阅消息处理器
*/
public class TopicSubscribeMessageHandler
extends SimpleChannelInboundHandler {
public final Logger log = LoggerFactory.getLogger(this.getClass());
private final Broker connect;
public TopicSubscribeMessageHandler(Broker connect) {
this.connect = connect;
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, TopicSubscribeMessage msg) {
final Set _topics = msg.getTopics();
if (_topics.isEmpty()) {
return;
}
final Channel channel = ctx.channel();
final Set topics = channel.attr(Static.TOPIC).get();
final Map topicCount = new HashMap<>();
for (final String topic : _topics) {
if (topics.add(topic)) {
topicCount.put(topic, 1);
}
}
//
if (!topicCount.isEmpty()) {
connect.getChannelService().writeAndFlush(
TopicSyncMessageEncoderHandler.INSTANCE.encode(new TopicSyncMessage(Static.TOPIC_VERSION.incrementAndGet(), false, topicCount)),
_channel -> {
final Boolean authorization = _channel.attr(Static.AUTHORIZATION).get();
return null != authorization && authorization;
}
);
}
//
log.info("[主题订阅]-[{}-{}]: {} size:{}", channel.attr(ChannelService.CHANNELID).get(), channel.id().asShortText(), msg, topics.size());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy