增加心跳超时配置项
parent
509051196b
commit
cb8151889b
|
@ -16,6 +16,9 @@ import io.netty.channel.ChannelInboundHandlerAdapter;
|
|||
*/
|
||||
public class NettyConfig {
|
||||
|
||||
protected final int readerIdleTime;
|
||||
protected final int writerIdleTime;
|
||||
protected final int allIdleTime;
|
||||
protected final int port;
|
||||
protected final int maxFrameLength;
|
||||
protected final LengthField lengthField;
|
||||
|
@ -28,7 +31,10 @@ public class NettyConfig {
|
|||
protected final SessionManager sessionManager;
|
||||
protected final SessionListener sessionListener;
|
||||
|
||||
private NettyConfig(int port,
|
||||
private NettyConfig(int readerIdleTime,
|
||||
int writerIdleTime,
|
||||
int allIdleTime,
|
||||
int port,
|
||||
int maxFrameLength,
|
||||
LengthField lengthField,
|
||||
Delimiter[] delimiter,
|
||||
|
@ -39,6 +45,9 @@ public class NettyConfig {
|
|||
SessionManager sessionManager,
|
||||
SessionListener sessionListener
|
||||
) {
|
||||
this.readerIdleTime = readerIdleTime;
|
||||
this.writerIdleTime = writerIdleTime;
|
||||
this.allIdleTime = allIdleTime;
|
||||
this.port = port;
|
||||
this.maxFrameLength = maxFrameLength;
|
||||
this.lengthField = lengthField;
|
||||
|
@ -58,6 +67,9 @@ public class NettyConfig {
|
|||
|
||||
public static class Builder {
|
||||
|
||||
private int readerIdleTime = 240;
|
||||
private int writerIdleTime = 0;
|
||||
private int allIdleTime = 0;
|
||||
private int port;
|
||||
private int maxFrameLength;
|
||||
private LengthField lengthField;
|
||||
|
@ -72,6 +84,13 @@ public class NettyConfig {
|
|||
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) {
|
||||
this.port = port;
|
||||
return this;
|
||||
|
@ -133,6 +152,9 @@ public class NettyConfig {
|
|||
|
||||
public NettyConfig build() {
|
||||
return new NettyConfig(
|
||||
this.readerIdleTime,
|
||||
this.writerIdleTime,
|
||||
this.allIdleTime,
|
||||
this.port,
|
||||
this.maxFrameLength,
|
||||
this.lengthField,
|
||||
|
|
|
@ -59,7 +59,7 @@ public class TCPServer {
|
|||
@Override
|
||||
public void initChannel(NioSocketChannel channel) {
|
||||
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("decoder", messageDecoderWrapper)
|
||||
.addLast("encoder", messageEncoderWrapper)
|
||||
|
|
|
@ -100,7 +100,7 @@ public class TCPServerHandler extends ChannelInboundHandlerAdapter {
|
|||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable e) {
|
||||
Session session = ctx.channel().attr(Session.KEY).get();
|
||||
if (e instanceof IOException)
|
||||
log.warn(">>>>>终端主动断开连接: {} {}", e.getMessage(), session);
|
||||
log.warn("<<<<<终端断开连接{} {}", session, e.getMessage());
|
||||
else
|
||||
log.warn(">>>>>消息处理异常" + session, e);
|
||||
}
|
||||
|
@ -110,9 +110,9 @@ public class TCPServerHandler extends ChannelInboundHandlerAdapter {
|
|||
if (evt instanceof IdleStateEvent) {
|
||||
IdleStateEvent event = (IdleStateEvent) evt;
|
||||
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();
|
||||
log.warn("<<<<<终端主动断开连接{}", session);
|
||||
log.warn(">>>>>终端心跳超时{} {}", session, state);
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue