[update] 显式指定注解处理器,4.x

This commit is contained in:
wujiawei
2026-01-07 14:32:30 +08:00
parent 4d6c5fcbd7
commit 0c6fb1680a
4 changed files with 56 additions and 1 deletions

View File

@@ -4,8 +4,12 @@ import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j;
import java.net.Socket;
@Slf4j
public class ChannelUtils {
/**
@@ -74,5 +78,36 @@ public class ChannelUtils {
}
}
/**
* 强制关闭Netty通道跳过TCP优雅关闭直接关闭套接字
* @param channel Netty通道
*/
public static void forceCloseChannel(Channel channel) {
if (channel == null || !channel.isOpen()) {
return;
}
try {
// 第一步:禁用通道读写,拒绝接收新数据
channel.config().setAutoRead(false);
channel.config().setAutoClose(true);
// 第二步获取底层Socket强制关闭核心抖音场景必须
if (channel instanceof NioSocketChannel) {
NioSocketChannel nioChannel = (NioSocketChannel) channel;
Socket socket = nioChannel.javaChannel().socket();
if (socket != null && !socket.isClosed()) {
socket.setSoLinger(true, 0); // 禁用SO_LINGER立即关闭
socket.close(); // 强制关闭Socket切断TCP连接
}
}
// 第三步关闭Netty通道兜底
channel.close().sync();
log.info("通道已强制关闭");
} catch (Exception e) {
log.error("强制关闭通道异常:{}", e.getMessage());
}
}
}