netty fix bug

This commit is contained in:
limqhz
2020-05-23 11:02:46 +08:00
parent 86cc9aab2d
commit 15e948d17e
3 changed files with 18 additions and 7 deletions

View File

@@ -1,10 +1,13 @@
package com.sv.netty.netty; package com.sv.netty.netty;
import com.sv.netty.utils.JsonMapper; import com.sv.netty.utils.JsonMapper;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState; import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent; 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 { 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."; 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() { public ClientHandler() {
@@ -54,7 +59,11 @@ public class ClientHandler extends ChannelInboundHandlerAdapter {
@Override @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception { throws Exception {
cause.printStackTrace(); super.exceptionCaught(ctx, cause);
logger.error("ClientHandler exceptionCaught",cause);
Channel channel = ctx.channel();
if(channel.isActive()) {
ctx.close(); ctx.close();
} }
}
} }

View File

@@ -3,10 +3,7 @@ package com.sv.netty.netty;
import com.sv.netty.config.Constant; import com.sv.netty.config.Constant;
import com.sv.netty.config.SpringContextHolder; import com.sv.netty.config.SpringContextHolder;
import com.sv.netty.service.MessageService; import com.sv.netty.service.MessageService;
import io.netty.channel.ChannelHandler; import io.netty.channel.*;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.timeout.IdleStateEvent; import io.netty.handler.timeout.IdleStateEvent;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -83,6 +80,11 @@ public class ServerHandler extends ChannelInboundHandlerAdapter {
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception { throws Exception {
super.exceptionCaught(ctx, cause); super.exceptionCaught(ctx, cause);
logger.error("ServerHandler exceptionCaught",cause);
Channel channel = ctx.channel();
if(channel.isActive()) {
ctx.close();
}
} }
/** /**

View File

@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF8"?> <?xml version="1.0" encoding="UTF8"?>
<configuration> <configuration>
<jmxConfigurator /> <jmxConfigurator />
<property name="LOG_HOME" value="/home/log/pad_logs"/> <property name="LOG_HOME" value="~/home/log/pad_logs"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">