From f4ce280a4cb072e05199f515edeeac42f7a2f3756ccb5294e0ad451974505d63 Mon Sep 17 00:00:00 2001 From: attiya <2413103649@qq.com> Date: Fri, 7 Nov 2025 16:20:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=B8=E5=BF=83=E9=80=9A=E4=BF=A1=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cdzy/gather/mqtt/MqttConnectionFactory.java | 17 ++++++++++++++--- .../cdzy/gather/mqtt/MqttConnectionPool.java | 14 ++++++++------ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/ebike-gather/src/main/java/org/cdzy/gather/mqtt/MqttConnectionFactory.java b/ebike-gather/src/main/java/org/cdzy/gather/mqtt/MqttConnectionFactory.java index 09a348f..614dc65 100644 --- a/ebike-gather/src/main/java/org/cdzy/gather/mqtt/MqttConnectionFactory.java +++ b/ebike-gather/src/main/java/org/cdzy/gather/mqtt/MqttConnectionFactory.java @@ -7,6 +7,8 @@ import org.apache.commons.pool2.BasePooledObjectFactory; import org.apache.commons.pool2.impl.DefaultPooledObject; import org.springframework.stereotype.Component; +import java.util.concurrent.TimeUnit; + /** * MQTT 连接工厂 */ @@ -54,14 +56,23 @@ public class MqttConnectionFactory extends BasePooledObjectFactory { } @Override - public void destroyObject(PooledObject p){ + public void destroyObject(PooledObject p) { Channel channel = p.getObject(); if (channel != null) { try { + String remoteAddress = channel.remoteAddress() != null ? + channel.remoteAddress().toString() : "unknown"; + + // 记录详细的销毁原因 + log.info("销毁MQTT连接 - 远程地址: {}, 活跃: {}, 开启: {}, 可写: {}", + remoteAddress, + channel.isActive(), + channel.isOpen(), + channel.isWritable()); // 需要实现这个方法 + if (channel.isActive()) { - channel.close().await(5, java.util.concurrent.TimeUnit.SECONDS); + channel.close().await(5, TimeUnit.SECONDS); } - log.debug("MQTT连接已销毁"); } catch (Exception e) { log.warn("销毁MQTT连接异常", e); } diff --git a/ebike-gather/src/main/java/org/cdzy/gather/mqtt/MqttConnectionPool.java b/ebike-gather/src/main/java/org/cdzy/gather/mqtt/MqttConnectionPool.java index b3a1901..b393b0c 100644 --- a/ebike-gather/src/main/java/org/cdzy/gather/mqtt/MqttConnectionPool.java +++ b/ebike-gather/src/main/java/org/cdzy/gather/mqtt/MqttConnectionPool.java @@ -46,11 +46,13 @@ public class MqttConnectionPool implements DisposableBean, InitializingBean { config.setMinIdle(MqttConfig.getPoolMinIdle()); // 启用空闲连接维护 - config.setTimeBetweenEvictionRuns(Duration.ofSeconds(30)); - config.setSoftMinEvictableIdleTime(Duration.ofMinutes(2)); - config.setNumTestsPerEvictionRun(3); - config.setTestWhileIdle(true); - config.setTestOnBorrow(true); + config.setTimeBetweenEvictionRuns(Duration.ofMinutes(5)); + config.setSoftMinEvictableIdleTime(Duration.ofMinutes(30)); + config.setMinEvictableIdleTime(Duration.ofMinutes(30)); + config.setNumTestsPerEvictionRun(-1); + + config.setTestOnBorrow(false); + config.setTestOnReturn(false); config.setTestOnReturn(false); connectionPool = new GenericObjectPool<>(mqttConnectionFactory, config); @@ -101,7 +103,7 @@ public class MqttConnectionPool implements DisposableBean, InitializingBean { } catch (Exception e) { log.error("维护MQTT连接池异常", e); } - }, 10, 60, TimeUnit.SECONDS); + }, 10, 60*5, TimeUnit.SECONDS); } /**