核心通信模块代码优化、默认日志打印格式修改

This commit is contained in:
attiya 2025-11-07 10:08:26 +08:00
parent 338d0f518e
commit aa1e9fc3a2
9 changed files with 50 additions and 15 deletions

View File

@ -111,3 +111,7 @@ management:
endpoint: endpoint:
health: health:
show-details: always show-details: always
logging:
pattern:
dateformat: yyyy-MM-dd HH:mm:ss

View File

@ -44,9 +44,8 @@ public class MqttConnectionFactory extends BasePooledObjectFactory<Channel> {
public boolean validateObject(PooledObject<Channel> p) { public boolean validateObject(PooledObject<Channel> p) {
Channel channel = p.getObject(); Channel channel = p.getObject();
boolean isValid = channel != null && boolean isValid = channel != null &&
channel.isActive() && channel.isActive()&&
channel.isOpen() && channel.isOpen();
channel.isWritable();
if (!isValid && log.isDebugEnabled()) { if (!isValid && log.isDebugEnabled()) {
log.debug("MQTT连接验证失败"); log.debug("MQTT连接验证失败");

View File

@ -4,6 +4,7 @@ import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.mqtt.*; import io.netty.handler.codec.mqtt.*;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -14,7 +15,6 @@ import java.nio.charset.StandardCharsets;
@Slf4j @Slf4j
public class MqttHandler extends ChannelInboundHandlerAdapter { public class MqttHandler extends ChannelInboundHandlerAdapter {
private volatile boolean connected = false;
@Override @Override
public void channelRead(ChannelHandlerContext ctx, Object msg){ public void channelRead(ChannelHandlerContext ctx, Object msg){
if (msg instanceof MqttMessage mqttMessage) { if (msg instanceof MqttMessage mqttMessage) {
@ -130,13 +130,21 @@ public class MqttHandler extends ChannelInboundHandlerAdapter {
} }
@Override @Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception { public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (connected) { if (evt instanceof IdleStateEvent idleStateEvent) {
log.warn("MQTT连接断开"); switch (idleStateEvent.state()) {
} else { case WRITER_IDLE:
log.debug("MQTT连接未完成握手即断开"); // 发送PINGREQ心跳
MqttMessage pingReq = new MqttMessage(new MqttFixedHeader(
MqttMessageType.PINGREQ, false, MqttQoS.AT_MOST_ONCE, false, 0));
ctx.writeAndFlush(pingReq);
// log.debug("发送PINGREQ心跳");
break;
case READER_IDLE:
// log.warn("读空闲超时,可能连接已断开");
break;
} }
connected = false; }
super.channelInactive(ctx); super.userEventTriggered(ctx, evt);
} }
} }

View File

@ -45,3 +45,7 @@ management:
endpoint: endpoint:
health: health:
show-details: always show-details: always
logging:
pattern:
dateformat: yyyy-MM-dd HH:mm:ss

View File

@ -110,3 +110,7 @@ minio:
secret-key: YuFoH+VJVzQbNLRqVMo39dM5pWXCEcMvrCrtgwB0 # 私有密钥 secret-key: YuFoH+VJVzQbNLRqVMo39dM5pWXCEcMvrCrtgwB0 # 私有密钥
bucket-name: operations-objects bucket-name: operations-objects
show-url: http://47.109.141.125:9000 show-url: http://47.109.141.125:9000
logging:
pattern:
dateformat: yyyy-MM-dd HH:mm:ss

View File

@ -105,3 +105,7 @@ management:
endpoint: endpoint:
health: health:
show-details: always show-details: always
logging:
pattern:
dateformat: yyyy-MM-dd HH:mm:ss

View File

@ -56,3 +56,7 @@ management:
endpoint: endpoint:
health: health:
show-details: always show-details: always
logging:
pattern:
dateformat: yyyy-MM-dd HH:mm:ss

View File

@ -87,4 +87,8 @@ management:
health: health:
show-details: always show-details: always
logging:
pattern:
dateformat: yyyy-MM-dd HH:mm:ss

View File

@ -105,3 +105,7 @@ minio:
bucket-name: user-objects bucket-name: user-objects
show-url: http://47.109.141.125:9000 show-url: http://47.109.141.125:9000
logging:
pattern:
dateformat: yyyy-MM-dd HH:mm:ss