摘要:如果有處理耗時(shí)任務(wù)的應(yīng)該怎么辦可以專(zhuān)門(mén)開(kāi)一個(gè)線(xiàn)程池,來(lái)專(zhuān)門(mén)處理耗時(shí)業(yè)務(wù)。如果是在線(xiàn)程,如果想處理耗時(shí)任務(wù)邏輯,那么就需要新建一個(gè)并調(diào)用他的相關(guān)方法其本質(zhì)是一個(gè)用來(lái)處理事件的線(xiàn)程,其本質(zhì)是一個(gè)線(xiàn)程池。
netty 處理耗時(shí)的任務(wù)邏輯,是不能在IO線(xiàn)程處理,因?yàn)檫@會(huì)造成堵塞,將會(huì)嚴(yán)重影響性能。那怎么區(qū)分IO線(xiàn)程呢?
答案就是EventLoop,EventLoop用來(lái)處理IO線(xiàn)程,因此耗時(shí)任務(wù)的handler不要在EventLoop里面處理。
以下面代碼為例:
bossGroup = new NioEventLoopGroup(); workerGroup = new NioEventLoopGroup(); try { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) ....
其中workerGroup就是專(zhuān)門(mén)用來(lái)處理IO線(xiàn)程業(yè)務(wù)邏輯的,例如執(zhí)行一些 編碼、解碼 等不耗時(shí)的業(yè)務(wù)處理。
如果有處理耗時(shí)任務(wù)的handler應(yīng)該怎么辦?
可以專(zhuān)門(mén)開(kāi)一個(gè)線(xiàn)程池,來(lái)專(zhuān)門(mén)處理耗時(shí)業(yè)務(wù)。Netty Api 已經(jīng)提供了一些說(shuō)明,http://netty.io/4.1/api/index...,ChannelPipeline中 可以找到如下描述:
A user is supposed to have one or more ChannelHandlers in a pipeline to receive I/O events (e.g. read) and to request I/O operations (e.g. write and close). For example, a typical server will have the following handlers in each channel"s pipeline, but your mileage may vary depending on the complexity and characteristics of the protocol and business logic:
Protocol Decoder - translates binary data (e.g. ByteBuf) into a Java object.
Protocol Encoder - translates a Java object into binary data.
Business Logic Handler - performs the actual business logic (e.g. database access).
and it could be represented as shown in the following example:
static final EventExecutorGroup group = new DefaultEventExecutorGroup(16);
...
ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("decoder", new MyProtocolDecoder()); pipeline.addLast("encoder", new MyProtocolEncoder()); // Tell the pipeline to run MyBusinessLogicHandler"s event handler methods // in a different thread than an I/O thread so that the I/O thread is not blocked by // a time-consuming task. // If your business logic is fully asynchronous or finished very quickly, you don"t // need to specify a group. pipeline.addLast(group, "handler", new MyBusinessLogicHandler());
其中EventExecutorGroup 就是專(zhuān)門(mén)來(lái)處理耗時(shí)業(yè)務(wù)的線(xiàn)程池。
在實(shí)際生產(chǎn)環(huán)境中,我們可能會(huì)碰到 需要臨時(shí)執(zhí)行也行計(jì)劃任務(wù),,這些任務(wù)如果不耗時(shí),我們可以通過(guò)channel提供的計(jì)劃任務(wù)方法處理:
future = channel.eventLoop.scheduleAtFixedRate(new Runnable() { @Override public void run() { //邏輯代碼,非耗時(shí)任務(wù) } }, 6, 6, TimeUnit.HOURS); ....
如果計(jì)劃任務(wù)里面的邏輯比較耗時(shí),那么就不能再用eventLoop,因?yàn)檫@會(huì)阻塞IO線(xiàn)程。如果是通過(guò)pipeline.addLast(group, "handler", new MyBusinessLogicHandler()); 這種方式添加的業(yè)務(wù)線(xiàn)程我們可以使用下面的方式添加計(jì)劃任務(wù)方法實(shí)現(xiàn):
***future = ctx.executor().scheduleAtFixedRate(new Runnable() { @Override public void run() { } }, 6, 6, TimeUnit.HOURS);***
...
netty 源碼
public EventExecutor executor() { return (EventExecutor)(this.executor == null?this.channel().eventLoop():this.executor); }
如果this.executor為null,就返回channel().eventLoop(),這個(gè)是io讀寫(xiě)線(xiàn)程,肯定是不能執(zhí)行耗時(shí)任務(wù)的。
如果不為空,那么是怎么傳進(jìn)來(lái)的呢?
DefaultChannelPipeline
public final ChannelPipeline addLast(EventExecutorGroup group, String name, ChannelHandler handler) { final AbstractChannelHandlerContext newCtx; synchronized(this) { checkMultiplicity(handler); newCtx = this.newContext(group, this.filterName(name, handler), handler);
DefaultChannelPipeline 的addLast(EventExecutorGroup group, String name, ChannelHandler handler)為例,該方法部分源碼如下
@Override public final ChannelPipeline addLast(EventExecutorGroup group, String name, ChannelHandler handler) { final AbstractChannelHandlerContext newCtx; synchronized (this) { checkMultiplicity(handler); newCtx = newContext(group, filterName(name, handler), handler); addLast0(newCtx); ...代碼略去...
private AbstractChannelHandlerContext newContext(EventExecutorGroup group, String name, ChannelHandler handler) { return new DefaultChannelHandlerContext(this, this.childExecutor(group), name, handler); }
通過(guò)源碼發(fā)現(xiàn):其實(shí)就是我們?cè)谔砑觝andler時(shí)指定的DefaultEventExecutorGroup。
、所以結(jié)論是:如果在處理耗時(shí)任務(wù)的Handler添加時(shí)用到了DefaultEventExecutorGroup是可以 ctx.executor().scheduleAtFixedRate這么用的,但是如果你再添加handler時(shí)沒(méi)有沒(méi)有指定特殊的EventExecutorGroup,是不能執(zhí)行耗時(shí)任務(wù)的。
如果是在IO線(xiàn)程,如果想處理耗時(shí)任務(wù)邏輯,那么就需要新建一個(gè)EventExecutorGroup,并調(diào)用他的相關(guān)方法
EventLoop:其本質(zhì)是一個(gè)用來(lái)處理IO事件的線(xiàn)程,EventLoopGroup 其本質(zhì)是一個(gè)線(xiàn)程池。一個(gè)EventLoop可以和多個(gè)Channel綁定,處理多個(gè)Channel的IO事件;但是一個(gè)Channel在整個(gè)生命周期內(nèi)只會(huì)被一個(gè)EventLoop處理,這就也就保證了線(xiàn)程安全。
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/66451.html
摘要:用這種方式很好的規(guī)避了多線(xiàn)程所帶來(lái)的問(wèn)題,很值得我們借鑒那么這個(gè)怎么來(lái)的呢看一下的方法如果為,就返回。 Netty channelRegisteredChannelActive---源碼分析經(jīng)過(guò)下面的分析我們可以了解netty讀數(shù)據(jù)的一個(gè)過(guò)程,以及為什么channelActive方法、channelReadComplete方法會(huì)回調(diào)ChannelOutboundHandler的read...
摘要:為程序員金三銀四精心挑選的余道面試題與答案,歡迎大家向我推薦你在面試過(guò)程中遇到的問(wèn)題我會(huì)把大家推薦的問(wèn)題添加到下面的常用面試題清單中供大家參考。 為Java程序員金三銀四精心挑選的300余道Java面試題與答案,歡迎大家向我推薦你在面試過(guò)程中遇到的問(wèn)題,我會(huì)把大家推薦的問(wèn)題添加到下面的常用面試題清單中供大家參考。 前兩天寫(xiě)的以下博客,大家比較認(rèn)可,熱度不錯(cuò),希望可以幫到準(zhǔn)備或者正在參加...
摘要:接上篇源碼分析之三我就是大名鼎鼎的一的處理循環(huán)在中一個(gè)需要負(fù)責(zé)兩個(gè)工作第一個(gè)是作為線(xiàn)程負(fù)責(zé)相應(yīng)的操作第二個(gè)是作為任務(wù)線(xiàn)程執(zhí)行中的任務(wù)接下來(lái)我們先從操縱方面入手看一下數(shù)據(jù)是如何從傳遞到我們的中的是模型的一個(gè)實(shí)現(xiàn)并且是基于的那么從的前生今世之四 接上篇Netty 源碼分析之 三 我就是大名鼎鼎的 EventLoop(一) Netty 的 IO 處理循環(huán) 在 Netty 中, 一個(gè) Even...
摘要:在系統(tǒng)正常運(yùn)行時(shí),可以變更爬蟲(chóng)的配置,一旦實(shí)時(shí)監(jiān)控爬蟲(chóng)出現(xiàn)異常,可實(shí)時(shí)修正配置進(jìn)行干預(yù)。從數(shù)據(jù)庫(kù)中實(shí)時(shí)讀取配置信息,響應(yīng)業(yè)務(wù)層的配置請(qǐng)求。處理系統(tǒng)通過(guò)服務(wù)層,每次去取配置信息可能維護(hù)人員在實(shí)時(shí)修正及待抓取的列表進(jìn)行處理。 showImg(https://segmentfault.com/img/bVLa4V?w=960&h=540); 一 ?緣起 在我工作的多家公司,有眾多的領(lǐng)域,如房...
閱讀 2332·2021-11-23 09:51
閱讀 3764·2021-11-11 10:57
閱讀 1410·2021-10-09 09:43
閱讀 2498·2021-09-29 09:35
閱讀 2028·2019-08-30 15:54
閱讀 1798·2019-08-30 15:44
閱讀 3194·2019-08-30 13:20
閱讀 1703·2019-08-30 11:19