美文网首页
Interface ChannelHandlerContext

Interface ChannelHandlerContext

作者: KardelShaw | 来源:发表于2017-12-14 14:32 被阅读0次

    官方文档:

    Interface ChannelHandlerContext ------------------ Netty API Reference (4.0.54.Final)

    【简称ChannelHandlerContext------------->ctx】
    【简称ChannelHandler------------------------>handler】
    【简称ChannelPipeline------------------------>pipeline】

    Enables a ChannelHandler to interact with its ChannelPipeline and other handlers. A handler can notify the next ChannelHandler in the ChannelPipeline, modify the ChannelPipeline it belongs to dynamically.

    让handler可以和ctx的pipeline和其他handler交互。一个handle可以告知pipeline中的下一个handler,修改它所属的pipeline。

    Notify

    You can notify the closest handler in the same ChannelPipeline by calling one of the various methods provided here. Please refer to ChannelPipeline to understand how an event flows.

    你可以通过调用ctx的方法,让handler通知在同一个pipeline最近的handler。要理解事件的流向,参考ChannelPipeline。

    Modifying a pipeline

    You can get the ChannelPipeline your handler belongs to by calling pipeline(). A non-trivial application could insert, remove, or replace handlers in the pipeline dynamically at runtime.

    通过调用pipeline(),你可以获得当前handler所属的pipeline(ctx.pipeline())。一些重要的应用可以在运行时插入、删除或替换pipeline里的handler。

    Retrieving for later use

    You can keep the ChannelHandlerContext for later use, such as triggering an event outside the handler methods, even from a different thread.

    你可以保存ctx待稍后使用。例如可以在handler方法外触发事件,甚至可以从另一个线程触发。

     public class MyHandler extends ChannelDuplexHandler {
    
         private ChannelHandlerContext ctx;
    
         public void beforeAdd(ChannelHandlerContext ctx) {
             this.ctx = ctx;
         }
    
         public void login(String username, password) {
             ctx.write(new LoginMessage(username, password));
         }
         ...
     }
    

    Storing stateful information

    AttributeMap.attr(AttributeKey) allow you to store and access stateful information that is related with a handler and its context. Please refer to ChannelHandler to learn various recommended ways to manage stateful information.

    AttributeMap可以存储和访问有状态信息,这些信息与handler和ctx相关。要获知更多处理有状态信息的方法请参考ChannelHandler。

    A handler can have more than one context

    Please note that a ChannelHandler instance can be added to more than one ChannelPipeline. It means a single ChannelHandler instance can have more than one ChannelHandlerContext and therefore the single instance can be invoked with different ChannelHandlerContexts if it is added to one or more ChannelPipelines more than once.
    For example, the following handler will have as many independent AttributeKeys as how many times it is added to pipelines, regardless if it is added to the same pipeline multiple times or added to different pipelines multiple times:

    请注意:一个handler实例可以被添加到多条pipeline中。这意味着一个handler实例可以有多个ctx。因此,假如某条handler被添加到多条pipeline中,这个handler就可以在不同的ctx被调用。

    例如,下面这个handler的独立AttributeKey和它被添加到pipeline的次数一样多。无论它是多次被添加到同一条pipeline,还是多次被添加到不同的pipeline。

     public class FactorialHandler extends ChannelInboundHandlerAdapter<Integer> {
    
       private final AttributeKey<Integer> counter =
               new AttributeKey<Integer>("counter");
    
       // This handler will receive a sequence of increasing integers starting
       //这个handler会接收一个从1开始的整数递增序列。
       // from 1.
        @Override
       public void channelRead(ChannelHandlerContext ctx, Object msg) {
         Integer a = ctx.attr(counter).get();
    
         if (a == null) {
           a = 1;
         }
    
         attr.set(a * integer));
       }
     }
    
     // Different context objects are given to "f1", "f2", "f3", and "f4" even if
     // they refer to the same handler instance.  Because the FactorialHandler
     // stores its state in a context object (as an (using an AttributeKey), the factorial is
     // calculated correctly 4 times once the two pipelines (p1 and p2) are active.
     
    //f1,f2,f3,f4  4个handler会获得不同的ctx,即使它们都指向同一个handler实例。
    //因为handler在ctx对象里存储着它们的状态(当作一个AttributeKey), 
    //一旦两个pipeline激活(p1和p2),handler会被正确计算4次
    
    
     FactorialHandler fh = new FactorialHandler();
    
     ChannelPipeline p1 = Channels.pipeline();
     p1.addLast("f1", fh);
     p1.addLast("f2", fh);
    
     ChannelPipeline p2 = Channels.pipeline();
     p2.addLast("f3", fh);
     p2.addLast("f4", fh);
    

    相关文章

      网友评论

          本文标题:Interface ChannelHandlerContext

          本文链接:https://www.haomeiwen.com/subject/kolkwxtx.html