首页  

netty异常处理机制     所属分类 netty 浏览量 1356
入站 出站异常处理

入站异常处理

默认的ChannelHandler#exceptionCaught 把异常传给管道中的下一个处理器
如果一个异常到达了管道的尾部,被记录为未被处理 记录 warning 日志
自定义处理,重写exceptionCaught方法,直接处理掉异常或继续传播

public class InboundExceptionHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx,Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }
}

InboundExceptionHandler 放到 pipeline最后,确保入站的所有异常都会被处理

public interface ChannelInboundHandler extends ChannelHandler
void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception;

The exceptionCaught() method is only invoked for exceptions from inbound events like channelRead(), channelActive() etc.


出站异常处理

出站 I/O 操作 返回一个 ChannelFuture
几乎所有的ChannelOutboundHandler都会传递一个ChannelPromise实例,
作为ChannelFuture的子类,它也可以被监听器管控来获取异步通知,但是ChannelPromise为实时通知提供可写的方法
setSuccess setFailure


public interface ChannelPromise extends ChannelFuture, Promise< Void>
    @Override
    ChannelPromise setSuccess(Void result);

    ChannelPromise setSuccess();

    @Override
    ChannelPromise setFailure(Throwable cause);

上一篇     下一篇
netty 服务端启动日志

springboot应用安全防护

netty ChannelFuture 实例

Garbage Collection Roots

内存分析工具MAT中的重要概念

获取jvm heap dump 的几种方式