All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.feingto.iot.server.handler.mqtt.PubrelHandler Maven / Gradle / Ivy

There is a newer version: 1.2.5.RELEASE
Show newest version
package com.feingto.iot.server.handler.mqtt;

import com.feingto.iot.common.service.mqtt.MessageResponse;
import com.feingto.iot.server.cache.MessageCache;
import com.feingto.iot.server.handler.BaseMessageHandler;
import com.feingto.iot.server.service.PushService;
import io.netty.channel.Channel;
import io.netty.handler.codec.mqtt.MqttMessage;
import io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader;
import io.netty.handler.codec.mqtt.MqttMessageType;

import java.util.Optional;

/**
 * Qos level = 2的协议流的第三个消息
 *
 * @author longfei
 */
public class PubrelHandler extends BaseMessageHandler {
    private final PushService pushService;

    public PubrelHandler(PushService pushService) {
        super(MqttMessageType.PUBREL);
        this.pushService = pushService;
    }

    @Override
    public void handle(Channel channel, Object object) {
        MqttMessage msg = (MqttMessage) object;
        MqttMessageIdVariableHeader variableHeader = (MqttMessageIdVariableHeader) msg.variableHeader();
        int messageId = variableHeader.messageId();

        // 发布消息到所有订阅者
        Optional.ofNullable(MessageCache.getInstance(igniteMessage)
                .findByMessageId(messageId))
                .ifPresent(messages -> {
                    // 发布消息到所有订阅者
                    messages.forEach(message -> {
                        pushService.internalSend(message);
                        // 删除持久化消息
                        MessageCache.getInstance(igniteMessage).remove(messageId);
                    });
                });

        // 返回pubcomp消息
        MessageResponse.pubcomp(channel, messageId);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy