├── README.md ├── src └── main │ ├── resources │ ├── application.properties │ └── config │ │ └── application-local.properties │ └── java │ └── com │ └── thinmoo │ └── cloud │ ├── ThinmooCloudApplication.java │ ├── mqtt │ ├── MqttSubClient.java │ ├── PushCallback.java │ ├── MqttConfiguration.java │ └── MqttPushClient.java │ └── controller │ └── MqttDemoController.java ├── .gitignore └── pom.xml /README.md: -------------------------------------------------------------------------------- 1 | # mqttDemo 2 | springboot整合mqtt 3 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #java -server -Xms256m -Xmx512m -jar springboot-1.0.jar 2 | server.port = 8099 3 | spring.profiles.active=local 4 | 5 | -------------------------------------------------------------------------------- /src/main/resources/config/application-local.properties: -------------------------------------------------------------------------------- 1 | #MQTT配置信息 2 | #MQTT-服务器连接地址,如果有多个,用逗号隔开,如:tcp://127.0.0.1:61613,tcp://192.168.2.133:61613 3 | ximo.mqtt.host=tcp://192.168.10.254:1883 4 | #MQTT-连接服务器默认客户端ID 5 | ximo.mqtt.clientid=mqttjs_cloud 6 | #MQTT-用户名 7 | ximo.mqtt.username=admin 8 | #MQTT-密码 9 | ximo.mqtt.password=public 10 | #连接超时 11 | ximo.mqtt.timeout=1000 12 | #心跳检查时间 13 | ximo.mqtt.keepalive = 10 14 | -------------------------------------------------------------------------------- /src/main/java/com/thinmoo/cloud/ThinmooCloudApplication.java: -------------------------------------------------------------------------------- 1 | package com.thinmoo.cloud; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication(scanBasePackages= {"com.thinmoo.cloud"}) 7 | public class ThinmooCloudApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(ThinmooCloudApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | /target/ 3 | /logs/ 4 | /dev/ 5 | /test/ 6 | /local/ 7 | /prod/ 8 | /uploadFiles/ 9 | /iospem/ 10 | /bin/ 11 | /.mvn/ 12 | !.mvn/wrapper/maven-wrapper.jar 13 | 14 | *prod-debug.properties 15 | 16 | ### STS ### 17 | .apt_generated 18 | .classpath 19 | .factorypath 20 | .project 21 | .settings 22 | .springBeans 23 | .sts4-cache 24 | 25 | ### IntelliJ IDEA ### 26 | .idea 27 | *.iws 28 | *.iml 29 | *.ipr 30 | 31 | ### NetBeans ### 32 | /nbproject/private/ 33 | /nbbuild/ 34 | /dist/ 35 | /nbdist/ 36 | /.nb-gradle/ 37 | /build/ 38 | 39 | ### VS Code ### 40 | .vscode/ 41 | -------------------------------------------------------------------------------- /src/main/java/com/thinmoo/cloud/mqtt/MqttSubClient.java: -------------------------------------------------------------------------------- 1 | package com.thinmoo.cloud.mqtt; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.eclipse.paho.client.mqttv3.MqttClient; 5 | import org.eclipse.paho.client.mqttv3.MqttException; 6 | import org.springframework.stereotype.Component; 7 | 8 | /** 9 | * @Author:Dong 10 | * @Date:2020/7/31 9:59 11 | */ 12 | @Slf4j 13 | @Component 14 | public class MqttSubClient { 15 | 16 | public MqttSubClient(MqttPushClient mqttPushClient){ 17 | subScribeDataPublishTopic(); 18 | } 19 | 20 | 21 | private void subScribeDataPublishTopic(){ 22 | //订阅test_queue主题 23 | subscribe("test_queue"); 24 | } 25 | 26 | /** 27 | * 订阅某个主题,qos默认为0 28 | * 29 | * @param topic 30 | */ 31 | public void subscribe(String topic) { 32 | subscribe(topic, 0); 33 | } 34 | 35 | /** 36 | * 订阅某个主题 37 | * 38 | * @param topic 主题名 39 | * @param qos 40 | */ 41 | public void subscribe(String topic, int qos) { 42 | try { 43 | MqttClient client = MqttPushClient.getClient(); 44 | if (client == null) return; 45 | client.subscribe(topic, qos); 46 | log.info("订阅主题:{}",topic); 47 | } catch (MqttException e) { 48 | e.printStackTrace(); 49 | } 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/thinmoo/cloud/mqtt/PushCallback.java: -------------------------------------------------------------------------------- 1 | package com.thinmoo.cloud.mqtt; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; 5 | import org.eclipse.paho.client.mqttv3.MqttCallback; 6 | import org.eclipse.paho.client.mqttv3.MqttMessage; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Component; 9 | 10 | @Slf4j 11 | @Component 12 | public class PushCallback implements MqttCallback { 13 | @Autowired 14 | private MqttConfiguration mqttConfiguration; 15 | 16 | @Override 17 | public void connectionLost(Throwable cause) { // 连接丢失后,一般在这里面进行重连 18 | log.info("连接断开,正在重连"); 19 | MqttPushClient mqttPushClient = mqttConfiguration.getMqttPushClient(); 20 | if (null != mqttPushClient) { 21 | mqttPushClient.connect(mqttConfiguration.getHost(), mqttConfiguration.getClientid(), mqttConfiguration.getUsername(), 22 | mqttConfiguration.getPassword(), mqttConfiguration.getTimeout(), mqttConfiguration.getKeepalive()); 23 | log.info("已重连"); 24 | } 25 | 26 | } 27 | 28 | /** 29 | * 发送消息,消息到达后处理方法 30 | * @param token 31 | */ 32 | @Override 33 | public void deliveryComplete(IMqttDeliveryToken token) { 34 | 35 | } 36 | 37 | /** 38 | * 订阅主题接收到消息处理方法 39 | * @param topic 40 | * @param message 41 | */ 42 | @Override 43 | public void messageArrived(String topic, MqttMessage message) { 44 | // subscribe后得到的消息会执行到这里面,这里在控制台有输出 45 | log.info("接收消息主题 : " + topic); 46 | log.info("接收消息Qos : " + message.getQos()); 47 | log.info("接收消息内容 : " + new String(message.getPayload())); 48 | 49 | } 50 | 51 | } -------------------------------------------------------------------------------- /src/main/java/com/thinmoo/cloud/controller/MqttDemoController.java: -------------------------------------------------------------------------------- 1 | package com.thinmoo.cloud.controller; 2 | 3 | import com.thinmoo.cloud.mqtt.MqttPushClient; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | /** 9 | * @Author:Dong 10 | * @Date:2020/8/26 16:12 11 | */ 12 | @RestController 13 | @RequestMapping("mqttDemo") 14 | public class MqttDemoController { 15 | @Autowired 16 | private MqttPushClient mqttPushClient; 17 | /** 18 | * 通过MqttPushClient类publish方法的发送"这是一条测试消息"到名为"test_queue"的主题,如果需要拿到这条消息, 19 | * 需要在MqttSubClient类的subScribeDataPublishTopic方法进行配置和订阅该主题(这个DEMO已经配置好,并在连接mqtt服务器时就已经订阅), 20 | * 配置完成后 PushCallBack类的messageArrived方法会接收到已订阅主题接收到的消息(订阅主题后可以在该方法中处理接收到的消息) 21 | * 22 | *Send "This is a test message" to the topic named "test_queue" through the publish method of the MqttPushClient class. 23 | * If you need to get this message,Need to configure and subscribe to the topic in the subScribeDataPublishTopic 24 | * method of the MqttSubClient class (this DEMO has been configured and subscribed when connecting to the mqtt server), 25 | * After the configuration is completed, the messageArrived method of the PushCallBack class will receive the message 26 | * received by the subscribed topic (you can process the received message in this method after subscribing to the topic) 27 | */ 28 | @RequestMapping("testPublishMessage1") 29 | public void testPublishMessage() { 30 | mqttPushClient.publish("test_queue","这是一条测试消息"); 31 | } 32 | 33 | @RequestMapping("testPublishMessage2") 34 | public void testPublishMessage2(String message){ 35 | mqttPushClient.publish("test_queue",message); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/thinmoo/cloud/mqtt/MqttConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.thinmoo.cloud.mqtt; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.boot.context.properties.ConfigurationProperties; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.Configuration; 7 | import org.springframework.stereotype.Component; 8 | 9 | /** 10 | * mqtt配置类,获取mqtt连接 11 | */ 12 | @Component 13 | @Configuration 14 | @ConfigurationProperties(MqttConfiguration.PREFIX) 15 | public class MqttConfiguration { 16 | 17 | @Autowired 18 | private MqttPushClient mqttPushClient; 19 | //指定配置文件application-local.properties中的属性名前缀 20 | public static final String PREFIX = "ximo.mqtt"; 21 | private String host; 22 | private String clientId; 23 | private String userName; 24 | private String password; 25 | private String topic; 26 | private int timeout; 27 | private int keepAlive; 28 | 29 | public String getClientid() { 30 | return clientId; 31 | } 32 | 33 | public void setClientid(String clientid) { 34 | this.clientId = clientid; 35 | } 36 | 37 | public String getUsername() { 38 | return userName; 39 | } 40 | 41 | public void setUsername(String username) { 42 | this.userName = username; 43 | } 44 | 45 | public String getPassword() { 46 | return password; 47 | } 48 | 49 | public void setPassword(String password) { 50 | this.password = password; 51 | } 52 | 53 | public String getTopic() { 54 | return topic; 55 | } 56 | 57 | public void setTopic(String topic) { 58 | this.topic = topic; 59 | } 60 | 61 | public int getTimeout() { 62 | return timeout; 63 | } 64 | 65 | public void setTimeout(int timeout) { 66 | this.timeout = timeout; 67 | } 68 | 69 | public int getKeepalive() { 70 | return keepAlive; 71 | } 72 | 73 | public void setKeepalive(int keepalive) { 74 | this.keepAlive = keepalive; 75 | } 76 | 77 | public String getHost() { 78 | return host; 79 | } 80 | 81 | public void setHost(String host) { 82 | this.host = host; 83 | } 84 | 85 | /** 86 | * 连接至mqtt服务器,获取mqtt连接 87 | * @return 88 | */ 89 | @Bean 90 | public MqttPushClient getMqttPushClient() { 91 | //连接至mqtt服务器,获取mqtt连接 92 | mqttPushClient.connect(host, clientId, userName, password, timeout, keepAlive); 93 | //一连接mqtt,就订阅默认需要订阅的主题(如test_queue) 94 | new MqttSubClient(mqttPushClient); 95 | return mqttPushClient; 96 | } 97 | } -------------------------------------------------------------------------------- /src/main/java/com/thinmoo/cloud/mqtt/MqttPushClient.java: -------------------------------------------------------------------------------- 1 | package com.thinmoo.cloud.mqtt; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.eclipse.paho.client.mqttv3.*; 5 | import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Component; 8 | 9 | @Slf4j 10 | @Component 11 | public class MqttPushClient{ 12 | 13 | @Autowired 14 | private PushCallback pushCallback; 15 | 16 | private static MqttClient client; 17 | 18 | 19 | public static void setClient(MqttClient client) { 20 | MqttPushClient.client = client; 21 | } 22 | 23 | public static MqttClient getClient() { 24 | return client; 25 | } 26 | 27 | public void connect(String host, String clientID, String username, String password, int timeout, int keepalive) { 28 | MqttClient client; 29 | try { 30 | client = new MqttClient(host, clientID, new MemoryPersistence()); 31 | MqttConnectOptions options = new MqttConnectOptions(); 32 | options.setCleanSession(true); 33 | options.setUserName(username); 34 | options.setPassword(password.toCharArray()); 35 | options.setConnectionTimeout(timeout); 36 | options.setKeepAliveInterval(keepalive); 37 | MqttPushClient.setClient(client); 38 | try { 39 | //设置回调类 40 | client.setCallback(pushCallback); 41 | //client.connect(options); 42 | IMqttToken iMqttToken = client.connectWithResult(options); 43 | boolean complete = iMqttToken.isComplete(); 44 | log.info("MQTT连接"+(complete?"成功":"失败")); 45 | } catch (Exception e) { 46 | e.printStackTrace(); 47 | } 48 | } catch (Exception e) { 49 | e.printStackTrace(); 50 | } 51 | 52 | } 53 | 54 | /** 55 | * 发布,默认qos为0,非持久化 56 | * 57 | * @param topic 主题名 58 | * @param pushMessage 消息 59 | */ 60 | public void publish(String topic, String pushMessage) { 61 | publish(0, false, topic, pushMessage); 62 | } 63 | 64 | /** 65 | * 发布 66 | * 67 | * @param qos 68 | * @param retained 69 | * @param topic 70 | * @param pushMessage 71 | */ 72 | public void publish(int qos, boolean retained, String topic, String pushMessage) { 73 | MqttMessage message = new MqttMessage(); 74 | message.setQos(qos); 75 | message.setRetained(retained); 76 | message.setPayload(pushMessage.getBytes()); 77 | MqttTopic mTopic = MqttPushClient.getClient().getTopic(topic); 78 | if (null == mTopic) { 79 | log.error("主题不存在:{}",mTopic); 80 | } 81 | try { 82 | mTopic.publish(message); 83 | } catch (Exception e) { 84 | log.error("mqtt发送消息异常:",e); 85 | } 86 | } 87 | 88 | } -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | thinmoo-cloud 12 | 0.0.1-SNAPSHOT 13 | 14 | 15 | 1.8 16 | Greenwich.RELEASE 17 | 18 | 19 | 20 | 21 | 22 | org.projectlombok 23 | lombok 24 | provided 25 | 26 | 27 | 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-test 32 | test 33 | 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-starter-web 38 | 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-starter-cache 43 | 44 | 45 | org.springframework 46 | spring-test 47 | 5.1.6.RELEASE 48 | compile 49 | 50 | 51 | 52 | 53 | org.slf4j 54 | slf4j-api 55 | 1.7.25 56 | 57 | 58 | org.slf4j 59 | slf4j-log4j12 60 | 1.7.25 61 | test 62 | 63 | 64 | org.slf4j 65 | slf4j-simple 66 | 1.7.25 67 | test 68 | 69 | 70 | com.turo 71 | pushy 72 | 0.13.3 73 | 74 | 75 | org.springframework.integration 76 | spring-integration-stream 77 | 78 | 79 | org.springframework.integration 80 | spring-integration-mqtt 81 | 82 | 83 | 84 | 85 | 86 | getui-nexus 87 | http://mvn.gt.igexin.com/nexus/content/repositories/releases/ 88 | 89 | 90 | 91 | 93 | 94 | 95 | ${project.artifactId} 96 | 97 | 98 | org.springframework.boot 99 | spring-boot-maven-plugin 100 | 101 | true 102 | 103 | 104 | 105 | 108 | 109 | 110 | org.apache.maven.plugins 111 | maven-surefire-plugin 112 | 113 | true 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | src/main/resources 122 | 123 | 124 | src/main/resources/lib 125 | BOOT-INF/lib/ 126 | 127 | **/*.jar 128 | 129 | 130 | 131 | src/main/java 132 | 133 | **/*.xml 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | --------------------------------------------------------------------------------