惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

A
About on SuperTechFans
C
Cybersecurity and Infrastructure Security Agency CISA
N
News and Events Feed by Topic
C
Cisco Blogs
Cisco Talos Blog
Cisco Talos Blog
A
Arctic Wolf
Scott Helme
Scott Helme
P
Palo Alto Networks Blog
S
Schneier on Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Tor Project blog
量子位
G
Google Developers Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
B
Blog RSS Feed
NISL@THU
NISL@THU
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
AWS News Blog
AWS News Blog
爱范儿
爱范儿
Last Week in AI
Last Week in AI
Y
Y Combinator Blog
L
LINUX DO - 最新话题
Security Archives - TechRepublic
Security Archives - TechRepublic
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Secure Thoughts
Cloudbric
Cloudbric
aimingoo的专栏
aimingoo的专栏
L
Lohrmann on Cybersecurity
TaoSecurity Blog
TaoSecurity Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hacker News: Ask HN
Hacker News: Ask HN
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The GitHub Blog
The GitHub Blog
有赞技术团队
有赞技术团队
S
Security @ Cisco Blogs
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cyber Attacks, Cyber Crime and Cyber Security
G
GRAHAM CLULEY
P
Proofpoint News Feed
V
V2EX
Martin Fowler
Martin Fowler
C
CERT Recently Published Vulnerability Notes
Attack and Defense Labs
Attack and Defense Labs
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Cloudflare Blog
SecWiki News
SecWiki News
罗磊的独立博客
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
小众软件
小众软件
The Last Watchdog
The Last Watchdog

博客园 - cn2025

kubeSphere发布ruoyi-web前端 20260711 kubeSphere发布ruoyi-pro后端 k8s集群-kubeShpere 20260710 安装Helm 20260710 k8s-portainer docker 镜像查询 k8集群一键重置 docker run OceanBase spring ai alibaba doc AI2.0 【多模态】 20260615 世界只有一个墨脱 AI2.0 【Mcp-client】 20260611 AI2.0 【Mcp-server】 20260611 GoLand 配Go SDK AI2.0 【redis向量-Rag】 问答顾问器QuestionAnswerAdvisor 20260608 AI2.0 【redis向量】redis-stack 20260608 AI2.0 【Embedding】嵌入模型 20260606 AI 工具 AI2.0 Tool调用20260604 AI2.0 对话chatMemory20260601 AI2.0 模型输出结构化【POJO、Record、List、Map 】20260530 AI2.0 Prompt【模板、流式 API、系统提示词】 20260529 AI2.0 自定义Advisor 20260528 AI2.0 ollama 20260528 AI2.0 dashscope-openai 20260527 ps 配Claude 20260527 spring-ai-alibaba-agent 260526 票务 Tool+Spring Security【动态tooLs:toolCallbacks】 20260525 spring-ai-alibaba-agent 260525 spring-ai-alibaba-agent 260523 票务 Tool接口 / 方法 / 参数【无意义、可读、业务化、参数数量过多】 20260521 票务 Tool参数幻觉 20260521 spring-ai-alibaba-agent 260521 ToolTemperature 温度过低,AI推算缺失自由度20260520 spring-ai-alibaba-agent 260520 票务Tool 20260519 票务助手 -多模型20260518 结构化输出 -原理【structuredconverter】20260518 spring-ai-alibaba-agent 260518 Dify 添加Ollma模型qwen2:0.5b 应用 20260516 docker 部【dify-api】/【dify-web】 20260514 Chatclient 结构化输出20260514 spring-ai-alibaba-agent 260514 Chatmemory 多层(近、中、长期)20260513 ChatmemoryRedis 历史对话存【REDIS】20260512 ChatmemoryJdbc 历史对话存【JDBC】20260511 spring-ai-alibaba-agent 260511 ChatmemoryConversationId 多用户对话记忆 20260508 spring-ai-alibaba-agent 260508 ChatmemoryMax 历史对话长度20260507 2026SE Chatmemory 对话记忆20260506 spring-ai-alibaba-agent 260506 ChatClientPrompt 自定义拦截器【ReReadingAdvisor】重读提示词 20260430 docker-apache/kafka:4.1.2部暑 集群20260423 ChatClientPrompt Template.st20260428 ChatClientPrompt Template20260427 sb-KafkaListener 20260425 docker-apache/kafka:4.1.2部暑 20260425 dashscope-sb ChatClientPrompt20260425 SBAI-MultiPlatformAndModel 20260424 PlatformModel SB-ChatClient-DeepSeekDashScopeOllamaModel 20260424 dashscope-sb ChatClient20260420 ollama-sb 多态 图转文 20260418 【gemma3:4b 解析慢】 dashscope-sb 多模态(图片、语音识别) 文生视频 ollama-sb 20260414 dashscope-sb 阿里百炼-文生图20260413 dashscope-sb20260413 dashscope-sb20260411 docker-zabbix 20260410 deepseek-sb20260408 MQTT20260403 spring-ai-alibaba-agent 260403 spring-ai-alibaba-agent 260401 docker安装 EMQX spring-ai-alibaba-agent 260331 MQTT 20260331 spring-ai-alibaba-agent 三大 Java 生态 AI Agent 框架 CentOS7-静态 IP centos-stream10 安装 百炼-工作流-sb sb-flink1.13.1-jdk8-分隔字符串 20260125
MqttTest 20260401
cn2025 · 2026-04-01 · via 博客园 - cn2025

