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,78 @@
/*
* 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;
import lombok.extern.slf4j.Slf4j;
import org.dromara.mica.mqtt.codec.message.MqttPublishMessage;
import org.dromara.mica.mqtt.core.client.IMqttClientMessageListener;
import org.dromara.mica.mqtt.core.deserialize.MqttDeserializer;
import org.dromara.mica.mqtt.core.function.ParamValueFunction;
import org.dromara.mica.mqtt.core.util.MethodParamUtil;
import org.tio.core.ChannelContext;
import org.tio.utils.mica.ExceptionUtils;
import java.lang.reflect.Method;
/**
* MqttClientSubscribe 注解订阅监听
*
* @author L.cm
*/
@Slf4j
public class MqttClientSubscribeListener implements IMqttClientMessageListener {
private final Object bean;
private final Method method;
private final ParamValueFunction[] paramValueFunctions;
public MqttClientSubscribeListener(Object bean, Method method, String[] topicTemplates, String[] topicFilters, MqttDeserializer deserializer) {
this.bean = bean;
this.method = method;
this.paramValueFunctions = MethodParamUtil.getParamValueFunctions(method, topicTemplates, topicFilters, deserializer);
}
@Override
public void onMessage(ChannelContext context, String topic, MqttPublishMessage message, byte[] payload) {
// 获取方法参数
Object[] args = getMethodParameters(context, topic, message, payload);
// 方法调用
try {
method.invoke(bean, args);
} catch (Throwable e) {
throw ExceptionUtils.unchecked(e);
}
}
/**
* 获取反射参数
*
* @param context context
* @param topic topic
* @param message message
* @param payload payload
* @return Object array
*/
protected Object[] getMethodParameters(ChannelContext context, String topic, MqttPublishMessage message, byte[] payload) {
int length = paramValueFunctions.length;
Object[] parameters = new Object[length];
for (int i = 0; i < length; i++) {
ParamValueFunction function = paramValueFunctions[i];
parameters[i] = function.getValue(context, topic, message, payload);
}
return parameters;
}
}

View File

