gpt4 book ai didi

java - Netty中的解码器、编码器、ServerHandler管道

转载 作者:行者123 更新时间:2023-12-01 20:24:41 26 4
gpt4 key购买 nike

查看文档,它是这样说的:

https://netty.io/4.0/api/io/netty/channel/ChannelPipeline.html

A user is supposed to have one or more ChannelHandlers in a pipelineto receive I/O events (e.g. read) and to request I/O operations (e.g.write and close). For example, a typical server will have thefollowing handlers in each channel's pipeline, but your mileage mayvary depending on the complexity and characteristics of the protocoland business logic:

Protocol Decoder - translates binary data (e.g. ByteBuf) into a Javaobject.Protocol Encoder - translates a Java object into binary data.

Business Logic Handler - performs the actual business logic (e.g.database access). and it could be represented as shown in thefollowing example: static final EventExecutorGroup group = newDefaultEventExecutorGroup(16); ...

ChannelPipeline pipeline = ch.pipeline();

pipeline.addLast("decoder", new MyProtocolDecoder());

pipeline.addLast("encoder", new MyProtocolEncoder());

// Tell the pipeline to run MyBusinessLogicHandler's event handlermethods // in a different thread than an I/O thread so that the I/Othread is not blocked by // a time-consuming task. // If yourbusiness logic is fully asynchronous or finished very quickly, youdon't // need to specify a group.

pipeline.addLast(group, "handler",new MyBusinessLogicHandler());

在 Github 上的很多示例中我都看到了同样的模式。我想知道是否有人可以解释为什么businessHandler不在解码器和编码器之间。我认为您会获取 POJO,然后在业务处理程序中对其进行处理,然后对其进行编码。

最佳答案

由于处理程序的调用顺序,解码器和编码器通常位于管道的开头。对于传入数据,它是自下而上的;对于传出数据,它是自上而下的。

例如

pipeline.addLast(new MyEncoder());
pipeline.addLast(new MyDecoder());
pipeline.addLast(new MyBusiness());

在这种情况下,对于传入数据调用顺序是:MyDecoder(将数据转换为 POJO)-> MyBusiness(传入流不调用编码器),对于传出数据:MyBusiness -> MyEncoder(不调用解码器)输出流)。

如果您在业务处理程序(实际上是解码器之后的 POJO)中收到传入流,对其进行处理并将其写回,则看起来 MyBusiness 位于编码器和解码器之间,因为数据正在返回到编码器。

关于java - Netty中的解码器、编码器、ServerHandler管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44061715/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com