【发布时间】:2011-10-18 07:43:09
【问题描述】:
在阅读 Netty 教程时,我发现了一个简单的 description 如何集成 Netty 和 Google Protocol Buffers。我已经开始研究它的示例(因为文档中没有更多信息)并编写了一个简单的应用程序,例如示例本地时间应用程序。但是这个例子是在 PipeFactory 类中使用静态初始化,例如:
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.handler.codec.protobuf.ProtobufDecoder;
import org.jboss.netty.handler.codec.protobuf.ProtobufEncoder;
import org.jboss.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import org.jboss.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
import static org.jboss.netty.channel.Channels.pipeline;
/**
* @author sergiizagriichuk
*/
class ProtoCommunicationClientPipeFactory implements ChannelPipelineFactory {
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline p = pipeline();
p.addLast("frameDecoder", new ProtobufVarint32FrameDecoder());
p.addLast("protobufDecoder", new ProtobufDecoder(Communication.DataMessage.getDefaultInstance()));
p.addLast("frameEncoder", new ProtobufVarint32LengthFieldPrepender());
p.addLast("protobufEncoder", new ProtobufEncoder());
p.addLast("handler", new ProtoCommunicationClientHandler());
return p;
}
}
(请看p.addLast("protobufDecoder", new ProtobufDecoder(Communication.DataMessage.getDefaultInstance()));行)
并且只能为ClientBootstrap 类创建一个工厂(据我所知),我的意思是bootstrap.setPipelineFactory() 方法。因此,在这种情况下,我可以使用 ONE 消息发送到服务器并使用 ONE 消息从服务器接收,这对我不利,而且我认为不仅对我而言: (我怎样才能为一个连接使用不同的消息往返?
也许我可以像这样创建几个protobufDecoder
p.addLast("protobufDecoder", new ProtobufDecoder(Communication.DataMessage.getDefaultInstance()));
p.addLast("protobufDecoder", new ProtobufDecoder(Communication.TestMessage.getDefaultInstance()));
p.addLast("protobufDecoder", new ProtobufDecoder(Communication.SrcMessage.getDefaultInstance()));
或其他技术? 非常感谢。
【问题讨论】:
-
您可以在管道中添加许多解码器/编码器,但它们应该能够传递他们不知道如何处理的数据。看着netty source at github 好像不是这样的。所以可能有办法做到这一点,但我怀疑它是否简单。无论如何尝试并分享结果:)
-
@Slartibartfast 是的,这并不简单,需要努力工作:(
标签: java static protocol-buffers netty