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

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

@ -110,4 +110,8 @@ management:
include: health,info
endpoint:
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) {
Channel channel = p.getObject();
boolean isValid = channel != null &&
channel.isActive() &&
channel.isOpen() &&
channel.isWritable();
channel.isActive()&&
channel.isOpen();
if (!isValid && log.isDebugEnabled()) {
log.debug("MQTT连接验证失败");

View File

@ -4,6 +4,7 @@ import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.mqtt.*;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j;
import java.nio.charset.StandardCharsets;
@ -14,7 +15,6 @@ import java.nio.charset.StandardCharsets;
@Slf4j
public class MqttHandler extends ChannelInboundHandlerAdapter {
private volatile boolean connected = false;
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg){
if (msg instanceof MqttMessage mqttMessage) {
@ -130,13 +130,21 @@ public class MqttHandler extends ChannelInboundHandlerAdapter {
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
if (connected) {
log.warn("MQTT连接断开");
} else {
log.debug("MQTT连接未完成握手即断开");
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof IdleStateEvent idleStateEvent) {
switch (idleStateEvent.state()) {
case WRITER_IDLE:
// 发送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

@ -44,4 +44,8 @@ management:
include: health,info
endpoint:
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 # 私有密钥
bucket-name: operations-objects
show-url: http://47.109.141.125:9000
logging:
pattern:
dateformat: yyyy-MM-dd HH:mm:ss

View File

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

View File

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

View File

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

View File

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