Spring集成MQTT(spring5.1.6)

因项目需要用到MQTT,一番折腾,水平有限,请大佬多多指点。

这里MQTT我用的是ActiveMQ,安装方法见我的另一篇文章:https://blog.csdn.net/w979000397/article/details/104048176

Spring我用的是5.1.6,在与MQTT集成上4和5有些不同,个人感觉5稍微简单一些,先来看看官方的说明:https://docs.spring.io/spring-integration/reference/html/mqtt.html

1.集成需要的jar包

org.eclipse.paho.client.mqttv3-1.2.1.jar

spring-integration-mqtt-5.1.6.RELEASE.jar

spring-integration-core-5.1.6.RELEASE.jar

spring-messaging-5.1.6.RELEASE.jar

reactive-streams-1.0.3.jar

reactor-core-3.2.3.RELEASE.jar

2.spring-mqtt.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:int="http://www.springframework.org/schema/integration"
       xmlns:int-mqtt="http://www.springframework.org/schema/integration/mqtt"
       xsi:schemaLocation="
	http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
	http://www.springframework.org/schema/integration/mqtt http://www.springframework.org/schema/integration/mqtt/spring-integration-mqtt.xsd
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- clientFactory 新版本登录信息需要设置到 org.eclipse.paho.client.mqttv3.MqttConnectOptions -->
    <bean id="clientFactory" class="org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory">
        <property name="connectionOptions">
            <bean class="org.eclipse.paho.client.mqttv3.MqttConnectOptions">
                <property name="userName" value="admin"/>
                <property name="password" value="admin"/>
                <property name="keepAliveInterval" value="30"/>
                <property name="serverURIs">
                    <array>
                        <value>tcp://127.0.0.1:1883</value>
                    </array>
                </property>
            </bean>
        </property>
    </bean>

    <!-- 入站(消息驱动)通道适配器-->
    <int:channel id="mqttInputChannel"/>
    <!-- 出站(消息驱动)通道适配器-->
    <int:channel id="mqttOutputChannel"/>

    <!-- 消费者 -->
    <int-mqtt:message-driven-channel-adapter
            id="mqttInbound"
            client-id="mqtt_bound_in"
            qos="1"
            client-factory="clientFactory"
            auto-startup="true"
            send-timeout="10"
            channel="mqttInputChannel"
            topics="/TOPIC_TEST/#"/>

    <!--生产者 -->
    <int-mqtt:outbound-channel-adapter
            id="mqttOutbound"
            client-id="mqtt_bound_out"
            client-factory="clientFactory"
            auto-startup="true"
            default-qos="1"
            default-topic="TOPIC_TEST"
            channel="mqttOutputChannel"/>

    <!-- 自定义消息接收类 -->
    <bean id="mqttMessageHandle" class="cn.com.sysystem.mqtt.MqttMessageHandle"></bean>

    <!--将自定义消息接收类装配到入站适配器, 用于接收消息-->
    <int:service-activator id="startCaseService" input-channel="mqttInputChannel" ref="mqttMessageHandle" method="handleMessage"/>

    <!-- 配置出站网关,用于发送消息 -->
    <int:gateway service-interface="cn.com.sysystem.mqtt.MqttMessageSender" id="mqttMessageSender" default-request-channel="mqttOutputChannel"/>

</beans>

生产者与消费者中配置说明见官方说明:https://docs.spring.io/spring-integration/reference/html/mqtt.html#mqtt-inbound

注:topics中的"#"用于模糊匹配,如上配置中的"/TOPIC_TEST/"主题相关的消息都会被MqttMessageHandle接收

3.定义消息接收类MqttMessageHandle 

import org.springframework.integration.mqtt.support.MqttHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessagingException;

import java.util.List;

/**
 * 入站消息接收类
 */
public class MqttMessageHandle {

    public void handleMessage(Message<String> message) throws MessagingException {
        String topic = message.getHeaders().get(MqttHeaders.RECEIVED_TOPIC).toString();
        String content = message.getPayload();
        System.out.println("收到消息");
        System.out.println("主题:" + topic);
        System.out.println("内容:" + content);
    }
}

4.定义消息发送接口(出站网关:gateway)

import org.springframework.integration.mqtt.support.MqttHeaders;
import org.springframework.messaging.handler.annotation.Header;

public interface MqttMessageSender {
    void sendMessage(@Header(MqttHeaders.TOPIC) String topic, String message);
}

5.发送消息

在cotroller或者service中注入出站网关MqttMessageSender ,调用sendMessage方法即可发送消息

@RestController  
@RequestMapping("/mqttTest")  
public class TestMessageController{
	
    @Autowired
    private MqttMessageSender mqttMessageSender;  
      
    @RequestMapping(value="/sendMessage")  
    public void sendMessage(){   
    	mqttMessageSender.sendMessage("TOPIC_TEST" ,"您好,世界!");
    }
}

6.接收订阅消息

项目启动成功后,配置中订阅主题的相关消息都会被节点 3.定义消息接收类MqttMessageHandle 接收,可根据不同topic执行相应的业务处理。

疑惑:在运行时添加或删除主题
见官方说明:https://docs.spring.io/spring-integration/reference/html/mqtt.html#adding-and-removing-topics-at-runtime,很想用此功能,但尚未搞明白该怎么弄,有搞过的朋友不妨留个连接,感激不尽。

完!

 

发布了3 篇原创文章 · 获赞 4 · 访问量 1892
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览