mirror of
https://gitee.com/dromara/RuoYi-Cloud-Plus.git
synced 2026-04-26 13:59:37 +08:00
add 增加 ruoyi-common-mqtt 模块
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
package org.dromara.common.mqtt.config;
|
||||
|
||||
import org.dromara.common.mqtt.listener.MqttClientConnectListener;
|
||||
import org.dromara.common.mqtt.listener.MqttClientGlobalMessageListener;
|
||||
import org.dromara.mica.mqtt.core.client.MqttClientCreator;
|
||||
import org.dromara.mica.mqtt.core.client.MqttClientCustomizer;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.core.task.VirtualThreadTaskExecutor;
|
||||
import org.tio.utils.thread.ThreadUtils;
|
||||
import org.tio.utils.thread.pool.SynThreadPoolExecutor;
|
||||
import org.tio.utils.thread.pool.TioCallerRunsPolicy;
|
||||
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* mqtt客户端配置初始化
|
||||
* <p>
|
||||
* 用法文档 <a href="https://mica-mqtt.dreamlu.net/guide/spring/client.html">...</a>
|
||||
* 测试server搭建:
|
||||
* 可执行下载其他mqtt服务端搭建
|
||||
* 也可使用 mica自带的server搭建 <a href="https://mica-mqtt.dreamlu.net/guide/spring/server.html">...</a>
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
@AutoConfiguration
|
||||
@ConditionalOnProperty(value = "mqtt.client.enabled", havingValue = "true")
|
||||
public class MqttAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public MqttClientConnectListener mqttClientConnectListener(MqttClientCreator mqttClientCreator) {
|
||||
return new MqttClientConnectListener(mqttClientCreator);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MqttClientGlobalMessageListener mqttClientGlobalMessageListener() {
|
||||
return new MqttClientGlobalMessageListener();
|
||||
}
|
||||
|
||||
/**
|
||||
* 客户端使用虚拟线程配置
|
||||
*/
|
||||
@Bean
|
||||
public MqttClientCustomizer mqttClientCustomizer() {
|
||||
return creator -> {
|
||||
// 这个数不重要 已经使用虚拟线程 就是填一下防止报错
|
||||
int corePoolSize = ThreadUtils.CORE_POOL_SIZE;
|
||||
|
||||
ThreadFactory factory = new VirtualThreadTaskExecutor("tio-worker-virtual").getVirtualThreadFactory();
|
||||
SynThreadPoolExecutor tioExecutor = new SynThreadPoolExecutor(corePoolSize, corePoolSize,
|
||||
0L, new LinkedBlockingQueue<>(), factory, new TioCallerRunsPolicy());
|
||||
tioExecutor.prestartCoreThread();
|
||||
creator.tioExecutor(tioExecutor);
|
||||
|
||||
ThreadFactory factory1 = new VirtualThreadTaskExecutor("tio-group-virtual").getVirtualThreadFactory();
|
||||
ThreadPoolExecutor groupExecutor = new ThreadPoolExecutor(corePoolSize, corePoolSize,
|
||||
0L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), factory1, new TioCallerRunsPolicy());
|
||||
groupExecutor.prestartCoreThread();
|
||||
creator.groupExecutor(groupExecutor);
|
||||
|
||||
ThreadFactory factory2 = new VirtualThreadTaskExecutor("biz-worker-virtual").getVirtualThreadFactory();
|
||||
ThreadPoolExecutor mqttExecutor = new ThreadPoolExecutor(corePoolSize, corePoolSize,
|
||||
0L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), factory2, new TioCallerRunsPolicy());
|
||||
mqttExecutor.prestartCoreThread();
|
||||
creator.mqttExecutor(mqttExecutor);
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package org.dromara.common.mqtt.listener;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.mica.mqtt.core.client.IMqttClientConnectListener;
|
||||
import org.dromara.mica.mqtt.core.client.MqttClientCreator;
|
||||
import org.tio.core.ChannelContext;
|
||||
|
||||
/**
|
||||
* 客户端连接状态监听
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
@Slf4j
|
||||
public class MqttClientConnectListener implements IMqttClientConnectListener {
|
||||
//
|
||||
private final MqttClientCreator mqttClientCreator;
|
||||
|
||||
public MqttClientConnectListener(MqttClientCreator mqttClientCreator) {
|
||||
this.mqttClientCreator = mqttClientCreator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnected(ChannelContext context, boolean isReconnect) {
|
||||
// 创建连接
|
||||
log.info("MqttConnectedEvent:{}", context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisconnect(ChannelContext context, Throwable throwable, String remark, boolean isRemove) {
|
||||
// 离线时更新重连
|
||||
log.info("MqttDisconnectEvent:{}", context, throwable);
|
||||
// 在断线时更新 clientId、username、password
|
||||
// mqttClientCreator.clientId("newClient" + System.currentTimeMillis())
|
||||
// .username("newUserName")
|
||||
// .password("newPassword");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.dromara.common.mqtt.listener;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.mica.mqtt.codec.message.MqttPublishMessage;
|
||||
import org.dromara.mica.mqtt.core.client.IMqttClientGlobalMessageListener;
|
||||
import org.tio.core.ChannelContext;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* 全局消息监听,可以监听到所有订阅消息
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
@Slf4j
|
||||
public class MqttClientGlobalMessageListener implements IMqttClientGlobalMessageListener {
|
||||
|
||||
@Override
|
||||
public void onMessage(ChannelContext context, String topic, MqttPublishMessage message, byte[] payload) {
|
||||
log.info("MqttGlobalMessageEvent => topic: {}, msg: {}", topic, new String(payload, StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
org.dromara.common.mqtt.config.MqttAutoConfiguration
|
||||
Reference in New Issue
Block a user