@@ -0,0 +1,458 @@
/*
* 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;
import lombok.extern.slf4j.Slf4j;
import org.dromara.mica.mqtt.codec.message.builder.MqttPublishBuilder;
import org.dromara.mica.mqtt.codec.properties.MqttProperties;
import org.dromara.mica.mqtt.codec.MqttQoS;
import org.dromara.mica.mqtt.core.client.*;
import org.tio.client.ClientChannelContext;
import org.tio.client.TioClient;
import org.tio.client.TioClientConfig;
import org.tio.utils.timer.TimerTask;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
/**
* mqtt client 模板
*
* @author wsq冷月宫主
* @author ChangJin Wei (魏昌进)
*/
@Slf4j
public class MqttClientTemplate {
private final MqttClientCreator clientCreator;
private final IMqttClientConnectListener clientConnectListener;
private final MqttClientCustomizer customizer;
private final List<MqttClientSubscription> tempSubscriptionList;
private MqttClient client;
public MqttClientTemplate(MqttClientCreator clientCreator) {
this(clientCreator, null, null);
}
public MqttClientTemplate(MqttClientCreator clientCreator, IMqttClientConnectListener clientConnectListener) {
this(clientCreator, clientConnectListener, null);
}
public MqttClientTemplate(MqttClientCreator clientCreator,
IMqttClientConnectListener clientConnectListener,
MqttClientCustomizer customizer) {
this.clientCreator = clientCreator;
this.clientConnectListener = clientConnectListener;
this.customizer = customizer;
this.tempSubscriptionList = new ArrayList<>();
}
/**
* 订阅
*
* @param topicFilter topicFilter
* @param listener MqttMessageListener
* @return MqttClient
*/
public MqttClient subQos0(String topicFilter, IMqttClientMessageListener listener) {
return client.subscribe(topicFilter, MqttQoS.QOS0, listener);
}
/**
* 订阅
*
* @param topicFilter topicFilter
* @param listener MqttMessageListener
* @return MqttClient
*/
public MqttClient subQos1(String topicFilter, IMqttClientMessageListener listener) {
return client.subscribe(topicFilter, MqttQoS.QOS1, listener);
}
/**
* 订阅
*
* @param topicFilter topicFilter
* @param listener MqttMessageListener
* @return MqttClient
*/
public MqttClient subQos2(String topicFilter, IMqttClientMessageListener listener) {
return client.subscribe(topicFilter, MqttQoS.QOS2, listener);
}
/**
* 订阅
*
* @param mqttQoS MqttQoS
* @param topicFilter topicFilter
* @param listener MqttMessageListener
* @return MqttClient
*/
public MqttClient subscribe(MqttQoS mqttQoS, String topicFilter, IMqttClientMessageListener listener) {
return client.subscribe(mqttQoS, topicFilter, listener);
}
/**
* 订阅
*
* @param mqttQoS MqttQoS
* @param topicFilter topicFilter
* @param listener MqttMessageListener
* @return MqttClient
*/
public MqttClient subscribe(String topicFilter, MqttQoS mqttQoS, IMqttClientMessageListener listener) {
return client.subscribe(topicFilter, mqttQoS, listener);
}
/**
* 订阅
*
* @param mqttQoS MqttQoS
* @param topicFilter topicFilter
* @param listener MqttMessageListener
* @param properties MqttProperties
* @return MqttClient
*/
public MqttClient subscribe(String topicFilter, MqttQoS mqttQoS, IMqttClientMessageListener listener, MqttProperties properties) {
return client.subscribe(topicFilter, mqttQoS, listener, properties);
}
/**
* 订阅
*
* @param topicFilters topicFilter 数组
* @param mqttQoS MqttQoS
* @param listener MqttMessageListener
* @return MqttClient
*/
public MqttClient subscribe(String[] topicFilters, MqttQoS mqttQoS, IMqttClientMessageListener listener) {
return client.subscribe(topicFilters, mqttQoS, listener);
}
/**
* 订阅
*
* @param topicFilters topicFilter 数组
* @param mqttQoS MqttQoS
* @param listener MqttMessageListener
* @param properties MqttProperties
* @return MqttClient
*/
public MqttClient subscribe(String[] topicFilters, MqttQoS mqttQoS, IMqttClientMessageListener listener, MqttProperties properties) {
return client.subscribe(topicFilters, mqttQoS, listener, properties);
}
/**
* 批量订阅
*
* @param subscriptionList 订阅集合
* @return MqttClient
*/
public MqttClient subscribe(List<MqttClientSubscription> subscriptionList) {
return client.subscribe(subscriptionList);
}
/**
* 批量订阅
*
* @param subscriptionList 订阅集合
* @param properties MqttProperties
* @return MqttClient
*/
public MqttClient subscribe(List<MqttClientSubscription> subscriptionList, MqttProperties properties) {
return client.subscribe(subscriptionList, properties);
}
/**
* 取消订阅
*
* @param topicFilters topicFilter 集合
* @return MqttClient
*/
public MqttClient unSubscribe(String... topicFilters) {
return client.unSubscribe(topicFilters);
}
/**
* 取消订阅
*
* @param topicFilters topicFilter 集合
* @return MqttClient
*/
public MqttClient unSubscribe(List<String> topicFilters) {
return client.unSubscribe(topicFilters);
}
/**
* 发布消息
*
* @param topic topic
* @param payload 消息内容
* @return 是否发送成功
*/
public boolean publish(String topic, Object payload) {
return client.publish(topic, payload, MqttQoS.QOS0);
}
/**
* 发布消息
*
* @param topic topic
* @param payload 消息内容
* @param qos MqttQoS
* @return 是否发送成功
*/
public boolean publish(String topic, Object payload, MqttQoS qos) {
return client.publish(topic, payload, qos);
}
/**
* 发布消息
*
* @param topic topic
* @param payload 消息内容
* @param retain 是否在服务器上保留消息
* @return 是否发送成功
*/
public boolean publish(String topic, Object payload, boolean retain) {
return client.publish(topic, payload, retain);
}
/**
* 发布消息
*
* @param topic topic
* @param payload 消息体
* @param qos MqttQoS
* @param retain 是否在服务器上保留消息
* @return 是否发送成功
*/
public boolean publish(String topic, Object payload, MqttQoS qos, boolean retain) {
return client.publish(topic, payload, qos, retain);
}
/**
* 发布消息
*
* @param topic topic
* @param payload 消息体
* @param qos MqttQoS
* @param retain 是否在服务器上保留消息
* @param properties MqttProperties
* @return 是否发送成功
*/
public boolean publish(String topic, Object payload, MqttQoS qos, boolean retain, MqttProperties properties) {
return client.publish(topic, payload, qos, retain, properties);
}
/**
* 发布消息
*
* @param topic topic
* @param payload 消息体
* @param qos MqttQoS
* @param builder PublishBuilder
* @return 是否发送成功
*/
public boolean publish(String topic, Object payload, MqttQoS qos, Consumer<MqttPublishBuilder> builder) {
return client.publish(topic, payload, qos, builder);
}
/**
* 发布消息
*
* @param builder PublishBuilder
* @return 是否发送成功
*/
public boolean publish(MqttPublishBuilder builder) {
return client.publish(builder);
}
/**
* 添加定时任务,注意:如果抛出异常,会终止后续任务,请自行处理异常
*
* @param command runnable
* @param delay delay
* @return TimerTask
*/
public TimerTask schedule(Runnable command, long delay) {
return client.schedule(command, delay);
}
/**
* 添加定时任务,注意:如果抛出异常,会终止后续任务,请自行处理异常
*
* @param command runnable
* @param delay delay
* @param executor 用于自定义线程池,处理耗时业务
* @return TimerTask
*/
public TimerTask schedule(Runnable command, long delay, Executor executor) {
return client.schedule(command, delay, executor);
}
/**
* 添加定时任务
*
* @param command runnable
* @param delay delay
* @return TimerTask
*/
public TimerTask scheduleOnce(Runnable command, long delay) {
return client.scheduleOnce(command, delay);
}
/**
* 添加定时任务
*
* @param command runnable
* @param delay delay
* @param executor 用于自定义线程池,处理耗时业务
* @return TimerTask
*/
public TimerTask scheduleOnce(Runnable command, long delay, Executor executor) {
return client.scheduleOnce(command, delay, executor);
}
/**
* 重连
*/
public void reconnect() {
client.reconnect();
}
/**
* 重连到新的服务端节点
*
* @param ip ip
* @param port port
* @return 是否成功
*/
public boolean reconnect(String ip, int port) {
return client.reconnect(ip, port);
}
/**
* 断开 mqtt 连接
*
* @return 是否成功
*/
public boolean disconnect() {
return client.disconnect();
}
/**
* 获取 TioClient
*
* @return TioClient
*/
public TioClient getTioClient() {
return client.getTioClient();
}
/**
* 获取配置
*
* @return MqttClientCreator
*/
public MqttClientCreator getClientCreator() {
return clientCreator;
}
/**
* 获取 ClientTioConfig
*
* @return ClientTioConfig
*/
public TioClientConfig getClientTioConfig() {
return client.getClientTioConfig();
}
/**
* 获取 ClientChannelContext
*
* @return ClientChannelContext
*/
public ClientChannelContext getContext() {
return client.getContext();
}
/**
* 判断客户端跟服务端是否连接
*
* @return 是否已经连接成功
*/
public boolean isConnected() {
return client.isConnected();
}
/**
* 判断客户端跟服务端是否断开连接
*
* @return 是否断连
*/
public boolean isDisconnected() {
return client.isDisconnected();
}
/**
* 获取 MqttClient
*
* @return MqttClient
*/
public MqttClient getMqttClient() {
return client;
}
/**
* 添加启动时的临时订阅
*
* @param topicFilters topicFilters
* @param qos MqttQoS
* @param messageListener IMqttClientMessageListener
*/
public void addSubscriptionList(String[] topicFilters, MqttQoS qos, IMqttClientMessageListener messageListener) {
for (String topicFilter : topicFilters) {
tempSubscriptionList.add(new MqttClientSubscription(qos, topicFilter, messageListener));
}
}
public void connect() {
// 配置客户端连接监听器
clientCreator.connectListener(clientConnectListener);
// 自定义处理
if (customizer != null) {
customizer.customize(clientCreator);
}
// 连接超时时间,如果没设置,改成 3s减少因连不上卡顿时间
Integer timeout = clientCreator.getTimeout();
if (timeout == null) {
clientCreator.timeout(3);
}
// 使用同步连接,不过如果连不上会卡一会
client = clientCreator.connectSync();
// 添加订阅并清理零时订阅存储
client.subscribe(tempSubscriptionList);
tempSubscriptionList.clear();
log.info("mqtt client connect...");
}
public void destroy() {
client.stop();
}
}