1、docker   emqx-47 服务端

image

2、MqttTest实例

2.1   pom

<properties>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>3.0.2</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.2.5</version>
</dependency>
</dependencies>

2.1   MqttTest

package com.sb.mqtt;

import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

public class MqttTest {

public static void main(String[] args) {
// 1. 基础配置
String subTopic = "testtopic/#"; // 订阅主题(通配符,匹配 testtopic/1、testtopic/2 等)
String pubTopic = "testtopic/1"; // 发布主题
String content = "Hello World 2026"; // 消息内容
int qos = 2; // 服务质量:0最多一次,1至少一次,2 exactly once
String broker = "tcp://192.168.91.165:9610";// MQTT Broker 地址
String clientId = "admin_" + System.currentTimeMillis(); // 客户端ID(必须全局唯一) admin_ emqx_test_
MemoryPersistence persistence = new MemoryPersistence(); // 内存持久化(生产环境可改用文件持久化)

try {
// 2. 创建 MQTT 客户端实例
MqttClient client = new MqttClient(broker, clientId, persistence);

// 3. 配置连接选项
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setUserName("admin"); // 用户名 emqx_test admin
connOpts.setPassword("public".toCharArray()); // 密码 emqx_test_password public
connOpts.setCleanSession(true); // 清除会话:true=不保留离线消息,false=保留
connOpts.setConnectionTimeout(10); // 连接超时时间(秒)
connOpts.setKeepAliveInterval(60); // 心跳间隔(秒)

// 4. 设置回调函数(核心:处理连接丢失、消息接收、消息送达)
client.setCallback(new MqttCallback() {
/**
* 连接丢失时触发(如网络断开、Broker 宕机)
*/
@Override
public void connectionLost(Throwable cause) {
System.out.println("连接断开,可做重连:" + cause.getMessage());
// 生产环境:在此处实现自动重连逻辑
}

/**
* 订阅主题收到消息时触发
*/
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
System.out.println("=====================================");
System.out.println("接收消息主题: " + topic);
System.out.println("接收消息Qos: " + message.getQos());
System.out.println("接收消息内容: " + new String(message.getPayload()));
System.out.println("=====================================");
}

/**
* 消息发布完成时触发(仅 QoS 1/2 有效)
*/
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
System.out.println("deliveryComplete---------" + token.isComplete());
}
});

// 5. 建立连接
System.out.println("Connecting to broker: " + broker);
client.connect(connOpts);
System.out.println("Connected");

// 6. 订阅主题
System.out.println("Subscribing to topic: " + subTopic);
client.subscribe(subTopic, qos);
System.out.println("Subscribed");

// 7. 发布消息
System.out.println("Publishing message: " + content);
MqttMessage message = new MqttMessage(content.getBytes());
message.setQos(qos);
message.setRetained(false); // 保留消息:false=不持久化,true=Broker 存储,新订阅者立即收到
client.publish(pubTopic, message);
System.out.println("Message published");

// 8. 等待消息接收(测试用,生产环境不要主动断开)
Thread.sleep(2000);

// 9. 断开连接、关闭资源
client.disconnect();
System.out.println("Disconnected");
client.close();
System.exit(0);

} catch (MqttException me) {
// 异常处理
System.out.println("reason " + me.getReasonCode());
System.out.println("msg " + me.getMessage());
System.out.println("loc " + me.getLocalizedMessage());
System.out.println("cause " + me.getCause());
System.out.println("excep " + me);
me.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

2.2   MqttTest  输出

Connecting to broker: tcp://192.168.91.165:9610
Connected
Subscribing to topic: testtopic/#
Subscribed
Publishing message: Hello World 2026
Message published
deliveryComplete---------true
=====================================
接收消息主题: testtopic/1
接收消息Qos: 2
接收消息内容: Hello World 2026
=====================================
Disconnected