MLightheart
MLightheart

Reputation: 11

Deleting handler from pipeline

I am trying to delete a certain handler from my handler pipeline, but I am having trouble doing it. When I list the handlers in the pipeline before and after, the handler that I tried to remove is still there. So what am I doing wrong here? Here is a code snippet. All of this is in the startup phase. You can see that last thing that I do is configure the pipeline factory. I am using Netty 3.6.1.final.

    List<String> handlers = new ArrayList<String>();

    // list handlers in the pipeline
    try {
        handlers = this.pipelineFactory.getPipeline().getNames();
        for (int len = handlers.size(), i = 0; i < len; i++) {
          String s = handlers.get(i);
          System.out.println("Item " + i + " is " + s);
        }

    } catch( Exception e ) {}

    try {
        System.out.println("Remove hexdump");
        this.pipelineFactory.getPipeline().remove("hexdump");
    } catch( Exception e ) {
       System.out.println("error = " + e.getMessage());
    }


    try {
        handlers = this.pipelineFactory.getPipeline().getNames();
        for (int len = handlers.size(), i = 0; i < len; i++) {
          String s = handlers.get(i);
          System.out.println("Item " + i + " is " + s);
        }

    } catch( Exception e ) {}

    // Configure the pipeline factory.
    this.bootstrap.setPipelineFactory(this.pipelineFactory);

Here is the output:

Item 0 is framer
Item 1 is hexdump
Item 2 is handler
Remove hexdump
Item 0 is framer
Item 1 is hexdump    
Item 2 is handler

Upvotes: 0

Views: 1644

Answers (1)

Abe
Abe

Reputation: 9061

Not sure without checking out full code, but looks like pipelineFactor.getPipeline() will always create a new pipeline in your case. Since its a factory, it will be creating the handlers each time. Put in one more sysout for this.pipelineFactory.getPipeline() and if you are seeing 3 different object hashcodes then this is the root cause.
Solution could be pipeline = this.pipelineFactory.getPipeline(), and then using the pipeline for adding removing etc.
Also for the record, it seems wrong usage anyway, you should be getting the pipeline from the ChannelHandlerContext object either in a decode method or a messageReceived method of a handler.

Upvotes: 3

Related Questions