View File

@@ -0,0 +1,195 @@
package org.dromara.mica.mqtt.client.solon.config;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.tio.utils.hutool.StrUtil;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* DataSize 兼容
*
* @author L.cm
*/
@Getter
@RequiredArgsConstructor
class DataSize {
/**
* Bytes per Kilobyte.
*/
private static final long BYTES_PER_KB = 1024;
/**
* Bytes per Megabyte.
*/
private static final long BYTES_PER_MB = BYTES_PER_KB * 1024;
/**
* Bytes per Gigabyte.
*/
private static final long BYTES_PER_GB = BYTES_PER_MB * 1024;
/**
* Bytes per Terabyte.
*/
private static final long BYTES_PER_TB = BYTES_PER_GB * 1024;
private final long bytes;
/**
* Obtain a {@link DataSize} representing the specified number of bytes.
* @param bytes the number of bytes, positive or negative
* @return a {@link DataSize}
*/
public static DataSize ofBytes(long bytes) {
return new DataSize(bytes);
}
/**
* Obtain a {@link DataSize} representing the specified number of kilobytes.
* @param kilobytes the number of kilobytes, positive or negative
* @return a {@link DataSize}
*/
public static DataSize ofKilobytes(long kilobytes) {
return new DataSize(Math.multiplyExact(kilobytes, BYTES_PER_KB));
}
/**
* Obtain a {@link DataSize} representing the specified number of megabytes.
* @param megabytes the number of megabytes, positive or negative
* @return a {@link DataSize}
*/
public static DataSize ofMegabytes(long megabytes) {
return new DataSize(Math.multiplyExact(megabytes, BYTES_PER_MB));
}
/**
* Obtain a {@link DataSize} representing the specified number of gigabytes.
* @param gigabytes the number of gigabytes, positive or negative
* @return a {@link DataSize}
*/
public static DataSize ofGigabytes(long gigabytes) {
return new DataSize(Math.multiplyExact(gigabytes, BYTES_PER_GB));
}
/**
* Obtain a {@link DataSize} representing the specified number of terabytes.
* @param terabytes the number of terabytes, positive or negative
* @return a {@link DataSize}
*/
public static DataSize ofTerabytes(long terabytes) {
return new DataSize(Math.multiplyExact(terabytes, BYTES_PER_TB));
}
/**
* Obtain a {@link DataSize} representing an amount in the specified {@link DataUnit}.
* @param amount the amount of the size, measured in terms of the unit,
* positive or negative
* @return a corresponding {@link DataSize}
*/
public static DataSize of(long amount, DataUnit unit) {
Objects.requireNonNull(unit, "Unit must not be null");
return new DataSize(Math.multiplyExact(amount, unit.getSize().getBytes()));
}
/**
* Obtain a {@link DataSize} from a text string such as {@code 12MB} using
* the specified default {@link DataUnit} if no unit is specified.
* <p>
* The string starts with a number followed optionally by a unit matching one of the
* supported {@linkplain DataUnit suffixes}.
* <p>
* Examples:
* <pre>
* "12KB" -- parses as "12 kilobytes"
* "5MB" -- parses as "5 megabytes"
* "20" -- parses as "20 kilobytes" (where the {@code defaultUnit} is {@link DataUnit#KILOBYTES})
* </pre>
* @param text the text to parse
* @return the parsed {@link DataSize}
*/
public static DataSize parse(String text) {
Objects.requireNonNull(text, "Text must not be null");
try {
Matcher matcher = DataSizeUtils.PATTERN.matcher(text.trim());
if (!matcher.matches()) {
throw new IllegalArgumentException("Invalid data size: " + text);
}
DataUnit unit = DataSizeUtils.determineDataUnit(matcher.group(2));
long amount = Long.parseLong(matcher.group(1));
return DataSize.of(amount, unit);
}
catch (Exception ex) {
throw new IllegalArgumentException("'" + text + "' is not a valid data size", ex);
}
}
/**
* Static nested class to support lazy loading of the {@link #PATTERN}.
* @since 5.3.21
*/
private static class DataSizeUtils {
/**
* The pattern for parsing.
*/
private static final Pattern PATTERN = Pattern.compile("^([+\\-]?\\d+)([a-zA-Z]{0,2})$");
private static DataUnit determineDataUnit(String suffix) {
return (StrUtil.isNotBlank(suffix) ? DataUnit.fromSuffix(suffix) : DataUnit.BYTES);
}
}
@Getter
@RequiredArgsConstructor
public enum DataUnit {
/**
* Bytes, represented by suffix {@code B}.
*/
BYTES("B", DataSize.ofBytes(1)),
/**
* Kilobytes, represented by suffix {@code KB}.
*/
KILOBYTES("KB", DataSize.ofKilobytes(1)),
/**
* Megabytes, represented by suffix {@code MB}.
*/
MEGABYTES("MB", DataSize.ofMegabytes(1)),
/**
* Gigabytes, represented by suffix {@code GB}.
*/
GIGABYTES("GB", DataSize.ofGigabytes(1)),
/**
* Terabytes, represented by suffix {@code TB}.
*/
TERABYTES("TB", DataSize.ofTerabytes(1));
private final String suffix;
private final DataSize size;
/**
* Return the {@link DataUnit} matching the specified {@code suffix}.
* @param suffix one of the standard suffixes
* @return the {@link DataUnit} matching the specified {@code suffix}
* @throws IllegalArgumentException if the suffix does not match the suffix
* of any of this enum's constants
*/
public static DataUnit fromSuffix(String suffix) {
for (DataUnit candidate : values()) {
if (candidate.suffix.equals(suffix)) {
return candidate;
}
}
throw new IllegalArgumentException("Unknown data unit suffix '" + suffix + "'");
}
}
}

