简体   繁体   English

如何跳过某些处理程序并直接转到netty中的特定处理程序

[英]How to skip certain handlers and directly go to specific handler in netty

Let's say I have these handler flow in netty pipeline: 假设我在netty管道中有以下处理程序流:

UpHandler1 -> UpHandler2 -> UpHandler3 -> ... -> DownHandler1 -> DownHandler2 -> DownHandler3 UpHandler1-> UpHandler2-> UpHandler3-> ...-> DownHandler1-> DownHandler2-> DownHandler3

Based on certain condition (ie already found response to request without doing further processing), is there anyway, in my UpHandler2, I can straight go to DownHandler2 (so skip certain upstream as well as downstream handlers in between)? 基于某些条件(即,在没有进行进一步处理的情况下已经找到对请求的响应),无论如何,在我的UpHandler2中,我可以直接进入DownHandler2(因此跳过两者之间的某些上游和下游处理程序)吗? Is this recommended? 推荐这个吗?

You can use UpHandler2's ChannelHandlerContext to retrieve the ChannelPipeline. 您可以使用UpHandler2的ChannelHandlerContext检索ChannelPipeline。 From here you can retrieve the channel handler context of any channel handler using one of the context(...) methods. 在这里,您可以使用context(...)方法之一检索任何通道处理程序的通道处理程序上下文。 Then sendDownstream for Netty 3, or write for Netty 4, will forward to the next downstream handler after the handler to which the context responds. 然后,Netty 3的sendDownstream或Netty 4的writeDownstream将转发到上下文响应的处理程序之后的下一个下游处理程序。 In effect I think you'll need to get the ChannelHandlerContext for DownHandler1 and use that to write your message. 实际上,我认为您需要获取DownHandler1的ChannelHandlerContext并使用它来编写消息。

Alternatively you can build the netty pipeline such that DownHandler2 is the next down stream handler from UpHandler2. 或者,您可以构建网络管道,以使DownHandler2是UpHandler2的下一个下游处理程序。 If I've understood your pipeline correctly then something like 如果我正确理解了您的管道,那么类似

pipeline.addLast("down3", downhandler3);
pipeline.addLast('up1", uphandler1);
pipeline.addLast("down2", downhandler2);
pipeline.addLast("up2", uphandler2);
pipeline.addLast("down1", downhandler1);
pipeline.addLast("up3", uphandler3);

might work. 可能有用。 However this could be quite brittle and also depends on whether your processing logic allows it. 但是,这可能非常脆弱,并且还取决于您的处理逻辑是否允许它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM