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

com.feingto.iot.server.service.PushService Maven / Gradle / Ivy

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

import com.feingto.iot.common.model.mqtt.SendMessage;
import com.feingto.iot.common.model.mqtt.SubscribeMessage;
import com.feingto.iot.common.service.mqtt.MessageRequest;
import com.feingto.iot.server.cache.SessionCache;
import com.feingto.iot.server.cache.SubscribeCache;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.IgniteMessaging;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Mqtt 消息推送服务
 *
 * @author longfei
 */
@Slf4j
@Service
public class PushService {
    private static final String LISTEN_TOPIC = "internal-mqtt-topic";

    @Resource(name = "igniteSubscribe")
    protected IgniteCache> igniteSubscribe;

    @Resource(name = "igniteMessaging")
    private IgniteMessaging igniteMessaging;

    @PostConstruct
    public void initListen() {
        igniteMessaging.localListen(LISTEN_TOPIC, (nodeId, msg) -> {
            log.debug(">>> listen from cluster group nodeId: {}", nodeId);
            this.push((SendMessage) msg);
            return true;
        });
    }

    /**
     * 推送消息
     */
    private void push(SendMessage mqttMessage) {
        SubscribeCache.getInstance(igniteSubscribe).findByTopic(mqttMessage.topic())
                .forEach(subscribe -> Optional.ofNullable(SessionCache.getInstance()
                        .get(subscribe.clientId()))
                        .ifPresent(sess -> {
                            log.debug(">>> send to {} with topic {}", subscribe.clientId(), mqttMessage.topic());
                            MessageRequest.publish(sess.channel(), mqttMessage);
                        }));
    }

    /**
     * 发布集群消息
     */
    public void internalSend(SendMessage mqttMessage) {
        if (CollectionUtils.isNotEmpty(igniteMessaging.clusterGroup().nodes())) {
            log.debug(">>> send to cluster group");
            igniteMessaging.send(LISTEN_TOPIC, mqttMessage);
        } else {
            this.push(mqttMessage);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy