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;
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();
super.exceptionCaught(ctx, cause);
logger.error("ClientHandler exceptionCaught",cause);
Channel channel = ctx.channel();
if(channel.isActive()) {
ctx.close();
}
}
}

View File

@@ -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();
}
}
/**

View File

@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF8"?>
<configuration>
<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">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">