View File

@@ -0,0 +1,106 @@
/*
* 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.config;
import org.dromara.mica.mqtt.client.solon.event.SolonEventMqttClientConnectListener;
import org.dromara.mica.mqtt.core.client.IMqttClientConnectListener;
import org.dromara.mica.mqtt.core.client.MqttClient;
import org.dromara.mica.mqtt.core.client.MqttClientCreator;
import org.dromara.mica.mqtt.core.deserialize.MqttDeserializer;
import org.dromara.mica.mqtt.core.deserialize.MqttJsonDeserializer;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Condition;
import org.noear.solon.annotation.Configuration;
import org.tio.utils.hutool.StrUtil;
/**
* mqtt client 配置
*
* @author L.cm
*/
@Configuration
public class MqttClientConfiguration {
@Bean
@Condition(onMissingBean = MqttDeserializer.class)
public MqttDeserializer mqttDeserializer() {
return new MqttJsonDeserializer();
}
@Bean
@Condition(onMissingBean = IMqttClientConnectListener.class)
public IMqttClientConnectListener solonEventMqttClientConnectListener() {
return new SolonEventMqttClientConnectListener();
}
@Bean
public MqttClientCreator mqttClientCreator(MqttClientProperties properties) {
MqttClientCreator clientCreator = MqttClient.create()
.name(properties.getName())
.ip(properties.getIp())
.port(properties.getPort())
.username(properties.getUsername())
.password(properties.getPassword())
.clientId(properties.getClientId())
.bindIp(properties.getBindIp())
.bindNetworkInterface(properties.getBindNetworkInterface())
.readBufferSize((int) DataSize.parse(properties.getReadBufferSize()).getBytes())
.maxBytesInMessage((int) DataSize.parse(properties.getMaxBytesInMessage()).getBytes())
.maxClientIdLength(properties.getMaxClientIdLength())
.keepAliveSecs(properties.getKeepAliveSecs())
.heartbeatMode(properties.getHeartbeatMode())
.heartbeatTimeoutStrategy(properties.getHeartbeatTimeoutStrategy())
.reconnect(properties.isReconnect())
.reInterval(properties.getReInterval())
.retryCount(properties.getRetryCount())
.reSubscribeBatchSize(properties.getReSubscribeBatchSize())
.version(properties.getVersion())
.cleanStart(properties.isCleanStart())
.sessionExpiryIntervalSecs(properties.getSessionExpiryIntervalSecs())
.statEnable(properties.isStatEnable())
.debug(properties.isDebug())
.disconnectBeforeStop(properties.isDisconnectBeforeStop());
Integer timeout = properties.getTimeout();
if (timeout != null && timeout > 0) {
clientCreator.timeout(timeout);
}
// mqtt 业务线程数
Integer bizThreadPoolSize = properties.getBizThreadPoolSize();
if (bizThreadPoolSize != null && bizThreadPoolSize > 0) {
clientCreator.bizThreadPoolSize(bizThreadPoolSize);
}
// 开启 ssl
MqttClientProperties.Ssl ssl = properties.getSsl();
if (ssl.isEnabled()) {
clientCreator.useSsl(ssl.getKeystorePath(), ssl.getKeystorePass(), ssl.getTruststorePath(), ssl.getTruststorePass());
}
// 构造遗嘱消息
MqttClientProperties.WillMessage willMessage = properties.getWillMessage();
if (willMessage != null && StrUtil.isNotBlank(willMessage.getTopic())) {
clientCreator.willMessage(builder -> {
builder.topic(willMessage.getTopic())
.qos(willMessage.getQos())
.retain(willMessage.isRetain());
if (StrUtil.isNotBlank(willMessage.getMessage())) {
builder.messageText(willMessage.getMessage());
}
});
}
return clientCreator;
}
}

View File

