0

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
4

1 回答 1

3

不确定不检查完整代码,但看起来 pipelineFactor.getPipeline() 将始终在您的情况下创建一个管道。由于它是一个工厂,它每次都会创建处理程序。再添加一个sysoutthis.pipelineFactory.getPipeline()如果您看到 3 个不同的对象哈希码,那么这就是根本原因。
解决方案可能是pipeline = this.pipelineFactory.getPipeline(),然后使用pipeline添加删除等。
另外,为了记录,无论如何它似乎是错误的用法,您应该从ChannelHandlerContext对象的decode方法或messageReceived处理程序的方法中获取管道。

于 2013-02-01T08:08:30.260 回答