您现在的位置:诗歌范文 > 西方文学 > 正文

Netty.docs New and noteworthy in 4.0

Netty.docs New and noteworthy in 4.0

Theyareused,,,,→Inbound,Downstream→OutboundThetermsupstreamanddownstreaminboundandoutbound,ChannelHandlerwasjustataginterface,andChannelUpstreamHandler,ChannelDownstreamHandler,,ChannelHandlermergesLifeCycleAwareChannelHandleralongwithacouplemoremethodswhichareusefultobothaninboundandanoutboundhandler:{();();(,);}Thefollowingdiagramdepictsthenewtypehierarchy:,everyI//write,,itoftenwastherootcauseofGCpressureanduncertainty()andhandleDownstream(),:Before:handleUpstream(ctx,e);handleDownstream(ctx,e);After:channelRegistered(ctx);channelUnregistered(ctx);channelActive(ctx);channelInactive(ctx);channelRead(ctx,message);bind(ctx,localAddress,promise);connect(ctx,remoteAddress,localAddress,promise);disconnect(ctx,promise);close(ctx,promise);deregister(ctx,promise);write(ctx,message,promise);flush(ctx);read(ctx);ChannelHandlerContexthasalsobeenchangedtoreflectthechangesmentionedabove:Before:ctxsendUpstream(evt);After:ctxfireChannelRead(receivedMessage);Allthesechangesm(),atleastthreeChannelStateEventsaretriggered:channelOpen,channelBound,,atleast3more:channelDisconnected,channelUnbound,,it,channelBound,,channelUnbound,,()andisConnected()havebeenmergedtoisActive().NotethatchannelRegisteredandc,deregistration,andre-registrationofaChannel,asillustratedbelow:write()()whichexplicitlyflushestheoutboundbufferofaChannel,andwrite(),,(),youcoulduseashortcutmethodwriteAndFlush().(boolean).ItintroducedcomplicatedinteractionsbetweenChannelHandlersandtheh,anewoutboundoperationcalledread()().setAutoRead(false),Nettywillnotreadanythinguntilyouexplicitlyinvoketheread()()operationyouissueiscompleteandthechannelagainstopsreading,aninboundeventcalledchannelReadSuspended()willbetriggeredsothatyoucanre-issueanotherread()()/()operationwhentheauto-readflagisnotset,ahalf-closedsocket,(),(..)willreturn-1,(),(..),()method,andausercansettheALLOW_HALF_CLOSUREChannelOptiontopreve(..)/,aChanneliscreatedbyaChannelFactoryandthenewlycreatedChannelisautomaticallyregisteredtoahiddenI/,anewChannelisnotregistered()(/Othreads),ausercanregisterdifferentChannelimplementationstothesameEventLoopGroup,,youcanrunaNIOserversocket,NIOclientsockets,NIOUDPsockets,andin-VMlocalchannelsinthesameI/·toanI/,itiscompletelytiedtoasingleI/,ausercanderegisteraChannelfromitsI/,youcantakeadvantageofhigh-levelnon-blockingI/ONettyprovidestodealwithcomplexprotocols,andthenlaterderegistertheChannelan,;SocketChannelmySocketSocketChannelopen();(mySocket);group;groupregister(ch);()sync();();myFiletransferFrom(mySocket,);;anotherGroupregister(ch);SchedulinganarbitrarytasktoberunbyanI/OthreadWhenaChannelisregisteredtoanEventLoopGroup,theChannelisactuallyanI/Othreadwheretheuser,whichwillbeexplainedlater,itbecameextremelyeasiertowriteathread-safehandler.{(,,){ctxwrite(msg,p);()schedule((p),,SECONDS);}}{([]){RunanarbitrarytaskfromanI/;chexecutor()execute((){});}}SimplifiedshutdownTheresnomorereleaseExternalResources().YoucancloseallopenchannelsimmediatelyandmakeallI/().Type-safeChannelOptio,(true).()e,,,heorshewillencounteraC,;Before:cfgsetOption(tcpNoDelay,);cfgsetOption(tcpNoDelay,);RuntimeClassCastExceptioncfgsetOption(tcpNoDelays,);Typointheoptionname-ignoredsilentlyAfter:cfgsetOption(TCP_NODELAY,);cfgsetOption(TCP_NODELAY,);CompileerrorAttributeMapInresponsetouserdemand,,whichChannelandChannelHandlerContextextend,,rbage-collected.{valueOf();(){ctxattr()set(());ctxfireChannelRegistered();}(,){statectxattr()get();}}NewbootstrapAPIThebootstrapAPIhasbeenrewrittenfromscratchalthoughitspurposestayssame;itperformsthetypicalstepsrequiredtomakeaserveroraclientupandrunning,([]args)throws{();workerGroup();{b();bgroup(bossGroup,workerGroup).channel(class).option(SO_BACKLOG,).localAddress().childOption(TCP_NODELAY,).childHandler((){(){chpipeline()addLast(handler1,handler2,);}});()sync();()closeFuture()sync();}{();workerGroupshutdownGracefully();()sync();workerGroupterminationFuture()sync();}}ChannelPipelineFactory→ChannelInitializerAsyounoticedintheexampleabove,,,theNettyprojectteamconcludedthatithasnobenefitforausertocreatehiso,→ChannelFutureandChannelProsoperationexplicit,butalsomakesitmoresafetousethereturnedChannelFutureinachain(likefiltering),,somemethodhoutworryingtoomuchaboutthreadsafety.。

上一篇: 深入HBase架构解析(一)
下一篇:没有了
回到顶部