first commit

This commit is contained in:
zc
2025-12-08 10:40:43 +08:00
commit 871ae8be0a
410 changed files with 38212 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
package org.dromara.mica.mqtt.client.solon;
import org.noear.solon.Solon;
import org.noear.solon.annotation.Configuration;
/**
* @author wsq
*/
@Configuration
public class MqttClientApplication {
/**
* 先启动 mica-mqtt-server-spring-boot-example 再启动本项目,进行测试
*/
public static void main(String[] args) {
Solon.start(MqttClientApplication.class, args);
}
}

View File

@@ -0,0 +1,26 @@
package org.dromara.mica.mqtt.client.solon.controller;
import org.dromara.mica.mqtt.client.solon.service.ClientService;
import org.noear.solon.annotation.*;
@Mapping("/mqtt/client")
@Controller
public class ClientController {
@Inject
private ClientService service;
@Post
@Mapping("/publish")
public boolean publish(String body) {
return service.publish(body);
}
@Get
@Mapping("/sub")
public boolean sub() {
return service.sub();
}
}

View File

@@ -0,0 +1,41 @@
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & dreamlu.net).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.mica.mqtt.client.solon.listener;
import lombok.extern.slf4j.Slf4j;
import org.dromara.mica.mqtt.client.solon.event.MqttConnectedEvent;
import org.dromara.mica.mqtt.core.client.MqttClientCreator;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.core.event.EventListener;
/**
* 客户端连接状态监听
*
* @author L.cm
*/
@Slf4j
@Component
public class MqttClientConnectedListener implements EventListener<MqttConnectedEvent> {
@Inject
private MqttClientCreator mqttClientCreator;
@Override
public void onEvent(MqttConnectedEvent mqttConnectedEvent) throws Throwable {
log.info("MqttConnectedEvent:{}", mqttConnectedEvent);
}
}

View File

@@ -0,0 +1,45 @@
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & dreamlu.net).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.mica.mqtt.client.solon.listener;
import lombok.extern.slf4j.Slf4j;
import org.dromara.mica.mqtt.client.solon.event.MqttDisconnectEvent;
import org.dromara.mica.mqtt.core.client.MqttClientCreator;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.core.event.EventListener;
/**
* 客户端连接状态监听
*
* @author L.cm
*/
@Slf4j
@Component
public class MqttClientDisconnectListener implements EventListener<MqttDisconnectEvent> {
@Inject
private MqttClientCreator mqttClientCreator;
@Override
public void onEvent(MqttDisconnectEvent mqttDisconnectEvent) throws Throwable {
log.info("MqttDisconnectEvent:{}", mqttDisconnectEvent);
// 在断线时更新 clientId、username、password只能改这 3 个,不可调用其他方法。
// mqttClientCreator.clientId("newClient" + System.currentTimeMillis())
// .username("newUserName")
// .password("newPassword");
}
}

View File

@@ -0,0 +1,25 @@
package org.dromara.mica.mqtt.client.solon.listener;
import lombok.extern.slf4j.Slf4j;
import org.dromara.mica.mqtt.core.annotation.MqttClientSubscribe;
import org.dromara.mica.mqtt.codec.message.MqttPublishMessage;
import org.dromara.mica.mqtt.core.client.IMqttClientMessageListener;
import org.tio.core.ChannelContext;
import java.nio.charset.StandardCharsets;
/**
* 客户端消息监听的另一种方式
*
* @author L.cm
*/
@Slf4j
@MqttClientSubscribe("${topic1}")
public class MqttClientMessageListener implements IMqttClientMessageListener {
@Override
public void onMessage(ChannelContext context, String topic, MqttPublishMessage message, byte[] payload) {
log.info("MqttClientMessageListener,topic:{} payload:{}", topic, new String(payload, StandardCharsets.UTF_8));
}
}

View File

