com.feingto.iot.server.handler.BaseMessageHandler Maven / Gradle / Ivy
package com.feingto.iot.server.handler;
import com.feingto.iot.common.handler.MessageHandler;
import com.feingto.iot.common.model.mqtt.SendMessage;
import com.feingto.iot.common.model.mqtt.SubscribeMessage;
import com.feingto.iot.server.cache.MessageCache;
import com.feingto.iot.server.cache.SessionCache;
import com.feingto.iot.server.cache.SubscribeCache;
import io.netty.channel.Channel;
import io.netty.handler.codec.mqtt.MqttMessageType;
import lombok.Setter;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.apache.ignite.IgniteCache;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
/**
* 消息处理抽象
*
* @author longfei
*/
@Slf4j
@Accessors(fluent = true)
public abstract class BaseMessageHandler implements MessageHandler {
private MqttMessageType type;
private List filters = new ArrayList<>();
@Setter
protected IgniteCache> igniteSubscribe;
@Setter
protected IgniteCache igniteRetained;
@Setter
protected IgniteCache> igniteMessage;
public BaseMessageHandler(MqttMessageType type) {
this.type = type;
}
public BaseMessageHandler filter(BaseMessageHandler filter) {
if (igniteSubscribe != null) {
filter.igniteSubscribe(igniteSubscribe);
}
if (igniteRetained != null) {
filter.igniteRetained(igniteRetained);
}
if (igniteMessage != null) {
filter.igniteMessage(igniteMessage);
}
this.filters.add(filter);
return this;
}
public void proceed(MqttMessageType type, Channel channel, Object object) {
if (this.type.equals(type)) {
log.debug(">>> message type: {}", type);
handle(channel, object);
}
this.filters.stream()
.filter(Objects::nonNull)
.filter(filter -> type.equals(filter.type))
.findFirst()
.ifPresent(filter -> filter.proceed(type, channel, object));
}
/**
* 清除会话缓存
*/
protected void cleanSession(String clientId) {
SessionCache.getInstance().remove(clientId);
SubscribeCache.getInstance(igniteSubscribe).remove(clientId);
MessageCache.getInstance(igniteMessage).remove(clientId);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy