├── README.md
├── modules
├── mqtt.customer
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── indi
│ │ │ └── zqc
│ │ │ └── mqtt
│ │ │ └── customer
│ │ │ ├── Application.java
│ │ │ └── configuration
│ │ │ └── AppConfiguration.java
│ │ └── resources
│ │ └── application.properties
└── mqtt.producer
│ ├── pom.xml
│ └── src
│ └── main
│ ├── java
│ └── indi
│ │ └── zqc
│ │ └── mqtt
│ │ └── producer
│ │ ├── Application.java
│ │ ├── configuration
│ │ └── AppConfiguration.java
│ │ └── handler
│ │ └── MqttMessageGateway.java
│ └── resources
│ └── application.properties
└── pom.xml
/README.md:
--------------------------------------------------------------------------------
1 | # springboot集成mqtt
2 |
3 | #### MQTT生产端和消费端的配置,实现消息发送与接收
4 | #### 使用rabbitmq做为mqtt服务器
--------------------------------------------------------------------------------
/modules/mqtt.customer/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | mqtt.parent
7 | indi.zqc
8 | 0.0.1
9 | ../../pom.xml
10 |
11 | 4.0.0
12 |
13 | mqtt.customer
14 | 0.0.1
15 |
16 |
17 |
--------------------------------------------------------------------------------
/modules/mqtt.customer/src/main/java/indi/zqc/mqtt/customer/Application.java:
--------------------------------------------------------------------------------
1 | package indi.zqc.mqtt.customer;
2 |
3 | import org.springframework.boot.autoconfigure.SpringBootApplication;
4 | import org.springframework.boot.builder.SpringApplicationBuilder;
5 |
6 | /**
7 | * mqtt消费端
8 | */
9 | @SpringBootApplication
10 | public class Application {
11 |
12 | public static void main(String[] args) {
13 | new SpringApplicationBuilder(Application.class).web(false).run(args);
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/modules/mqtt.customer/src/main/java/indi/zqc/mqtt/customer/configuration/AppConfiguration.java:
--------------------------------------------------------------------------------
1 | package indi.zqc.mqtt.customer.configuration;
2 |
3 | import org.springframework.beans.factory.annotation.Value;
4 | import org.springframework.context.annotation.Bean;
5 | import org.springframework.context.annotation.Configuration;
6 | import org.springframework.integration.annotation.ServiceActivator;
7 | import org.springframework.integration.channel.DirectChannel;
8 | import org.springframework.integration.core.MessageProducer;
9 | import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
10 | import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
11 | import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
12 | import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter;
13 | import org.springframework.messaging.Message;
14 | import org.springframework.messaging.MessageChannel;
15 | import org.springframework.messaging.MessageHandler;
16 | import org.springframework.messaging.MessagingException;
17 |
18 | @Configuration
19 | public class AppConfiguration {
20 |
21 | @Value("${mqtt.broker.serverUri}")
22 | private String serviceUri;
23 |
24 | @Value("${mqtt.broker.username:}")
25 | private String username;
26 |
27 | @Value("${mqtt.broker.password:}")
28 | private String password;
29 |
30 | @Value("${mqtt.topic}")
31 | private String topic;
32 |
33 | @Value("${mqtt.clientId}")
34 | private String clientId;
35 |
36 | @Value("${mqtt.qos}")
37 | private Integer qos;
38 |
39 | @Value("${mqtt.completionTimeout}")
40 | private Integer completionTimeout;
41 |
42 | /**
43 | * 消息通道
44 | * @return
45 | */
46 | @Bean
47 | public MessageChannel mqttInputChannel() {
48 | return new DirectChannel();
49 | }
50 |
51 | /**
52 | * mqtt服务器配置
53 | * @return
54 | */
55 | @Bean
56 | public MqttPahoClientFactory clientFactory() {
57 | DefaultMqttPahoClientFactory clientFactory = new DefaultMqttPahoClientFactory();
58 | clientFactory.setServerURIs(serviceUri);
59 | clientFactory.setUserName(username);
60 | clientFactory.setPassword(password);
61 | return clientFactory;
62 | }
63 |
64 | /**
65 | * 通道适配器
66 | * @param clientFactory
67 | * @param mqttInputChannel
68 | * @return
69 | */
70 | @Bean
71 | public MessageProducer inbound(MqttPahoClientFactory clientFactory, MessageChannel mqttInputChannel) {
72 | //clientId 客户端ID,生产端和消费端的客户端ID需不同,不然服务器会认为是同一个客户端,会接收不到信息
73 | //topic 订阅的主题
74 | MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter(clientId, clientFactory, topic);
75 | //超时时间
76 | adapter.setCompletionTimeout(completionTimeout);
77 | //转换器
78 | adapter.setConverter(new DefaultPahoMessageConverter());
79 | adapter.setQos(qos);
80 | adapter.setOutputChannel(mqttInputChannel);
81 | return adapter;
82 | }
83 |
84 | /**
85 | * 消息处理
86 | * @return
87 | */
88 | @Bean
89 | @ServiceActivator(inputChannel = "mqttInputChannel")
90 | public MessageHandler handler() {
91 | return new MessageHandler() {
92 |
93 | @Override
94 | public void handleMessage(Message> message) throws MessagingException {
95 | System.out.println(message.getPayload());
96 | }
97 |
98 | };
99 | }
100 |
101 | }
102 |
--------------------------------------------------------------------------------
/modules/mqtt.customer/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | #springboot配置
2 | server.port=8080
3 | #mqtt服务器配置
4 | mqtt.broker.serverUri=tcp://localhost:1883
5 | mqtt.broker.username=guest
6 | mqtt.broker.password=guest
7 | mqtt.clientId=mqtt-customer
8 | mqtt.topic=topic1
9 | mqtt.qos=1
10 | mqtt.completionTimeout=5000
--------------------------------------------------------------------------------
/modules/mqtt.producer/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | mqtt.parent
7 | indi.zqc
8 | 0.0.1
9 | ../../pom.xml
10 |
11 | 4.0.0
12 |
13 | mqtt.producer
14 | 0.0.1
15 |
16 |
17 |
--------------------------------------------------------------------------------
/modules/mqtt.producer/src/main/java/indi/zqc/mqtt/producer/Application.java:
--------------------------------------------------------------------------------
1 | package indi.zqc.mqtt.producer;
2 |
3 | import indi.zqc.mqtt.producer.handler.MqttMessageGateway;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.boot.builder.SpringApplicationBuilder;
6 | import org.springframework.context.ConfigurableApplicationContext;
7 | import org.springframework.integration.mqtt.support.MqttHeaders;
8 | import org.springframework.messaging.Message;
9 | import org.springframework.messaging.support.MessageBuilder;
10 |
11 | /**
12 | * mqtt生产端
13 | */
14 | @SpringBootApplication
15 | public class Application {
16 |
17 | public static void main(String[] args) {
18 | ConfigurableApplicationContext context =
19 | new SpringApplicationBuilder(Application.class)
20 | .web(false)
21 | .run(args);
22 | //发送的消息
23 | Message message = MessageBuilder.withPayload("mqtt message")
24 | //发送的主题
25 | .setHeader(MqttHeaders.TOPIC, "topic1").build();
26 | MqttMessageGateway gateway = context.getBean(MqttMessageGateway.class);
27 | gateway.sendMessage(message);
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/modules/mqtt.producer/src/main/java/indi/zqc/mqtt/producer/configuration/AppConfiguration.java:
--------------------------------------------------------------------------------
1 | package indi.zqc.mqtt.producer.configuration;
2 |
3 | import org.springframework.beans.factory.annotation.Value;
4 | import org.springframework.context.annotation.Bean;
5 | import org.springframework.context.annotation.Configuration;
6 | import org.springframework.integration.annotation.ServiceActivator;
7 | import org.springframework.integration.channel.DirectChannel;
8 | import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
9 | import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
10 | import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;
11 | import org.springframework.messaging.MessageChannel;
12 |
13 | @Configuration
14 | public class AppConfiguration {
15 |
16 | @Value("${mqtt.broker.serverUri}")
17 | private String serviceUri;
18 |
19 | @Value("${mqtt.broker.username:}")
20 | private String username;
21 |
22 | @Value("${mqtt.broker.password:}")
23 | private String password;
24 |
25 | @Value("${mqtt.clientId}")
26 | private String clientId;
27 |
28 | /**
29 | * 消息通道
30 | * @return
31 | */
32 | @Bean
33 | public MessageChannel mqttOutboundChannel() {
34 | return new DirectChannel();
35 | }
36 |
37 | /**
38 | * mqtt服务器配置
39 | * @return
40 | */
41 | @Bean
42 | public MqttPahoClientFactory clientFactory() {
43 | DefaultMqttPahoClientFactory clientFactory = new DefaultMqttPahoClientFactory();
44 | clientFactory.setServerURIs(serviceUri);
45 | clientFactory.setUserName(username);
46 | clientFactory.setPassword(password);
47 | return clientFactory;
48 | }
49 |
50 | /**
51 | * 通道适配器
52 | * @param clientFactory
53 | * @return
54 | */
55 | @Bean
56 | @ServiceActivator(inputChannel = "mqttOutboundChannel")
57 | public MqttPahoMessageHandler mqttOutbound(MqttPahoClientFactory clientFactory) {
58 | MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler(clientId, clientFactory);
59 | messageHandler.setAsync(true);
60 | messageHandler.setDefaultQos(1);
61 | messageHandler.setDefaultRetained(false);
62 | messageHandler.setAsyncEvents(false);
63 | return messageHandler;
64 | }
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/modules/mqtt.producer/src/main/java/indi/zqc/mqtt/producer/handler/MqttMessageGateway.java:
--------------------------------------------------------------------------------
1 | package indi.zqc.mqtt.producer.handler;
2 |
3 | import org.springframework.integration.annotation.MessagingGateway;
4 | import org.springframework.messaging.Message;
5 |
6 | /**
7 | * Title : MqttMessageGateway.java
8 | * Package : indi.zqc.mqtt.producer.handler
9 | * Description : 消息发送接口,不需要实现,spring会通过代理的方式实现
10 | * Create on : 2018/1/4 10:58
11 | *
12 | * @author Zhu.Qianchang
13 | * @version v1.0.0
14 | */
15 | @MessagingGateway(defaultRequestChannel = "mqttOutboundChannel")
16 | public interface MqttMessageGateway {
17 |
18 | void sendMessage(Message> message);
19 | }
20 |
--------------------------------------------------------------------------------
/modules/mqtt.producer/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | #springboot配置
2 | server.port=8090
3 | #mqtt配置
4 | mqtt.broker.serverUri=tcp://localhost:1883
5 | mqtt.broker.username=guest
6 | mqtt.broker.password=guest
7 | mqtt.clientId=mqtt-producer
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | indi.zqc
7 | mqtt.parent
8 | pom
9 | 0.0.1
10 |
11 | modules/mqtt.customer
12 | modules/mqtt.producer
13 |
14 |
15 |
16 | org.springframework.boot
17 | spring-boot-starter-parent
18 | 1.5.8.RELEASE
19 |
20 |
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter-integration
25 |
26 |
27 |
28 | org.springframework.integration
29 | spring-integration-stream
30 |
31 |
32 |
33 | org.springframework.integration
34 | spring-integration-mqtt
35 |
36 |
37 |
38 |
39 | 1.8
40 |
41 |
42 |
43 |
44 |
45 | org.springframework.boot
46 | spring-boot-maven-plugin
47 |
48 |
49 |
50 |
51 |
--------------------------------------------------------------------------------