@@ -0,0 +1,68 @@
package org.dromara.mica.mqtt.client.solon.listener;
import lombok.extern.slf4j.Slf4j;
import org.dromara.mica.mqtt.core.annotation.MqttClientSubscribe;
import org.dromara.mica.mqtt.client.solon.pojo.User;
import org.dromara.mica.mqtt.codec.MqttQoS;
import org.dromara.mica.mqtt.codec.message.MqttPublishMessage;
import org.dromara.mica.mqtt.core.deserialize.MqttJsonDeserializer;
import org.noear.solon.annotation.Component;
import java.nio.charset.StandardCharsets;
import java.util.Map;
/**
* 客户端消息监听
*
* @author L.cm
*/
@Slf4j
@Component
public class MqttClientSubscribeListener {
@MqttClientSubscribe("/test/#")
public void subQos0(String topic, byte[] payload) {
log.info("MqttClientSubscribeListener.subQos0,topic:{} payload:{}", topic, new String(payload, StandardCharsets.UTF_8));
}
@MqttClientSubscribe(value = "/qos1/#", qos = MqttQoS.QOS1)
public void subQos1(String topic, byte[] payload) {
log.info("topic:{} payload:{}", topic, new String(payload, StandardCharsets.UTF_8));
}
@MqttClientSubscribe(
value = "/test/json",
deserialize = MqttJsonDeserializer.class // 2.4.5 开始支持 自定义序列化,默认 json 序列化
)
public void testJson(String topic, MqttPublishMessage message, Map<String, Object> data) {
// solon 插件为 2.4.6 开始支持,支持 2 到 3 个参数,字段类型映射规则(顺序)如下
// String 字符串会默认映射到 topic
// Map<String, String> topicVars 会默认映射到 topic 中的变量解析v2.5.4支持),注意:别跟消息序列化的冲突,消息反序列化不要用 Map<String, String>
// MqttPublishMessage 会默认映射到 原始的消息,可以拿到 mqtt5 的 props 参数
// byte[] 会映射到 mqtt 消息内容 payload
// ByteBuffer 会映射到 mqtt 消息内容 payload
// 其他类型会走序列化,确保消息能够序列化,默认为 json 序列化
log.info("topic:{} json data:{}", topic, data);
}
@MqttClientSubscribe(value = "/test/object")
public void testJson(String topic, MqttPublishMessage message, User<User> data) {
log.info("topic:{} json data:{}", topic, data);
}
/**
* 订阅,参数为可选,但是参数数量必须大于 2
*
* @param topic topic 参数,可选参数
* @param topicVars 订阅 topic 模板 ${productKey} 中的变量解析v2.5.4支持),可选参数,注意:类型必须为 Map<String, String>
* @param payload 消息内容
*/
@MqttClientSubscribe("/sys/${productKey}/${deviceName}/thing/sub/register")
public void thingSubRegister(String topic, Map<String, String> topicVars, byte[] payload) {
// 1.3.8 开始支持,@MqttClientSubscribe 注解支持 ${} 变量替换,会默认替换成 +
// 注意mica-mqtt 会先从 Spring boot 配置中替换参数 ${},如果存在配置会优先被替换。
log.info("topic:{} payload:{}", topic, new String(payload, StandardCharsets.UTF_8));
}
}

View File

@@ -0,0 +1,9 @@
package org.dromara.mica.mqtt.client.solon.pojo;
import lombok.Data;
@Data
public class User<T> {
private String name;
private T girlfriend;
}

View File

@@ -0,0 +1,32 @@
package org.dromara.mica.mqtt.client.solon.service;
import lombok.extern.slf4j.Slf4j;
import org.dromara.mica.mqtt.client.solon.MqttClientTemplate;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import java.nio.charset.StandardCharsets;
/**
* @author wsq
*/
@Slf4j
@Component
public class ClientService {
@Inject
private MqttClientTemplate client;
public boolean publish(String body) {
client.publish("/test/client", body.getBytes(StandardCharsets.UTF_8));
return true;
}
public boolean sub() {
client.subQos0("/test/#", (context, topic, message, payload) -> {
log.info("{}\t{}", topic, new String(payload, StandardCharsets.UTF_8));
});
return true;
}
}

View File

@@ -0,0 +1,36 @@
server:
port: 30303
# solon 配置
solon:
logging:
appender:
console:
level: INFO
# mqtt-client 配置
mqtt:
client:
enabled: true # 是否开启客户端默认true
ip: 127.0.0.1 # 连接的服务端 ip 默认127.0.0.1
port: 1883 # 端口默认1883
name: Mica-Mqtt-Client # 名称默认Mica-Mqtt-Client
clientId: 000001 # 客户端Id非常重要一般为设备 sn不可重复
username: mica # 认证的用户名注意2.5.x 开始将 user-name 改成了 username
password: mica # 认证的密码
timeout: 5 # 超时时间单位默认5秒
reconnect: true # 是否重连默认true
re-interval: 5000 # 重连时间,默认 5000 毫秒
version: mqtt_3_1_1 # mqtt 协议版本,可选 MQTT_3_1、mqtt_3_1_1、mqtt_5默认mqtt_3_1_1
read-buffer-size: 8KB # 接收数据的 buffer size默认8k
max-bytes-in-message: 10MB # 消息解析最大 bytes 长度默认10M
keep-alive-secs: 60 # keep-alive 时间,单位:秒
heartbeat-mode: LAST_REQ # 心跳模式支持最后发送或接收心跳时间来计算心跳默认最后发送心跳的时间。2.4.3 开始支持)
heartbeat-timeout-strategy: PING # 心跳超时策略,支持发送 PING 和 CLOSE 断开连接,默认:最大努力发送 PING。2.4.3 开始支持)
clean-start: true # session 保留 2.5.x 使用 clean-start老版本用 clean-session默认true
ssl:
enabled: false # 是否开启 ssl 认证2.1.0 开始支持双向认证
keystore-path: # 可选参数ssl 双向认证 keystore 目录,支持 classpath:/ 路径。
keystore-pass: # 可选参数ssl 双向认证 keystore 密码
truststore-path: # 可选参数ssl 双向认证 truststore 目录,支持 classpath:/ 路径。
truststore-pass: # 可选参数ssl 双向认证 truststore 密码
topic1: /test/#