├── .gitignore ├── LICENSE ├── README.md ├── mqtt-spring-boot-autoconfigure ├── .gitignore ├── mqtt-spring-boot-autoconfigure.iml ├── pom.xml └── src │ └── main │ ├── java │ └── io │ │ └── github │ │ └── brandonbai │ │ └── mqtt │ │ ├── MqttAutoConfiguration.java │ │ ├── MqttMessageClient.java │ │ └── MqttProperties.java │ └── resources │ └── META-INF │ └── spring.factories ├── mqtt-spring-boot-sample ├── .gitignore ├── mqtt-spring-boot-sample.iml ├── pom.xml └── src │ └── main │ ├── java │ └── io │ │ └── github │ │ └── brandonbai │ │ └── sample │ │ └── mqtt │ │ ├── MqttSpringBootStarterSampleApplication.java │ │ └── controller │ │ └── DemoController.java │ └── resources │ └── application.yml ├── mqtt-spring-boot-starter ├── .gitignore ├── mqtt-spring-boot-starter.iml ├── pom.xml └── src │ └── main │ └── resources │ └── META-INF │ └── spring.providers └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | 3 | target/ 4 | 5 | .iml -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Feihu Ji 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MQTT integration with Spring Boot 2 | 3 | mqtt-spring-boot-starter will help you use mqtt with Spring Boot. 4 | 5 | 6 | ## Summary 7 | 8 | - [Maven](#maven) 9 | - [Configuration](#configuration) 10 | - [Use](#use) 11 | - [Samples](#samples) 12 | 13 | ## Maven 14 | ```xml 15 | 16 | io.github.brandonbai 17 | mqtt-spring-boot-starter 18 | 1.0-SNAPSHOT 19 | 20 | ``` 21 | 22 | ## Configuration 23 | `{your applicaton.yml/applicaton.properties}` 24 | 25 | ```yml 26 | mqtt: 27 | username: mqttPubClient 28 | password: 123456 29 | cleanSession: false 30 | serverURIs: 31 | - tcp://localhost:1883 32 | async: true 33 | completionTimeout: 20000 34 | keepAliveInterval: 30 35 | clientId: mqttPubClient 36 | defaultQos: 1 37 | ``` 38 | 39 | ## Use 40 | 41 | ```java 42 | public class YourClass { 43 | @Resource 44 | private MqttMessageClient mqttMessageClient; 45 | 46 | public void yourMethod() { 47 | mqttMessageClient.sendMessage("topic", "content"); 48 | } 49 | } 50 | ``` 51 | 52 | ## Samples 53 | 54 | - [Mqtt + SpringMvc + Spring](./samples/mqtt-spring-sample) 55 | 56 | - [mqtt + Spring Boot](./samples/mqtt-spring-boot-sample) 57 | 58 | - [mqtt-spring-boot-starter](./samples/mqtt-spring-boot-starter-sample/) 59 | -------------------------------------------------------------------------------- /mqtt-spring-boot-autoconfigure/.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | 3 | target/ 4 | 5 | .iml -------------------------------------------------------------------------------- /mqtt-spring-boot-autoconfigure/mqtt-spring-boot-autoconfigure.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /mqtt-spring-boot-autoconfigure/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | 8 | io.github.brandonbai 9 | mqtt-spring-boot 10 | 1.0.0 11 | 12 | mqtt-spring-boot-autoconfigure 13 | 1.0.0 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-autoconfigure-processor 19 | true 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-configuration-processor 24 | true 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-autoconfigure 29 | true 30 | 31 | 32 | org.projectlombok 33 | lombok 34 | 1.16.20 35 | 36 | 37 | org.slf4j 38 | slf4j-api 39 | true 40 | 41 | 42 | org.springframework.boot 43 | spring-boot 44 | true 45 | 46 | 47 | org.eclipse.paho 48 | org.eclipse.paho.client.mqttv3 49 | 1.2.1 50 | 51 | 52 | org.springframework.integration 53 | spring-integration-core 54 | 5.2.0.RELEASE 55 | 56 | 57 | org.springframework.integration 58 | spring-integration-mqtt 59 | 5.1.1.RELEASE 60 | 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /mqtt-spring-boot-autoconfigure/src/main/java/io/github/brandonbai/mqtt/MqttAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | package io.github.brandonbai.mqtt; 2 | 3 | import org.eclipse.paho.client.mqttv3.MqttConnectOptions; 4 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.Configuration; 7 | import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory; 8 | 9 | import javax.annotation.Resource; 10 | 11 | /** 12 | * mqtt auto configuration 13 | */ 14 | @Configuration 15 | @EnableConfigurationProperties(MqttProperties.class) 16 | public class MqttAutoConfiguration { 17 | 18 | @Resource 19 | private MqttProperties mqttProperties; 20 | 21 | @Bean 22 | public DefaultMqttPahoClientFactory clientFactory() { 23 | 24 | MqttConnectOptions connectOptions = new MqttConnectOptions(); 25 | String username = mqttProperties.getUsername(); 26 | String password = mqttProperties.getPassword(); 27 | if(username != null) { 28 | connectOptions.setUserName(username); 29 | } 30 | if (password != null) { 31 | connectOptions.setPassword(password.toCharArray()); 32 | } 33 | String[] serverURIs = mqttProperties.getServerURIs(); 34 | if (serverURIs == null || serverURIs.length == 0) { 35 | throw new NullPointerException("serverURIs can not be null"); 36 | } 37 | connectOptions.setCleanSession(mqttProperties.getCleanSession()); 38 | connectOptions.setKeepAliveInterval(mqttProperties.getKeepAliveInterval()); 39 | connectOptions.setServerURIs(serverURIs); 40 | 41 | DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory(); 42 | factory.setConnectionOptions(connectOptions); 43 | 44 | return factory; 45 | } 46 | 47 | @Bean 48 | public MqttMessageClient mqttMessageClient() { 49 | 50 | MqttMessageClient client = new MqttMessageClient(mqttProperties.getClientId(), clientFactory()); 51 | client.setAsync(mqttProperties.getAsync()); 52 | client.setDefaultQos(mqttProperties.getDefaultQos()); 53 | client.setCompletionTimeout(mqttProperties.getCompletionTimeout()); 54 | 55 | return client; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /mqtt-spring-boot-autoconfigure/src/main/java/io/github/brandonbai/mqtt/MqttMessageClient.java: -------------------------------------------------------------------------------- 1 | package io.github.brandonbai.mqtt; 2 | 3 | import org.springframework.integration.mqtt.core.MqttPahoClientFactory; 4 | import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler; 5 | import org.springframework.integration.mqtt.support.MqttHeaders; 6 | import org.springframework.messaging.Message; 7 | import org.springframework.messaging.support.MessageBuilder; 8 | 9 | public class MqttMessageClient extends MqttPahoMessageHandler { 10 | 11 | public MqttMessageClient(String clientId, MqttPahoClientFactory clientFactory) { 12 | super(clientId, clientFactory); 13 | } 14 | 15 | /** 16 | * send mqtt message 17 | * 18 | * @param topic mqtt topic 19 | * @param content mqtt payload 20 | */ 21 | public void sendMessage(String topic, String content) { 22 | 23 | Message messages = MessageBuilder.withPayload(content).setHeader(MqttHeaders.TOPIC, topic).build(); 24 | this.handleMessage(messages); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /mqtt-spring-boot-autoconfigure/src/main/java/io/github/brandonbai/mqtt/MqttProperties.java: -------------------------------------------------------------------------------- 1 | package io.github.brandonbai.mqtt; 2 | 3 | import lombok.Data; 4 | import org.springframework.boot.context.properties.ConfigurationProperties; 5 | 6 | @Data 7 | @ConfigurationProperties(prefix = "mqtt") 8 | public class MqttProperties { 9 | 10 | /** 11 | * 用户名 12 | */ 13 | private String username; 14 | /** 15 | * 密码 16 | */ 17 | private String password; 18 | /** 19 | * 是否清除会话 20 | */ 21 | private Boolean cleanSession = false; 22 | /** 23 | * 服务端url 24 | */ 25 | private String[] serverURIs; 26 | /** 27 | * 是否异步发送 28 | */ 29 | private Boolean async = true; 30 | /** 31 | * 超时时间 32 | */ 33 | private int completionTimeout = 20000; 34 | /** 35 | * 心跳 36 | */ 37 | private int keepAliveInterval = 30; 38 | /** 39 | * 客户端id 40 | */ 41 | private String clientId = "mqttPubClient"; 42 | /** 43 | * 默认的消息服务质量 44 | */ 45 | private int defaultQos = 1; 46 | } 47 | -------------------------------------------------------------------------------- /mqtt-spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | # auto config 2 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ 3 | io.github.brandonbai.mqtt.MqttAutoConfiguration -------------------------------------------------------------------------------- /mqtt-spring-boot-sample/.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | 3 | target/ 4 | 5 | .iml -------------------------------------------------------------------------------- /mqtt-spring-boot-sample/mqtt-spring-boot-sample.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /mqtt-spring-boot-sample/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | 8 | org.springframework.boot 9 | spring-boot-starter-parent 10 | 2.1.1.RELEASE 11 | 12 | 13 | 14 | io.github.brandonbai 15 | mqtt-spring-boot-sample 16 | 1.0.0 17 | 18 | 19 | 1.8 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | io.github.brandonbai 29 | mqtt-spring-boot-starter 30 | 1.0.0 31 | 32 | 33 | -------------------------------------------------------------------------------- /mqtt-spring-boot-sample/src/main/java/io/github/brandonbai/sample/mqtt/MqttSpringBootStarterSampleApplication.java: -------------------------------------------------------------------------------- 1 | package io.github.brandonbai.sample.mqtt; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class MqttSpringBootStarterSampleApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(MqttSpringBootStarterSampleApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /mqtt-spring-boot-sample/src/main/java/io/github/brandonbai/sample/mqtt/controller/DemoController.java: -------------------------------------------------------------------------------- 1 | package io.github.brandonbai.sample.mqtt.controller; 2 | 3 | import io.github.brandonbai.mqtt.MqttMessageClient; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | import javax.annotation.Resource; 8 | 9 | @RestController 10 | @RequestMapping("/mqtt") 11 | public class DemoController { 12 | 13 | @Resource 14 | private MqttMessageClient mqttMessageClient; 15 | 16 | @RequestMapping("/send") 17 | public String send(String topic, String content) { 18 | 19 | mqttMessageClient.sendMessage(topic, content); 20 | return "ok"; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /mqtt-spring-boot-sample/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | mqtt: 2 | serverURIs: 3 | - tcp://iot.eclipse.org:1883 -------------------------------------------------------------------------------- /mqtt-spring-boot-starter/.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | 3 | target/ 4 | 5 | .iml -------------------------------------------------------------------------------- /mqtt-spring-boot-starter/mqtt-spring-boot-starter.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /mqtt-spring-boot-starter/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | 8 | io.github.brandonbai 9 | mqtt-spring-boot 10 | 1.0.0 11 | 12 | mqtt-spring-boot-starter 13 | 1.0.0 14 | 15 | 16 | 17 | io.github.brandonbai 18 | mqtt-spring-boot-autoconfigure 19 | 1.0.0 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /mqtt-spring-boot-starter/src/main/resources/META-INF/spring.providers: -------------------------------------------------------------------------------- 1 | providers: mqtt-spring-boot-autoconfigure -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | io.github.brandonbai 8 | mqtt-spring-boot 9 | 1.0.0 10 | pom 11 | 12 | 13 | 2.1.1.RELEASE 14 | 15 | 16 | 17 | mqtt-spring-boot-autoconfigure 18 | mqtt-spring-boot-starter 19 | 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-dependencies 26 | ${spring-boot.version} 27 | pom 28 | import 29 | 30 | 31 | 32 | 33 | 34 | 35 | --------------------------------------------------------------------------------