Netty--ChannelHandler和ChannelPipeline
生活随笔
收集整理的這篇文章主要介紹了
Netty--ChannelHandler和ChannelPipeline
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
ChannelHandler
類圖
ChannelHandler
public interface ChannelHandler {void handlerAdded(ChannelHandlerContext ctx) throws Exception;void handlerRemoved(ChannelHandlerContext ctx) throws Exception; }ChannelInboundHandler
inbound事件:從I/O線程流向用戶業務handler的事件。
public interface ChannelInboundHandler extends ChannelHandler {void channelRegistered(ChannelHandlerContext ctx) throws Exception;void channelUnregistered(ChannelHandlerContext ctx) throws Exception;void channelActive(ChannelHandlerContext ctx) throws Exception;void channelInactive(ChannelHandlerContext ctx) throws Exception;void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception;void channelReadComplete(ChannelHandlerContext ctx) throws Exception;void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception;void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception;/*** Gets called if a {@link Throwable} was thrown.*/@Override@SuppressWarnings("deprecation")void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception; }ChannelOutboundHandler
outbound事件:由用戶或代碼發起的I/O操作事件。
public interface ChannelOutboundHandler extends ChannelHandler {void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) throws Exception;void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress,SocketAddress localAddress, ChannelPromise promise) throws Exception;void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception;void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception;void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception;void read(ChannelHandlerContext ctx) throws Exception;void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception;void flush(ChannelHandlerContext ctx) throws Exception; }ByteToMessageDecoder
用于把讀取到的字節數組或字節緩沖區解析為業務POJO。
MessageToMessageDecoder
MessageToMessageDecoder為二次解碼器,用于把一個對象二次解析為另一個對象。
LineBasedFrameDecoder
通過長度處理半包,粘包問題
ChannelPipeline
Netty將Channel的數據管道抽象為ChannelPipeline,消息在ChannelPipeline中流動和傳遞。ChannelPipeline持有I/O事件攔截器ChannelHandler的鏈表,由ChannelHandler對I/O事件進行處理。
在ChannelPipeline 中ChannelHandler由ChannelHandlerContext進行了封裝,包含next,prev指針。
volatile AbstractChannelHandlerContext next;volatile AbstractChannelHandlerContext prev;調用事件時,調用next或者prev的同名事件。
static void invokeChannelRegistered(final AbstractChannelHandlerContext next) {EventExecutor executor = next.executor();if (executor.inEventLoop()) {next.invokeChannelRegistered();} else {executor.execute(new Runnable() {@Overridepublic void run() {next.invokeChannelRegistered();}});}}ChannelPipeline 包含2個屬性:head,tail,當fireXXX()時,調用head或tail的相關方法。
final AbstractChannelHandlerContext head;final AbstractChannelHandlerContext tail; public final ChannelPipeline fireChannelUnregistered() {AbstractChannelHandlerContext.invokeChannelUnregistered(head);return this;}?
總結
以上是生活随笔為你收集整理的Netty--ChannelHandler和ChannelPipeline的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Netty--Future和Promis
- 下一篇: Java开发增强