@@ -0,0 +1,211 @@
/*
* 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.config;
import lombok.Getter;
import lombok.Setter;
import org.dromara.mica.mqtt.codec.MqttConstant;
import org.dromara.mica.mqtt.codec.MqttQoS;
import org.dromara.mica.mqtt.codec.MqttVersion;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;
import org.tio.client.task.HeartbeatTimeoutStrategy;
import org.tio.core.task.HeartbeatMode;
/**
* MqttClient 配置
*
* @author wsq冷月宫主
*/
@Getter
@Setter
@Configuration
@Inject(value = "${" + MqttClientProperties.PREFIX + "}", required = false)
public class MqttClientProperties {
/**
* 配置前缀
*/
public static final String PREFIX = "mqtt.client";
/**
* 是否启用默认false
*/
private boolean enabled = true;
/**
* 名称默认Mica-Mqtt-Client
*/
private String name = "Mica-Mqtt-Client";
/**
* 服务端 ip默认127.0.0.1
*/
private String ip = "127.0.0.1";
/**
* 端口默认1883
*/
private int port = 1883;
/**
* 用户名
*/
private String username;
/**
* 密码
*/
private String password;
/**
* 客户端ID
*/
private String clientId;
/**
* 超时时间单位t-io 配置,可为 null
*/
private Integer timeout;
/**
* 绑定 ip绑定网卡用于多网卡默认为 null
*/
private String bindIp;
/**
* 绑定网卡,网卡名称,和 bindIp 取其一
*/
private String bindNetworkInterface;
/**
* 接收数据的 buffer size默认8KB
*/
private String readBufferSize = "8KB";
/**
* 消息解析最大 bytes 长度默认10MB
*/
private String maxBytesInMessage = "10MB";
/**
* mqtt 3.1 会校验此参数为 23为了减少问题设置成了 64
*/
private int maxClientIdLength = MqttConstant.DEFAULT_MAX_CLIENT_ID_LENGTH;
/**
* Keep Alive (s)
*/
private int keepAliveSecs = 60;
/**
* 心跳模式,支持最后发送或接收心跳时间来计算心跳,默认:最后发送心跳的时间
*/
private HeartbeatMode heartbeatMode = HeartbeatMode.LAST_REQ;
/**
* 心跳超时策略,支持发送 PING 和 CLOSE 断开连接,默认:最大努力发送 PING
*/
private HeartbeatTimeoutStrategy heartbeatTimeoutStrategy = HeartbeatTimeoutStrategy.PING;
/**
* 自动重连
*/
private boolean reconnect = true;
/**
* 重连的间隔时间单位毫秒默认5000
*/
private long reInterval = 5000;
/**
* 连续重连次数当连续重连这么多次都失败时不再重连。0和负数则一直重连
*/
private int retryCount = 0;
/**
* 重连重新订阅一个批次大小默认20
*/
private int reSubscribeBatchSize = 20;
/**
* mqtt 协议默认MQTT_5
*/
private MqttVersion version = MqttVersion.MQTT_5;
/**
* 清除会话
* <p>
* false 表示如果订阅的客户机断线了,那么要保存其要推送的消息,如果其重新连接时,则将这些消息推送。
* true 表示消除,表示客户机是第一次连接,消息所以以前的连接信息。
* </p>
*/
private boolean cleanStart = true;
/**
* 开启保留 session 时session 的有效期默认0
*/
private int sessionExpiryIntervalSecs = 0;
/**
* 遗嘱消息
*/
private WillMessage willMessage;
/**
* 是否开启监控默认false 不开启,节省内存
*/
private boolean statEnable = false;
/**
* debug
*/
private boolean debug = false;
/**
* mqtt 工作线程数默认2如果消息量比较大处理较慢例如做 emqx 的转发消息处理,可以调大此参数
*/
private Integer bizThreadPoolSize;
/**
* 停止前是否发送 disconnect 消息默认true 不会触发遗嘱消息
*/
private boolean disconnectBeforeStop = true;
/**
* ssl 配置
*/
private Ssl ssl = new Ssl();
@Getter
@Setter
public static class WillMessage {
/**
* 遗嘱消息 topic
*/
private String topic;
/**
* 遗嘱消息 qos默认 qos0
*/
private MqttQoS qos = MqttQoS.QOS0;
/**
* 遗嘱消息 payload
*/
private String message;
/**
* 遗嘱消息保留标识符,默认: false
*/
private boolean retain = false;
}
@Getter
@Setter
public static class Ssl {
/**
* 启用 ssl
*/
private boolean enabled = false;
/**
* keystore 证书路径
*/
private String keystorePath;
/**
* keystore 密码
*/
private String keystorePass;
/**
* truststore 证书路径
*/
private String truststorePath;
/**
* truststore 密码
*/
private String truststorePass;
}
}

View File

@@ -0,0 +1,40 @@
/*
* 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.event;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* mqtt 客户端连接成功事件
*
* @author L.cm
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MqttConnectedEvent implements Serializable {
/**
* 是否重连
*/
private boolean isReconnect;
}

View File

@@ -0,0 +1,44 @@
/*
* 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.event;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* mqtt 客户端断开连接事件
*
* @author L.cm
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MqttDisconnectEvent implements Serializable {
/**
* 断开原因
*/
String reason;
/**
* 是否删除连接
*/
boolean isRemove;
}

View File

@@ -0,0 +1,55 @@
/*
* 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.event;
import lombok.extern.slf4j.Slf4j;
import org.dromara.mica.mqtt.core.client.IMqttClientConnectListener;
import org.noear.solon.core.event.EventBus;
import org.tio.core.ChannelContext;
/**
* spring event mqtt client 连接监听
*
* @author L.cm
*/
@Slf4j
public class SolonEventMqttClientConnectListener implements IMqttClientConnectListener {
@Override
public void onConnected(ChannelContext context, boolean isReconnect) {
if (isReconnect) {
log.info("重连 mqtt 服务器重连成功...");
} else {
log.info("连接 mqtt 服务器成功...");
}
EventBus.publish(new MqttConnectedEvent(isReconnect));
}
@Override
public void onDisconnect(ChannelContext channelContext, Throwable throwable, String remark, boolean isRemove) {
String reason;
if (throwable == null) {
reason = remark;
log.info("mqtt 链接断开 remark:{} isRemove:{}", remark, isRemove);
} else {
reason = remark + " Exception:" + throwable.getMessage();
log.error("mqtt 链接断开 remark:{} isRemove:{}", remark, isRemove, throwable);
}
EventBus.publish(new MqttDisconnectEvent(reason, isRemove));
}
}

View File

