SpringBoot整合MQTT
使用java程式碼通過MQTT完成釋出訂閱操作
pom 匯入依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hzx</groupId>
<artifactId>mqtt_test_02</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-integration</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-stream</artifactId>
<version>5.4.1</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-mqtt</artifactId>
<version>5.4.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.12</version>
</dependency>
</dependencies>
</project>
yml檔案中配置
spring:
mqtt:
username: admin
password: public
host-url: tcp://127.0.0.1:1883
client-id: test
default-topic: test
timeout: 100
keepalive: 100
獲取配置
package com.mqtt.common;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties("spring.mqtt")
@Setter
@Getter
public class MqttConfig {
@Autowired
private MqttPushClient mqttPushClient;
private String username;
private String password;
private String hostUrl;
private String clientId;
private String defaultTopic;
private int timeout;
private int keepalive;
@Bean
public MqttPushClient getMqttPushClient(){
mqttPushClient.connect(hostUrl,clientId,username,password,timeout,keepalive);
mqttPushClient.subscribe("test/#",0);
return mqttPushClient;
}
}
MQTT推送客戶端
package com.mqtt.common;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class MqttPushClient {
private static final Logger logger = LoggerFactory.getLogger(MqttPushClient.class);
@Autowired
private PushCallback pushCallback;
private static MqttClient client;
private static MqttClient getClient() {
return client;
}
private static void setClient(MqttClient client) {
MqttPushClient.client = client;
}
/**
* 客戶端連線
*
* @param host ip+埠
* @param clientId
* @param username
* @param password
* @param timeout
* @param keepalive 保留數
*/
public void connect(String host, String clientId, String username, String password, int timeout, int keepalive) {
MqttClient client;
try {
client = new MqttClient(host, clientId, new MemoryPersistence());
MqttConnectOptions options = new MqttConnectOptions();
options.setCleanSession(true);
options.setUserName(username);
options.setPassword(password.toCharArray());
options.setConnectionTimeout(timeout);
options.setKeepAliveInterval(keepalive);
MqttPushClient.setClient(client);
try {
client.setCallback(pushCallback);
client.connect(options);
} catch (Exception e) {
e.printStackTrace();
}
} catch (MqttException e) {
e.printStackTrace();
}
}
/**
* 釋出
*
* @param qos 連線方式
* @param retained 是否保留
* @param topic 主題
* @param pushMessage 訊息體
*/
public void publish(int qos, boolean retained, String topic, String pushMessage) {
MqttMessage message = new MqttMessage();
message.setQos(qos);
message.setRetained(retained);
message.setPayload(pushMessage.getBytes());
MqttTopic mqttTopic = MqttPushClient.getClient().getTopic(topic);
if (mqttTopic == null) {
logger.error("topic not exist");
}
MqttDeliveryToken token;
try {
token = mqttTopic.publish(message);
token.waitForCompletion();
} catch (MqttPersistenceException e) {
e.printStackTrace();
} catch (MqttException e) {
e.printStackTrace();
}
}
/**
* 訂閱某個主題
* @param topic
* @param qos
*/
public void subscribe(String topic,int qos){
logger.info("開始訂閱主題"+topic);
try {
MqttPushClient.getClient().subscribe(topic, qos);
} catch (MqttException e) {
e.printStackTrace();
}
}
}
監聽類
package com.mqtt.common;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class PushCallback implements MqttCallback {
private static final Logger logger = LoggerFactory.getLogger(MqttPushClient.class);
@Autowired
private MqttConfig mqttConfig;
private static MqttClient client;
@Override
public void connectionLost(Throwable throwable) {
// 連線丟失後,在此處重連
logger.info("連線斷開,可以重連");
if (client == null || !client.isConnected()) {
mqttConfig.getMqttPushClient();
}
}
@Override
public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
// subscribe後得到的訊息會執行到這裡面
logger.info("接收訊息主題:"+ topic);
logger.info("接收訊息QoS:" + mqttMessage.getQos());
logger.info("接收訊息內容:" + new String(mqttMessage.getPayload()));
}
@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
logger.info("deliveryCompplete-------------"+iMqttDeliveryToken.isComplete());
}
}
Controller返回值格式
package com.mqtt.common;
import org.springframework.http.HttpStatus;
import java.util.HashMap;
import java.util.Map;
public class RUtils extends HashMap<String,Object> {
private static final long serialVersionUID = 1L;
public RUtils(){
put("code",200);
put("msg","success");
}
public static RUtils error(HttpStatus code, String msg){
RUtils r = new RUtils();
r.put("code",code);
r.put("msg",msg);
return r;
}
public static RUtils error(int code,String msg,String data){
RUtils r = new RUtils();
r.put("code",code);
r.put("msg",msg);
r.put("data",data);
return r;
}
public static RUtils ok(String msg){
RUtils r = new RUtils();
r.put("msg",msg);
return r;
}
public static RUtils ok(Map<String,Object> map){
RUtils r = new RUtils();
r.putAll(map);
return r;
}
public static RUtils error(){
return error(HttpStatus.INTERNAL_SERVER_ERROR,"未知異常");
}
public static RUtils error(String msg){
return error(HttpStatus.INTERNAL_SERVER_ERROR,msg);
}
}
controller測試
@RestController
@RequestMapping("/")
public class TestController {
@Autowired
private MqttPushClient mqttPushClient;
@GetMapping(value = "/publishTopic")
public RUtils publishTopic(){
mqttPushClient.publish(0,false,"test/test","測試釋出訊息");
return RUtils.ok("釋出成功");
}
}
測試結果
訪問http://localhost:18083/#/clients
有新的客戶端連線
在MQTT中,在主題test/test中釋出訊息
在java控制檯接收到訊息
在MQTTX中,在主題test/te中釋出訊息
在java控制檯接收訊息
相關文章
- MQTT(EMQX) - SpringBoot 整合MQTT 連線池 Demo - 附原始碼 + 線上客服聊天架構圖MQQTSpring Boot原始碼架構
- SpringBoot 結合官網對MQTT訊息佇列整合記錄Spring BootMQQT佇列
- SpringBoot(19)---SpringBoot整合ApolloSpring Boot
- SpringBoot(17)---SpringBoot整合RocketMQSpring BootMQ
- SpringBoot(十六)_springboot整合JasperReSpring Boot
- SpringBoot整合系列-整合JPASpring Boot
- SpringBoot 整合 rabbitmqSpring BootMQ
- SpringBoot 整合 apolloSpring Boot
- SpringBoot 整合 elkSpring Boot
- SpringBoot 整合 elasticsearchSpring BootElasticsearch
- SpringBoot整合elasticsearchSpring BootElasticsearch
- RocketMQ整合SpringBootMQSpring Boot
- springboot整合redis?Spring BootRedis
- SpringBoot整合MinIOSpring Boot
- Springboot整合pagehelperSpring Boot
- ElasticSearch 整合 SpringBootElasticsearchSpring Boot
- springboot 整合LogBackSpring Boot
- SpringBoot整合ESSpring Boot
- springBoot 整合 mybatisSpring BootMyBatis
- SpringBoot整合NacosSpring Boot
- SpringBoot 整合 RedisSpring BootRedis
- SpringBoot整合DubboSpring Boot
- springboot 整合jeagerSpring Boot
- 【SpringBoot】整合RedisSpring BootRedis
- Springboot整合MybatisSpring BootMyBatis
- SpringBoot整合SwaggerSpring BootSwagger
- SpringBoot 整合 dockerSpring BootDocker
- flowable 整合 springbootSpring Boot
- Springboot整合RabbitMQSpring BootMQ
- springBoot整合flowableSpring Boot
- Springboot整合MyabitsSpring Boot
- SpringBoot整合RedisSpring BootRedis
- springBoot整合thymeleafSpring Boot
- springboot整合druidSpring BootUI
- springboot對接mqtt出現的問題Spring BootMQQT
- SpringBoot整合系列–整合MyBatis-plusSpring BootMyBatis
- SpringBoot整合系列-整合H2Spring Boot
- SpringSession系列-整合SpringBootGseSessionSpring Boot