1. Overview
1.概述
In this quick article, we’ll be looking at exception handling in Netty.
在这篇快速文章中,我们将探讨Netty的异常处理。
Simply put, Netty is a framework for building high-performance asynchronous and event-driven network applications. I/O operations are handled inside its life-cycle using callback methods.
简单地说,Netty是一个用于构建高性能异步和事件驱动的网络应用的框架。I/O操作是在其生命周期内使用回调方法处理的。
More details about the framework and how to get started with it can be found in our previous article here.
关于该框架的更多细节以及如何开始使用它,可以在我们之前的文章中找到。
2. Handling Exceptions in Netty
2.在Netty中处理异常情况
As mentioned earlier, Netty is an event-driven system and has callback methods for specific events. Exceptions are such events too.
如前所述,Netty是一个事件驱动的系统,对特定事件有回调方法。异常也是此类事件。
Exceptions can occur while processing data received from the client or during I/O operations. When this happens, a dedicated exception-caught event is fired.
在处理从客户端收到的数据时或在I/O操作期间,可能会发生异常。当这种情况发生时,会有一个专门的异常捕获事件被触发。
2.1. Handling Exceptions in the Channel of Origin
2.1.处理起源渠道中的异常情况
The exception-caught event, when fired, is handled by the exceptionsCaught() method of the ChannelInboundHandler or its adapters and subclasses.
异常捕获事件被触发后,由ChannelInboundHandler或其适配器和子类的exceptionsCaught()方法处理。
Note that the callback has been deprecated in the ChannelHandler interface. It’s now limited to the ChannelInboudHandler interface.
请注意,在ChannelHandler 接口中,回调已经被废弃。它现在仅限于ChannelInboudHandler接口。
The method accepts a Throwable object and a ChannelHandlerContext object as parameters. The Throwable object could be used to print the stack trace or get the localized error message.
该方法接受一个Throwable对象和一个ChannelHandlerContext对象作为参数。Throwable对象可用于打印堆栈跟踪或获得本地化的错误信息。
So let’s create a channel handler, ChannelHandlerA and override its exceptionCaught() with our implementation:
因此,让我们创建一个通道处理程序,ChannelHandlerA,并用我们的实现重写它的exceptionCaught() 。
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
logger.info(cause.getLocalizedMessage());
//do more exception handling
ctx.close();
}
In the code snippet above, we logged the exception message and also call the close() of the ChannelHandlerContext.
在上面的代码片段中,我们记录了异常信息,也调用了ChannelHandlerContext的close()。
This will close the channel between the server and the client. Essentially causing the client to disconnect and terminate.
这将关闭服务器和客户端之间的通道。基本上导致客户端断开连接并终止。
2.2. Propagating Exceptions
2.2.传播例外情况
In the previous section, we handled the exception in its channel of origin. However, we can actually propagate the exception on to another channel handler in the pipeline.
在上一节中,我们在其原生通道中处理了异常。然而,我们实际上可以将异常传播给管道中的另一个通道处理程序。
Instead of logging the error message and calling ctx.close(), we’ll use the ChannelHandlerContext object to fire another exception-caught event manually.
我们将使用ChannelHandlerContext对象来手动触发另一个捕获异常的事件,而不是记录错误信息并调用ctx.close()。
This will cause the exceptionCaught() of the next channel handler in the pipeline to be invoked.
这将导致管道中下一个通道处理程序的exceptionCaught()被调用。
Let’s modify the code snippet in ChannelHandlerA to propagate the event by calling the ctx.fireExceptionCaught():
让我们修改ChannelHandlerA中的代码片段,通过调用ctx.fireExceptionCaught()来传播该事件。
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
logger.info("Exception Occurred in ChannelHandler A");
ctx.fireExceptionCaught(cause);
}
Furthermore, let’s create another channel handler, ChannelHandlerB and override its exceptionCaught() with this implementation:
此外,让我们创建另一个通道处理程序,ChannelHandlerB,并用这个实现重写它的exceptionCaught() 。
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
logger.info("Exception Handled in ChannelHandler B");
logger.info(cause.getLocalizedMessage());
//do more exception handling
ctx.close();
}
In the Server class, the channels are added to the pipeline in the following order:
在Server类中,通道按照以下顺序被添加到管道中。
ch.pipeline().addLast(new ChannelHandlerA(), new ChannelHandlerB());
Propagating exception-caught events manually is useful in cases where all exceptions are being handled by one designated channel handler.
在所有的异常都由一个指定的通道处理程序来处理的情况下,手动传播捕获的异常事件是非常有用的。
3. Conclusion
3.结论
In this tutorial, we’ve looked at how to handle exceptions in Netty using the callback method and how to propagate the exceptions if needed.
在本教程中,我们已经研究了如何在Netty中使用回调方法处理异常,以及如何在需要时传播异常。
The complete source code is available over on Github.
完整的源代码可在Github上获得,。