@@ -0,0 +1,173 @@
/* Copyright (c) 2022 Peigen.info. All rights reserved. */
package org.dromara.mica.mqtt.client.solon.integration;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.mica.mqtt.core.annotation.MqttClientSubscribe;
import org.dromara.mica.mqtt.client.solon.MqttClientSubscribeListener;
import org.dromara.mica.mqtt.client.solon.MqttClientTemplate;
import org.dromara.mica.mqtt.client.solon.config.MqttClientConfiguration;
import org.dromara.mica.mqtt.client.solon.config.MqttClientProperties;
import org.dromara.mica.mqtt.core.client.*;
import org.dromara.mica.mqtt.core.deserialize.MqttDeserializer;
import org.dromara.mica.mqtt.core.util.TopicUtil;
import org.noear.solon.Solon;
import org.noear.solon.core.AppContext;
import org.noear.solon.core.BeanWrap;
import org.noear.solon.core.Plugin;
import org.noear.solon.core.util.ClassUtil;
import org.tio.core.ssl.SSLEngineCustomizer;
import org.tio.core.ssl.SslConfig;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
/**
* <b>(MqttClientPluginImpl)</b>
*
* @author Lihai、L.cm
* @version 1.0.0
* @since 2023/7/20
*/
@Slf4j
public class MqttClientPluginImpl implements Plugin {
private final List<ExtractorClassTag<MqttClientSubscribe>> subscribeClassTags = new ArrayList<>();
private final List<ExtractorMethodTag<MqttClientSubscribe>> subscribeMethodTags = new ArrayList<>();
private AppContext context;
@Override
public void start(AppContext context) throws Throwable {
this.context = context; //todo: 去掉 Solon.context() 写法,可同时兼容 2.5 之前与之后的版本 by noear,2023-09-15
// 查找类上的 MqttClientSubscribe 注解
context.beanBuilderAdd(MqttClientSubscribe.class, (clz, beanWrap, anno) -> {
subscribeClassTags.add(new ExtractorClassTag<>(clz, beanWrap, anno));
});
// 查找方法上的 MqttClientSubscribe 注解
context.beanExtractorAdd(MqttClientSubscribe.class, (bw, method, anno) -> {
subscribeMethodTags.add(new ExtractorMethodTag<>(bw, method, anno));
});
context.lifecycle(-9, () -> {
context.beanMake(MqttClientProperties.class);
context.beanMake(MqttClientConfiguration.class);
MqttClientProperties properties = context.getBean(MqttClientProperties.class);
MqttClientCreator clientCreator = context.getBean(MqttClientCreator.class);
// MqttClientTemplate init
IMqttClientConnectListener clientConnectListener = context.getBean(IMqttClientConnectListener.class);
MqttClientCustomizer customizers = context.getBean(MqttClientCustomizer.class);
MqttClientTemplate clientTemplate = new MqttClientTemplate(clientCreator, clientConnectListener, customizers);
BeanWrap mqttClientTemplateWrap = context.wrap(MqttClientTemplate.class, clientTemplate);
context.putWrap(MqttClientSubscribe.DEFAULT_CLIENT_TEMPLATE_BEAN, mqttClientTemplateWrap);
context.putWrap(MqttClientTemplate.class, mqttClientTemplateWrap);
// ssl 自定义配置
SslConfig sslConfig = clientCreator.getSslConfig();
if (sslConfig != null) {
SSLEngineCustomizer sslCustomizer = context.getBean(SSLEngineCustomizer.class);
if (sslCustomizer != null) {
sslConfig.setSslEngineCustomizer(sslCustomizer);
}
}
// 客户端 session
IMqttClientSession clientSession = context.getBean(IMqttClientSession.class);
clientCreator.clientSession(clientSession);
// 添加启动时的临时订阅
subscribeDetector();
// connect
if (properties.isEnabled()) {
clientTemplate.connect();
}
});
}
private void subscribeDetector() {
// 类级别的注解订阅
subscribeClassTags.forEach(each -> {
MqttClientSubscribe anno = each.getAnno();
MqttClientTemplate clientTemplate = getMqttClientTemplate(anno);
// 订阅的 topic 转换
String[] topicFilters = getTopicFilters(anno.value());
// 订阅
IMqttClientMessageListener clientMessageListener = each.getBeanWrap().get();
clientTemplate.addSubscriptionList(topicFilters, anno.qos(), clientMessageListener);
});
// 方法级别的注解订阅
subscribeMethodTags.forEach(each -> {
MqttClientSubscribe anno = each.getAnno();
MqttClientTemplate clientTemplate = getMqttClientTemplate(anno);
// 订阅的 topic 转换
String[] topicTemplates = anno.value();
String[] topicFilters = getTopicFilters(topicTemplates);
// 自定义的反序列化,支持 solon bean 或者 无参构造器初始化
Class<? extends MqttDeserializer> deserialized = anno.deserialize();
MqttDeserializer deserializer = getMqttDeserializer(deserialized);
// 构造监听器
Object bean = each.getBw().get();
Method method = each.getMethod();
// 订阅
MqttClientSubscribeListener listener = new MqttClientSubscribeListener(bean, method, topicTemplates, topicFilters, deserializer);
clientTemplate.addSubscriptionList(topicFilters, anno.qos(), listener);
});
}
@Override
public void stop() throws Throwable {
MqttClientTemplate clientTemplate = context.getBean(MqttClientTemplate.class);
clientTemplate.destroy();
log.info("mqtt client stop...");
}
private MqttClientTemplate getMqttClientTemplate(MqttClientSubscribe anno) {
String beanName = anno.clientTemplateBean();
// 添加对占位符的支持gitee #ID7PF6 https://gitee.com/dromara/mica-mqtt/issues/ID7PF6
String resolvedBeanName = Optional.ofNullable(Solon.cfg().getByTmpl(beanName)).orElse(beanName);
return context.getBean(resolvedBeanName);
}
/**
* 获取解码器
*
* @param deserializerType deserializerType
* @return 解码器
*/
private MqttDeserializer getMqttDeserializer(Class<?> deserializerType) {
BeanWrap beanWrap = context.getWrap(deserializerType);
if (beanWrap == null) {
return ClassUtil.newInstance(deserializerType);
}
return beanWrap.get();
}
private String[] getTopicFilters(String[] topicTemplates) {
// 1. 替换 solon cfg 变量
// 2. 替换订阅中的其他变量
return Arrays.stream(topicTemplates)
.map((x) -> Optional.ofNullable(Solon.cfg().getByTmpl(x)).orElse(x))
.map(TopicUtil::getTopicFilter)
.toArray(String[]::new);
}
@Data
@RequiredArgsConstructor
private static class ExtractorClassTag<T> {
private final Class<?> clz;
private final BeanWrap beanWrap;
private final T anno;
}
@Data
@RequiredArgsConstructor
private static class ExtractorMethodTag<T> {
private final BeanWrap bw;
private final Method method;
private final T anno;
}
}

