From 15e948d17e255d5f52981e89ee380a2938953edd Mon Sep 17 00:00:00 2001 From: limqhz Date: Sat, 23 May 2020 11:02:46 +0800 Subject: [PATCH] netty fix bug --- .../main/java/com/sv/netty/netty/ClientHandler.java | 13 +++++++++++-- .../main/java/com/sv/netty/netty/ServerHandler.java | 10 ++++++---- netty-pad/src/main/resources/logback.xml | 2 +- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/netty-pad/src/main/java/com/sv/netty/netty/ClientHandler.java b/netty-pad/src/main/java/com/sv/netty/netty/ClientHandler.java index 077ef92..56c469d 100644 --- a/netty-pad/src/main/java/com/sv/netty/netty/ClientHandler.java +++ b/netty-pad/src/main/java/com/sv/netty/netty/ClientHandler.java @@ -1,10 +1,13 @@ package com.sv.netty.netty; import com.sv.netty.utils.JsonMapper; +import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.handler.timeout.IdleState; import io.netty.handler.timeout.IdleStateEvent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * 客户端处理器 @@ -13,6 +16,8 @@ import io.netty.handler.timeout.IdleStateEvent; */ public class ClientHandler extends ChannelInboundHandlerAdapter { + private static Logger logger = LoggerFactory.getLogger(ClientHandler.class); + private static final String MESSAGE = "Netty is a NIO client server framework which enables quick and easy development of network applications such as protocol servers and clients."; public ClientHandler() { @@ -54,7 +59,11 @@ public class ClientHandler extends ChannelInboundHandlerAdapter { @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - cause.printStackTrace(); - ctx.close(); + super.exceptionCaught(ctx, cause); + logger.error("ClientHandler exceptionCaught",cause); + Channel channel = ctx.channel(); + if(channel.isActive()) { + ctx.close(); + } } } diff --git a/netty-pad/src/main/java/com/sv/netty/netty/ServerHandler.java b/netty-pad/src/main/java/com/sv/netty/netty/ServerHandler.java index 9b0b641..43e1156 100644 --- a/netty-pad/src/main/java/com/sv/netty/netty/ServerHandler.java +++ b/netty-pad/src/main/java/com/sv/netty/netty/ServerHandler.java @@ -3,10 +3,7 @@ package com.sv.netty.netty; import com.sv.netty.config.Constant; import com.sv.netty.config.SpringContextHolder; import com.sv.netty.service.MessageService; -import io.netty.channel.ChannelHandler; -import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.ChannelInboundHandlerAdapter; -import io.netty.channel.ChannelPipeline; +import io.netty.channel.*; import io.netty.handler.timeout.IdleStateEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -83,6 +80,11 @@ public class ServerHandler extends ChannelInboundHandlerAdapter { public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { super.exceptionCaught(ctx, cause); + logger.error("ServerHandler exceptionCaught",cause); + Channel channel = ctx.channel(); + if(channel.isActive()) { + ctx.close(); + } } /** diff --git a/netty-pad/src/main/resources/logback.xml b/netty-pad/src/main/resources/logback.xml index e7fb94d..799dc1d 100644 --- a/netty-pad/src/main/resources/logback.xml +++ b/netty-pad/src/main/resources/logback.xml @@ -1,7 +1,7 @@ - +