MQTT to subscribe to the message store database

First, the business scene

        Tip: For Broker (MQTT server), the publisher whether we are, or subscribers, belong to the client

         Data acquisition hardware will be reported to MQTT server, our platform (own WEB service) will subscribe to the messages stored in the database , how to use JAVA as a client subscription message has to explain in my previous article on how to use JAVA prepared MQTT MQTT server client connections .

        For a lot of friends that understand a little deviation in time Tell me what network, the official online face, said open source version does not support the message store database, which means the main expression of the message store database server does not automatically MQTT received (when the Enterprise Edition is after configuration, MQTT server will automatically receive the message store database), but we can write your own code to achieve this function, not trouble.

Second, the solution

         Based on the above issue for open source version, there are two ways to subscribe to the message store database, as follows:

         A way, we can write their own plugins using EMQX support for custom plug-in extensions, but personally do not recommend using custom plug-ins, inflexible, too much trouble, I own is the message store database subscriptions received in the callback function.

         Second way, here mainly on how to call in our service callback service, a subscription to a data store database

         1. First, Springboot project to build the framework of the preparation of MQTT client code is described in detail in my previous chapter written in JAVA MQTT client connection MQTT server .

         2, how to call in our service callback service to store data, you may use a lot of friends in the form of annotations directly in the callback function @Autowired, just as we directly call the service controller in service, but the actual use of injected notes found service service is empty, quoted you java.lang.NullException, then disconnect the MQTT it.

             In fact, the use of annotations in the form of service can get to services, it is because we coded incorrectly. Given below is one kind of uses ApplicationContext Spring obtaining context service can get our way service, then the service is invoked method corresponding to the operation of the database.

package com.siborui.am.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Spring工具类,获取Spring上下文对象等
 *
 * @author Mr.Qu
 * @since 2020/1/9 16:26
 */
@Component
public class SpringContextUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if(SpringContextUtil.applicationContext == null){
            SpringContextUtil.applicationContext  = applicationContext;
        }
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static Object getBean(String name){
        return getApplicationContext().getBean(name);
    }

    public static <T> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);
    }

    public static <T> T getBean(String name,Class<T> clazz){
        return getApplicationContext().getBean(name, clazz);
    }
}

 The above is a write operation ApplicationContext I get spring context tools, you can call this tool getBean class () method directly in the callback function for example:

package com.siborui.dc.mqtt;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;

/**
 * MQTT回调函数
 *
 * @author Mr.Qu
 * @since 2020/1/9 16:26
 */
@Slf4j
public class Callback implements MqttCallback {

    /**
     * MQTT 断开连接会执行此方法
     */
    @Override
    public void connectionLost(Throwable throwable) {
        log.info("断开了MQTT连接 :{}", throwable.getMessage());
        log.error(throwable.getMessage(), throwable);
    }

    /**
     * publish发布成功后会执行到这里
     */
    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        log.info("发布消息成功");
    }

    /**
     * subscribe订阅后得到的消息会执行到这里
     */
    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        //  TODO    此处可以将订阅得到的消息进行业务处理、数据存储
        log.info("收到来自 " + topic + " 的消息:{}", new String(message.getPayload()));
        MqttDataService mqttDataService=SpringContextUtil.getBean(MqttDataService.class);
        mqttDataService.save(new String(message.getPayload()));
    }
}

Mainly: MqttDataService mqttDataService = SpringContextUtil.getBean (MqttDataService.class); acquisition services to our service.

Third, the end of the

Do not know where to take notes, I feel this article useful to your little friends, you can play hard cash reward a tea

 

Released six original articles · won praise 7 · views 3079

Guess you like

Origin blog.csdn.net/qq_37949192/article/details/103998723