增加心跳超时配置项

master
剑器近 2021-08-06 17:43:30 +08:00
parent 509051196b
commit cb8151889b
3 changed files with 27 additions and 5 deletions

View File

@ -16,6 +16,9 @@ import io.netty.channel.ChannelInboundHandlerAdapter;
*/ */
public class NettyConfig { public class NettyConfig {
protected final int readerIdleTime;
protected final int writerIdleTime;
protected final int allIdleTime;
protected final int port; protected final int port;
protected final int maxFrameLength; protected final int maxFrameLength;
protected final LengthField lengthField; protected final LengthField lengthField;
@ -28,7 +31,10 @@ public class NettyConfig {
protected final SessionManager sessionManager; protected final SessionManager sessionManager;
protected final SessionListener sessionListener; protected final SessionListener sessionListener;
private NettyConfig(int port, private NettyConfig(int readerIdleTime,
int writerIdleTime,
int allIdleTime,
int port,
int maxFrameLength, int maxFrameLength,
LengthField lengthField, LengthField lengthField,
Delimiter[] delimiter, Delimiter[] delimiter,
@ -39,6 +45,9 @@ public class NettyConfig {
SessionManager sessionManager, SessionManager sessionManager,
SessionListener sessionListener SessionListener sessionListener
) { ) {
this.readerIdleTime = readerIdleTime;
this.writerIdleTime = writerIdleTime;
this.allIdleTime = allIdleTime;
this.port = port; this.port = port;
this.maxFrameLength = maxFrameLength; this.maxFrameLength = maxFrameLength;
this.lengthField = lengthField; this.lengthField = lengthField;
@ -58,6 +67,9 @@ public class NettyConfig {
public static class Builder { public static class Builder {
private int readerIdleTime = 240;
private int writerIdleTime = 0;
private int allIdleTime = 0;
private int port; private int port;
private int maxFrameLength; private int maxFrameLength;
private LengthField lengthField; private LengthField lengthField;
@ -72,6 +84,13 @@ public class NettyConfig {
public Builder() { public Builder() {
} }
public Builder setIdleStateTime(int readerIdleTime, int writerIdleTime, int allIdleTime) {
this.readerIdleTime = readerIdleTime;
this.writerIdleTime = writerIdleTime;
this.allIdleTime = allIdleTime;
return this;
}
public Builder setPort(int port) { public Builder setPort(int port) {
this.port = port; this.port = port;
return this; return this;
@ -133,6 +152,9 @@ public class NettyConfig {
public NettyConfig build() { public NettyConfig build() {
return new NettyConfig( return new NettyConfig(
this.readerIdleTime,
this.writerIdleTime,
this.allIdleTime,
this.port, this.port,
this.maxFrameLength, this.maxFrameLength,
this.lengthField, this.lengthField,

View File

@ -59,7 +59,7 @@ public class TCPServer {
@Override @Override
public void initChannel(NioSocketChannel channel) { public void initChannel(NioSocketChannel channel) {
channel.pipeline() channel.pipeline()
.addLast(new IdleStateHandler(4, 0, 0, TimeUnit.MINUTES)) .addLast(new IdleStateHandler(config.readerIdleTime, config.writerIdleTime, config.allIdleTime, TimeUnit.SECONDS))
.addLast("frameDecoder", frameDecoder()) .addLast("frameDecoder", frameDecoder())
.addLast("decoder", messageDecoderWrapper) .addLast("decoder", messageDecoderWrapper)
.addLast("encoder", messageEncoderWrapper) .addLast("encoder", messageEncoderWrapper)

View File

@ -100,7 +100,7 @@ public class TCPServerHandler extends ChannelInboundHandlerAdapter {
public void exceptionCaught(ChannelHandlerContext ctx, Throwable e) { public void exceptionCaught(ChannelHandlerContext ctx, Throwable e) {
Session session = ctx.channel().attr(Session.KEY).get(); Session session = ctx.channel().attr(Session.KEY).get();
if (e instanceof IOException) if (e instanceof IOException)
log.warn(">>>>>终端主动断开连接: {} {}", e.getMessage(), session); log.warn("<<<<<终端断开连接{} {}", session, e.getMessage());
else else
log.warn(">>>>>消息处理异常" + session, e); log.warn(">>>>>消息处理异常" + session, e);
} }
@ -110,9 +110,9 @@ public class TCPServerHandler extends ChannelInboundHandlerAdapter {
if (evt instanceof IdleStateEvent) { if (evt instanceof IdleStateEvent) {
IdleStateEvent event = (IdleStateEvent) evt; IdleStateEvent event = (IdleStateEvent) evt;
IdleState state = event.state(); IdleState state = event.state();
if (state == IdleState.READER_IDLE || state == IdleState.WRITER_IDLE) { if (state == IdleState.READER_IDLE || state == IdleState.WRITER_IDLE || state == IdleState.ALL_IDLE) {
Session session = ctx.channel().attr(Session.KEY).get(); Session session = ctx.channel().attr(Session.KEY).get();
log.warn("<<<<<终端主动断开连接{}", session); log.warn(">>>>>终端心跳超时{} {}", session, state);
ctx.close(); ctx.close();
} }
} }