spring和mqtt集成
Posted 1024toadmin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring和mqtt集成相关的知识,希望对你有一定的参考价值。
spring框架和mqtt集成的方式
借鉴网络上部分开发文档
配置文件: spring-mtqq.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:context="http://www.springframework.org/schema/context"
- 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-4.1.xsd
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
- http://www.springframework.org/schema/integration/mqtt http://www.springframework.org/schema/integration/mqtt/spring-integration-mqtt-4.1.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.1.xsd">
- <bean id="clientFactory"
- class="org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory">
- <property name="userName" value="username"/>
- <property name="password" value="password"/>
- <property name="connectionTimeout" value="10"/>
- <property name="keepAliveInterval" value="20"/>
- <property name="serverURIs">
- <array>
- <value>url</value>
- </array>
- </property>
- </bean>
- <bean id="mqttHandler" class="org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler">
- <constructor-arg name="clientId" value="cilentId"/>
- <constructor-arg name="clientFactory" ref="clientFactory"/>
- <property name="defaultQos" value="1"/>
- <property name="defaultRetained" value="true"/>
- </bean>
- </beans>
点击查询DefaultMqttPahoClientFactory类将需要的属性配置;
点击查询MqttPahoMessageHandler、AbstractMqttMessageHandler将需要的属性配置;
代码调用:
引入 mqttHandler 调用其handlerMessage(message)方法将Message 消息发送
以上是关于spring和mqtt集成的主要内容,如果未能解决你的问题,请参考以下文章
Spring Integration Dispatcher 没有频道订阅者
Spring Boot 集成 WebSocket,轻松实现信息推送!
Spring Boot MQTT Too many publishes in progress错误的解决方案