View File

@@ -0,0 +1,9 @@
open module org.dromara.mica.mqtt.client.solon.plugin {
requires solon;
requires lombok;
requires transitive org.dromara.mica.mqtt.client;
exports org.dromara.mica.mqtt.client.solon;
exports org.dromara.mica.mqtt.client.solon.event;
exports org.dromara.mica.mqtt.client.solon.config;
provides org.noear.solon.core.Plugin with org.dromara.mica.mqtt.client.solon.integration.MqttClientPluginImpl;
}

View File

@@ -0,0 +1,199 @@
{
"properties": [
{
"name": "mqtt.client.bind-ip",
"type": "java.lang.String",
"description": "绑定 ip绑定网卡用于多网卡默认为 null"
},
{
"name": "mqtt.client.bind-network-interface",
"type": "java.lang.String",
"description": "绑定网卡,网卡名称,和 bindIp 取其一"
},
{
"name": "mqtt.client.biz-thread-pool-size",
"type": "java.lang.Integer",
"description": "mqtt 工作线程数默认2如果消息量比较大处理较慢例如做 emqx 的转发消息处理,可以调大此参数"
},
{
"name": "mqtt.client.clean-start",
"type": "java.lang.Boolean",
"description": "清除会话 <p> false 表示如果订阅的客户机断线了,那么要保存其要推送的消息,如果其重新连接时,则将这些消息推送。 true 表示消除,表示客户机是第一次连接,消息所以以前的连接信息。 <\/p>",
"defaultValue": true
},
{
"name": "mqtt.client.client-id",
"type": "java.lang.String",
"description": "客户端ID"
},
{
"name": "mqtt.client.debug",
"type": "java.lang.Boolean",
"description": "debug",
"defaultValue": false
},
{
"name": "mqtt.client.enabled",
"type": "java.lang.Boolean",
"description": "是否启用默认true",
"defaultValue": true
},
{
"name": "mqtt.client.global-subscribe",
"type": "java.util.List<org.dromara.mica.mqtt.codec.MqttTopicSubscription>",
"description": "全局订阅"
},
{
"name": "mqtt.client.heartbeat-mode",
"type": "org.tio.core.task.HeartbeatMode",
"description": "心跳模式,支持最后发送或接收心跳时间来计算心跳,默认:最后发送心跳的时间"
},
{
"name": "mqtt.client.heartbeat-timeout-strategy",
"type": "org.tio.client.task.HeartbeatTimeoutStrategy",
"description": "心跳超时策略,支持发送 PING 和 CLOSE 断开连接,默认:最大努力发送 PING"
},
{
"name": "mqtt.client.ip",
"type": "java.lang.String",
"description": "服务端 ip默认127.0.0.1",
"defaultValue": "127.0.0.1"
},
{
"name": "mqtt.client.keep-alive-secs",
"type": "java.lang.Integer",
"description": "Keep Alive (s)",
"defaultValue": 60
},
{
"name": "mqtt.client.max-bytes-in-message",
"type": "org.springframework.util.unit.DataSize",
"description": "消息解析最大 bytes 长度默认10M"
},
{
"name": "mqtt.client.max-client-id-length",
"type": "java.lang.Integer",
"description": "mqtt 3.1 会校验此参数为 23为了减少问题设置成了 64"
},
{
"name": "mqtt.client.name",
"type": "java.lang.String",
"description": "名称默认Mica-Mqtt-Client",
"defaultValue": "Mica-Mqtt-Client"
},
{
"name": "mqtt.client.password",
"type": "java.lang.String",
"description": "密码"
},
{
"name": "mqtt.client.port",
"type": "java.lang.Integer",
"description": "端口默认1883",
"defaultValue": 1883
},
{
"name": "mqtt.client.re-interval",
"type": "java.lang.Long",
"description": "重连的间隔时间单位毫秒默认5000",
"defaultValue": 5000
},
{
"name": "mqtt.client.re-subscribe-batch-size",
"type": "java.lang.Integer",
"description": "重连重新订阅一个批次大小默认20",
"defaultValue": 20
},
{
"name": "mqtt.client.read-buffer-size",
"type": "org.springframework.util.unit.DataSize",
"description": "接收数据的 buffer size默认8k"
},
{
"name": "mqtt.client.reconnect",
"type": "java.lang.Boolean",
"description": "自动重连",
"defaultValue": true
},
{
"name": "mqtt.client.retry-count",
"type": "java.lang.Integer",
"description": "连续重连次数当连续重连这么多次都失败时不再重连。0和负数则一直重连",
"defaultValue": 0
},
{
"name": "mqtt.client.session-expiry-interval-secs",
"type": "java.lang.Integer",
"description": "开启保留 session 时session 的有效期默认0",
"defaultValue": 0
},
{
"name": "mqtt.client.ssl.enabled",
"type": "java.lang.Boolean",
"description": "启用 ssl",
"defaultValue": false
},
{
"name": "mqtt.client.ssl.keystore-pass",
"type": "java.lang.String",
"description": "keystore 密码"
},
{
"name": "mqtt.client.ssl.keystore-path",
"type": "java.lang.String",
"description": "keystore 证书路径"
},
{
"name": "mqtt.client.ssl.truststore-pass",
"type": "java.lang.String",
"description": "truststore 密码"
},
{
"name": "mqtt.client.ssl.truststore-path",
"type": "java.lang.String",
"description": "truststore 证书路径"
},
{
"name": "mqtt.client.stat-enable",
"type": "java.lang.Boolean",
"description": "是否开启监控默认false 不开启,节省内存",
"defaultValue": false
},
{
"name": "mqtt.client.timeout",
"type": "java.lang.Integer",
"description": "超时时间单位t-io 配置,可为 null"
},
{
"name": "mqtt.client.username",
"type": "java.lang.String",
"description": "用户名"
},
{
"name": "mqtt.client.version",
"type": "org.dromara.mica.mqtt.codec.MqttVersion",
"description": "mqtt 协议默认MQTT_5"
},
{
"name": "mqtt.client.will-message.message",
"type": "java.lang.String",
"description": "遗嘱消息 payload"
},
{
"name": "mqtt.client.will-message.qos",
"type": "org.dromara.mica.mqtt.codec.MqttQoS",
"description": "遗嘱消息 qos默认 qos0"
},
{
"name": "mqtt.client.will-message.retain",
"type": "java.lang.Boolean",
"description": "遗嘱消息保留标识符,默认: false",
"defaultValue": false
},
{
"name": "mqtt.client.will-message.topic",
"type": "java.lang.String",
"description": "遗嘱消息 topic"
}
]
}

