ChannelUpstreamHandler理解

2020-06-09  本文已影响0人  追梦小蜗牛
person-walking-near-columns-3214972.jpg

介绍

处理upstream ChannelEvent,同时把ChannelEvent发送给在ChannelPipeline中的上一个或者下一个handler。
upstream ChannelEvent:是在ChannelPipeline中处理的,从第一个handler到最后一个handler。

upstream ChannelEvent

Event name Event type and condition Meaning
messageReceived MessageEvent a message object (e.g. {@link ChannelBuffer}) was received from a remote peer
exceptionCaught ExceptionEvent an exception was raised by an I/O thread or a {@link ChannelHandler}
channelOpen ChannelStateEvent a {@link Channel} is open, but not bound nor connected
channelClosed ChannelStateEvent a {@link Channel} was closed and all its related resources were released
channelBound ChannelStateEvent a {@link Channel} is open and bound to a local address, but not connected
channelUnbound ChannelStateEvent a {@link Channel} was unbound from the current local address
channelConnected ChannelStateEvent a {@link Channel} is open, bound to a local address, and connected to a remote address
writeComplete WriteCompletionEvent omething has been written to a remote peer
channelDisconnected ChannelStateEvent a {@link Channel} was disconnected from its remote peer
channelInterestChanged ChannelStateEvent a {@link Channel}'s {@link Channel#getInterestOps() interestOps} was changed

Sending the event forward (upstream)
通过ChannelHandlerContext把ChannelEvent传递给下一个handler

 void handleUpstream({@link ChannelHandlerContext} ctx, {@link ChannelEvent} e) throws Exception {
      ...
      ctx.sendUpstream(e);
      ...
  }

线程安全

一个IO线程可以用于处理多个Channel NIO
一个IO线程只能处理一个Channel OIO
OrderedMemoryAwareThreadPoolExecutor用来保证同一个Channel的Event的执行顺序
ChannelPipelineCoverage注解:all 代表可以把同一个ChannelHandler加入到多个ChannelPipeline,one代表只能加入到一个ChannelPipeline,其实all代表是线程安全、无状态,one代表线程不安全、有状态。

总结:

最近发现自己在学习业务项目的时候,分析的方式好像不对,事倍功半。比如分析一个复杂的业务系统,一上来自己就下个定义:这个太复杂了,不好理解的,以至于自己好像没有了征服的信心啦。还有就是,太过于专注点了,而没有从面开始,要分析一个大的业务系统,首先要从整体把握,走通整体流程,然后再深入到局部,熟悉具体的业务逻辑。不然只会看到冰山一角,掌控不了全局。
所以有时候,学习方法这个东西,还是要投入点时间思考的,不能一直一味的埋头苦干,适当的停下来,思考一下,优化一下自己的方式、思想。这也就是经常说的,及时的归纳总结。

上一篇下一篇

猜你喜欢

热点阅读