io.netty.channel

Interface ChannelHandlerContext

  • All Superinterfaces:
    io.netty.util.AttributeMap


    public interface ChannelHandlerContextextends io.netty.util.AttributeMap
    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.

    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.

    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.

    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.
     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.

    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:

     public class FactorialHandler extends ChannelInboundHandlerAdapter&ltInteger&gt {   private final AttributeKey&ltInteger&gt counter =           new AttributeKey&ltInteger&gt("counter");   // This handler will receive a sequence of increasing integers starting   // from 1.   @Override   public void channelRead(ChannelHandlerContext ctx, Integer integer) {     Attribute&ltInteger&gt attr = ctx.getAttr(counter);     Integer a = ctx.getAttr(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. 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); 

    Additional resources worth reading

    Please refer to the ChannelHandler, and ChannelPipeline to find out more about inbound and outbound operations, what fundamental differences they have, how they flow in a pipeline, and how to handle the operation in your application.

Copyright © 2008–2014 The Netty Project. All rights reserved.



NOTHING
NOTHING
Add the Maven Dependecy to your project: maven dependecy for com.amazonaws : aws-java-sdk : 1.3.14