一,maven依赖
<dependencies>
<!-- spring整合mqtt 开始-->
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-mqtt</artifactId>
<version>4.1.0.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.eclipse.paho</groupId>
<artifactId>mqtt-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- spring整合mqtt 结束-->
<!-- mqtt依赖 开始 -->
<dependency>
<groupId>org.fusesource.mqtt-client</groupId>
<artifactId>mqtt-client</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
</dependency>
<!-- mqtt依赖 结束 -->
<!-- spring dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<!-- spring dependencies end -->
二,web.xml和spring.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>robot-mainservice</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
<?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:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<!-- 引入mqtt配置文件 -->
<import resource="xml/mqtt.xml"/>
</beans>
三.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: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">
<!-- mqtt客户端 -->
<bean id="clientFactory"
class="org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory">
<property name="userName" value="用户名" />
<property name="password" value="密码" />
</bean>
<!-- 消息适配器 -->
<int-mqtt:message-driven-channel-adapter
id="mqttInbound" client-id="CID_2016524123456" url="tcp://localhost:1883"
topics="robot_server"
qos="1"
client-factory="clientFactory"
auto-startup="true"
send-timeout="12"
channel="startCase" />
<int:channel id="startCase" />
<!-- 消息处理类 -->
<int:service-activator id="startCaseService"
input-channel="startCase" ref="mqttCaseService" method="startCase" />
<bean id="mqttCaseService" class="com.efrobot.api.mqtt.MqttService" />
</beans>
public class MqttService {
public void startCase(String message){
System.out.println(message);
}
}
示例:
{"msgContent":{"status":-10000,"descri":""},"msgType":"4","robotId":"100000000106"}
{"msgContent":{"filterUseTime":-10000,"filterDescri":"","conf":"","filterStatus":"","status":"0#-10000#0#0#0","descri":""},"msgType":"3","robotId":"100000000106"}
{"msgContent":{"smell":106,"air_Quality":"","pm":77,"temperature":26,"humIdity":46},"msgType":"2","robotId":"100000000104"}
{"msgContent":{"filterUseTime":0,"filterDescri":"","conf":"","filterStatus":"","status":"1#2#1#0#0","descri":""},"msgType":"3","robotId":"100000000104"}
{"msgContent":{"status":-10000,"descri":""},"msgType":"4","robotId":"100000000104"}