netty中pipeline的handler添加刪除分析
添加
ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.group(bossGroup,workGroup) .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>(){ @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new MyServerHandler()); } }); ChannelFuture channelFuture = serverBootstrap.bind(8899).sync(); channelFuture.channel().closeFuture().sync();
分析pipeline.addLast(new MyServerHandler())
中的addLast
。
首先通過channel
拿到當(dāng)前的pipline
, 拿到pipeline
之后再為其添加handler
, 因為channel
初始化默認創(chuàng)建的是DefualtChannelPipeline
DefaultChannelPipeline.addLast(ChannelHandler... handlers)
public final ChannelPipeline addLast(ChannelHandler... handlers) { return addLast(null, handlers); }
public final ChannelPipeline addLast(EventExecutorGroup executor, ChannelHandler... handlers) { if (handlers == null) { throw new NullPointerException("handlers"); } for (ChannelHandler h: handlers) { if (h == null) { break; } addLast(executor, null, h); } return this; }
這里的handlers
只有一個
public final ChannelPipeline addLast(EventExecutorGroup group, String name, ChannelHandler handler) { final AbstractChannelHandlerContext newCtx; synchronized (this) { //判斷handler是否被重復(fù)添加(1) checkMultiplicity(handler); //創(chuàng)建一個HandlerContext并添加到列表(2) newCtx = newContext(group, filterName(name, handler), handler); //添加HandlerContext(3) addLast0(newCtx); //是否已注冊 if (!registered) { newCtx.setAddPending(); callHandlerCallbackLater(newCtx, true); return this; } EventExecutor executor = newCtx.executor(); if (!executor.inEventLoop()) { newCtx.setAddPending(); //回調(diào)用戶事件 executor.execute(new Runnable() { @Override public void run() { callHandlerAdded0(newCtx); } }); return this; } } //回調(diào)添加事件(4) callHandlerAdded0(newCtx); return this; }
分為四個步驟:
- 重復(fù)添加驗證
- 創(chuàng)建一個
HandlerContext
并添加到列表 - 添加
context
- 回調(diào)添加事件
checkMultiplicity(handler)重復(fù)添加驗證
private static void checkMultiplicity(ChannelHandler handler) { if (handler instanceof ChannelHandlerAdapter) { ChannelHandlerAdapter h = (ChannelHandlerAdapter) handler; if (!h.isSharable() && h.added) { throw new ChannelPipelineException( h.getClass().getName() + " is not a @Sharable handler, so can't be added or removed multiple times."); } //滿足條件設(shè)置為true, 代表已添加 h.added = true; } }
- 首先判斷是不是
ChannelHandlerAdapter
類型, 因為我們自定義的handler
通常會直接或者間接的繼承該接口, 所以這里為true
拿到handler
之后轉(zhuǎn)換成ChannelHandlerAdapter
類型。 - 然后進行條件判斷
if (!h.isSharable() && h.added)
代表如果不是共享的handler
, 并且是未添加狀態(tài), 則拋出異常。
isSharable()
public boolean isSharable() { Class<?> clazz = getClass(); Map<Class<?>, Boolean> cache = InternalThreadLocalMap.get().handlerSharableCache(); Boolean sharable = cache.get(clazz); if (sharable == null) { //如果這個類注解了Sharable.class, 說明這個類會被多個channel共享 sharable = clazz.isAnnotationPresent(Sharable.class); cache.put(clazz, sharable); } return sharable; }
- 首先拿到當(dāng)前
handler
的class
對象。 - 然后再從
netty
自定義的一個ThreadLocalMap
對象中獲取一個盛放handler
的class
對象的map
, 并獲取其value
。 - 如果
value
值為空, 則會判斷是否被Sharable
注解, 并將自身handler
的class
對象和判斷結(jié)果存入map
對象中, 最后返回判斷結(jié)果。 - 這說明了被
Sharable
注解的handler
是一個共享handler
。 - 從這個邏輯我們可以判斷, 共享對象是可以重復(fù)添加的。
回到DefaultChannelPipeline.addLast
,如果是共享對象或者沒有被添加, 則將ChannelHandlerAdapter
的added
設(shè)置為true
, 代表已添加分析完了重復(fù)添加驗證, 回到addLast
方法中, 我們看第二步, 創(chuàng)建一個HandlerContext
并添加到列表
newCtx = newContext(group, filterName(name, handler), handler)
newCtx = newContext(group, filterName(name, handler), handler)
首先看filterName(name, handler)方法, 這個方法是判斷添加handler的name是否重復(fù)
filterName(name, handler)
首先看filterName(name, handler)
方法, 這個方法是判斷添加handler
的name
是否重復(fù)
private String filterName(String name, ChannelHandler handler) { if (name == null) { //沒有名字創(chuàng)建默認名字 return generateName(handler); } //檢查名字是否重復(fù) checkDuplicateName(name); return name; }
因為我們添加handler
時候, 不一定會給handler
命名, 所以這一步name
有可能是null
, 如果是null
, 則創(chuàng)建一個默認的名字, 這里創(chuàng)建名字的方法就不分析了
checkDuplicateName(name)
private void checkDuplicateName(String name) { //不為空 if (context0(name) != null) { throw new IllegalArgumentException("Duplicate handler name: " + name); } }
繼續(xù)跟進分析context0(name)
方法
context0(name)
private AbstractChannelHandlerContext context0(String name) { //遍歷pipeline AbstractChannelHandlerContext context = head.next; while (context != tail) { //發(fā)現(xiàn)name相同, 說明存在handler if (context.name().equals(name)) { //返回 return context; } context = context.next; } return null; }
這里的邏輯就是將pipeline
中, 從head
節(jié)點往下遍歷HandlerContext
, 一直遍歷到tail
, 如果發(fā)現(xiàn)名字相同則會認為重復(fù)并返回HandlerContext
對象。
繼續(xù)跟到newContext(group, filterName(name, handler), handler)
方法中。
newContext(EventExecutorGroup group, String name, ChannelHandler handler)
private AbstractChannelHandlerContext newContext(EventExecutorGroup group, String name, ChannelHandler handler) { return new DefaultChannelHandlerContext(this, childExecutor(group), name, handler); }
可以看到創(chuàng)建了一個DefaultChannelHandlerContext
對象, 構(gòu)造方法的參數(shù)中, 第一個this
代表當(dāng)前的pipeline
對象, group
為null
, 所以childExecutor(group)
也會返回null
, name
為handler
的名字, handler
為新添加的handler
對象
new DefaultChannelHandlerContext(this, childExecutor(group), name, handler)
DefaultChannelHandlerContext( DefaultChannelPipeline pipeline, EventExecutor executor, String name, ChannelHandler handler) { super(pipeline, executor, name, isInbound(handler), isOutbound(handler)); if (handler == null) { throw new NullPointerException("handler"); } this.handler = handler; }
- 首先調(diào)用了父類的構(gòu)造方法, 之后將
handler
賦值為自身handler
的成員變量,HandlerConext
和handler
關(guān)系在此也展現(xiàn)了出來, 是一種組合關(guān)系 - 父類的構(gòu)造方法, 有這么兩個參數(shù):
isInbound(handler)
,isOutbound(handler)
, 這兩個參數(shù)意思是判斷需要添加的handler
是inboundHandler
還是outBoundHandler
isInbound(handler)
private static boolean isInbound(ChannelHandler handler) { return handler instanceof ChannelInboundHandler; }
這里通過是否實現(xiàn)ChannelInboundHandler
接口來判斷是否為inboundhandler
isOutbound(handler)
private static boolean isOutbound(ChannelHandler handler) { return handler instanceof ChannelOutboundHandler; }
通過判斷是否實現(xiàn)ChannelOutboundHandler
接口判斷是否為outboundhandler
在跟到其父類AbstractChannelHandlerContext的構(gòu)造方法中
AbstractChannelHandlerContext(DefaultChannelPipeline pipeline, EventExecutor executor, String name, boolean inbound, boolean outbound) { this.name = ObjectUtil.checkNotNull(name, "name"); this.pipeline = pipeline; this.executor = executor; this.inbound = inbound; this.outbound = outbound; ordered = executor == null || executor instanceof OrderedEventExecutor; }
之前tail
節(jié)點和head
節(jié)點創(chuàng)建的時候也執(zhí)行到了這里,初始化了name
, pipeline
, 以及標(biāo)識添加的handler
是inboundhanlder
還是outboundhandler
。
回到DefaultChannelPipeline.addLast
,分析完了創(chuàng)建HandlerContext
的相關(guān)邏輯, 我們繼續(xù)跟第三步, 添加HandlerContext
addLast0(newCtx)
private void addLast0(AbstractChannelHandlerContext newCtx) { //拿到tail節(jié)點的前置節(jié)點 AbstractChannelHandlerContext prev = tail.prev; //當(dāng)前節(jié)點的前置節(jié)點賦值為tail節(jié)點的前置節(jié)點 newCtx.prev = prev; //當(dāng)前節(jié)點的下一個節(jié)點賦值為tail節(jié)點 newCtx.next = tail; //tail前置節(jié)點的下一個節(jié)點賦值為當(dāng)前節(jié)點 prev.next = newCtx; //tail節(jié)點的前一個節(jié)點賦值為當(dāng)前節(jié)點 tail.prev = newCtx; }
做了一個指針的指向操作, 將新添加的handlerConext
放在tail
節(jié)點之前, 之前tail
節(jié)點的上一個節(jié)點之后, 如果是第一次添加handler
, 那么添加后的結(jié)構(gòu)入下圖所示
添加完handler
之后, 這里會判斷當(dāng)前channel
是否已經(jīng)注冊, 這部分邏輯之后再進行分析,先接著繼續(xù)執(zhí)行。
之后會判斷當(dāng)前線程線程是否為eventLoop
線程, 如果不是eventLoop
線程, 就將添加回調(diào)事件封裝成task
交給eventLoop
線程執(zhí)行, 否則, 直接執(zhí)行添加回調(diào)事件callHandlerAdded0(newCtx)
。
callHandlerAdded0(newCtx)
private void callHandlerAdded0(final AbstractChannelHandlerContext ctx) { try { ctx.handler().handlerAdded(ctx); ctx.setAddComplete(); } catch (Throwable t) { /** * 省略 * */ } }
分析ctx.handler().handlerAdded(ctx)
,其中ctx
是我們新創(chuàng)建的HandlerContext
, 通過handler()
方法拿到綁定的handler
, 也就是新添加的handler
, 然后執(zhí)行handlerAdded(ctx)
方法, 如果我們沒有重寫這個方法, 則會執(zhí)行父類的該方法。
ChannelHandlerAdapter.(ChannelHandlerContext ctx)
public void handlerAdded(ChannelHandlerContext ctx) throws Exception { // NOOP }
沒做任何操作, 也就是如果我們沒有重寫該方法時, 如果添加handler
之后將不會做任何操作, 這里如果我們需要做一些業(yè)務(wù)邏輯, 可以通過重寫該方法進行實現(xiàn)
刪除
刪除的邏輯和添加的邏輯相同,區(qū)別刪除是將pipeline
的雙向鏈表的節(jié)點去掉。這里就不詳細的分析。
以上就是netty中pipelinehandler添加刪除分析的詳細內(nèi)容,更多關(guān)于netty pipeline handler的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java Web使用簡單的批處理操作(記事本+Tomcat)
這篇文章主要介紹了Java Web使用簡單的批處理操作 ,需要的朋友可以參考下2014-10-10Java詳解ScriptEngine接口動態(tài)執(zhí)行JS腳本
ScriptEngine是基本接口,其方法必須在本規(guī)范的每個實現(xiàn)中完全起作用。這些方法提供基本腳本功能。 寫入這個簡單接口的應(yīng)用程序可以在每個實現(xiàn)中進行最少的修改。 它包括執(zhí)行腳本的方法,以及設(shè)置和獲取值的方法2022-08-08關(guān)于BufferedReader的read()和readLine()的區(qū)別
這篇文章主要介紹了關(guān)于BufferedReader的read()和readLine()的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12