View File

@@ -0,0 +1,2 @@
solon.plugin=org.dromara.mica.mqtt.client.solon.integration.MqttClientPluginImpl
solon.plugin.priority=1

View File

@@ -0,0 +1,32 @@
package org.dromara.mica.mqtt.client.solon.test;
import org.dromara.mica.mqtt.client.solon.MqttClientTemplate;
import org.noear.solon.Solon;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.core.event.AppLoadEndEvent;
import org.noear.solon.core.event.EventListener;
import java.nio.charset.StandardCharsets;
/**
* <b>(ClientTest)</b>
*
* @author Peigen
* @version 1.0.0
* @since 2023/7/15
*/
@Component
public class ClientTest implements EventListener<AppLoadEndEvent> {
public static void main(String[] args) {
Solon.start(ClientTest.class, args);
}
@Inject
MqttClientTemplate client;
@Override
public void onEvent(AppLoadEndEvent event) throws Throwable {
client.publish("mica", "hello".getBytes(StandardCharsets.UTF_8));
}
}

View File

@@ -0,0 +1,43 @@
/*
* 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.test.listener;
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 客户端连接状态监听
*
* @author L.cm
*/
@Component
public class MqttClientConnectListener implements EventListener<MqttConnectedEvent> {
private static final Logger logger = LoggerFactory.getLogger(MqttClientConnectListener.class);
@Inject
private MqttClientCreator mqttClientCreator;
@Override
public void onEvent(MqttConnectedEvent mqttConnectedEvent) throws Throwable {
logger.info("MqttConnectedEvent:{}", mqttConnectedEvent);
}
}

View File

@@ -0,0 +1,47 @@
/*
* 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.test.listener;
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 客户端连接状态监听
*
* @author L.cm
*/
@Component
public class MqttClientDisConnectListener implements EventListener<MqttDisconnectEvent> {
private static final Logger logger = LoggerFactory.getLogger(MqttClientDisConnectListener.class);
@Inject
private MqttClientCreator mqttClientCreator;
@Override
public void onEvent(MqttDisconnectEvent mqttDisconnectEvent) throws Throwable {
logger.info("MqttDisconnectEvent:{}", mqttDisconnectEvent);
// 在断线时更新 clientId、username、password
mqttClientCreator.clientId("newClient" + System.currentTimeMillis())
.username("newUserName")
.password("newPassword");
}
}

View File

@@ -0,0 +1,26 @@
package org.dromara.mica.mqtt.client.solon.test.listener;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import java.nio.charset.StandardCharsets;
/**
* 客户端消息监听的另一种方式
*
* @author L.cm
*/
@MqttClientSubscribe("${topic1}")
public class MqttClientMessageListener implements IMqttClientMessageListener {
private static final Logger logger = LoggerFactory.getLogger(MqttClientMessageListener.class);
@Override
public void onMessage(ChannelContext context, String topic, MqttPublishMessage message, byte[] payload) {
logger.info("topic:{} payload:{}", topic, new String(payload, StandardCharsets.UTF_8));
}
}

View File

@@ -0,0 +1,38 @@
package org.dromara.mica.mqtt.client.solon.test.listener;
import org.dromara.mica.mqtt.core.annotation.MqttClientSubscribe;
import org.dromara.mica.mqtt.codec.MqttQoS;
import org.noear.solon.annotation.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.nio.charset.StandardCharsets;
/**
* 客户端消息监听
*
* @author L.cm
*/
@Component
public class MqttClientSubscribeListener {
private static final Logger logger = LoggerFactory.getLogger(MqttClientSubscribeListener.class);
@MqttClientSubscribe("/test/#")
public void subQos0(String topic, byte[] payload) {
logger.info("topic:{} payload:{}", topic, new String(payload, StandardCharsets.UTF_8));
}
@MqttClientSubscribe(value = "/qos1/#", qos = MqttQoS.QOS1)
public void subQos1(String topic, byte[] payload) {
logger.info("topic:{} payload:{}", topic, new String(payload, StandardCharsets.UTF_8));
}
@MqttClientSubscribe("/sys/${productKey}/${deviceName}/thing/sub/register")
public void thingSubRegister(String topic, byte[] payload) {
// 1.3.8 开始支持,@MqttClientSubscribe 注解支持 ${} 变量替换,会默认替换成 +
// 注意mica-mqtt 会先从 Spring boot 配置中替换参数 ${},如果存在配置会优先被替换。
logger.info("topic:{} payload:{}", topic, new String(payload, StandardCharsets.UTF_8));
}
}

View File

@@ -0,0 +1,27 @@
server:
port: 30036
# mqtt-client 配置
mqtt:
client:
enabled: true # 是否开启客户端默认true
ip: 127.0.0.1 # 连接的服务端 ip 默认127.0.0.1
port: 18889 # 端口默认1883
# clientId: 000001 # 客户端Id非常重要一般为设备 sn不可重复
# username: mica # 认证的用户名
# 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 时间,单位:秒
# clean-session: true # mqtt 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: /test2/#