搜索

46.9. 自定义管道

download PDF

自定义频道管道通过插入自定义处理程序、编码器和解码器,而无需以非常简单的方式在 Netty Endpoint URL 中为用户提供完全控制。

要添加自定义管道,必须通过上下文 registry (Registry 或 camel-spring ApplicationContextRegistry 等)创建自定义频道管道工厂。

自定义管道工厂必须构建如下

  • 链接的频道管道工厂必须扩展抽象类 ClientPipelineFactory
  • Consumer linked 频道管道工厂必须扩展抽象类 ServerInitializerFactory
  • 类应该覆盖 initChannel ()方法,以便插入自定义处理程序、编码器和解码器。不覆盖 initChannel () 方法会创建一个没有处理程序、编码器或解码器到管道的管道。

以下示例演示了如何创建 ServerInitializerFactory 工厂

46.9.1. 使用自定义管道工厂

public class SampleServerInitializerFactory extends ServerInitializerFactory {
    private int maxLineSize = 1024;

    protected void initChannel(Channel ch) throws Exception {
        ChannelPipeline channelPipeline = ch.pipeline();

        channelPipeline.addLast("encoder-SD", new StringEncoder(CharsetUtil.UTF_8));
        channelPipeline.addLast("decoder-DELIM", new DelimiterBasedFrameDecoder(maxLineSize, true, Delimiters.lineDelimiter()));
        channelPipeline.addLast("decoder-SD", new StringDecoder(CharsetUtil.UTF_8));
        // here we add the default Camel ServerChannelHandler for the consumer, to allow Camel to route the message etc.
        channelPipeline.addLast("handler", new ServerChannelHandler(consumer));
    }
}

然后可将自定义频道管道工厂添加到 registry 中,并以以下方式在 camel 路由上实例化/使用

Registry registry = camelContext.getRegistry();
ServerInitializerFactory factory = new TestServerInitializerFactory();
registry.bind("spf", factory);
context.addRoutes(new RouteBuilder() {
  public void configure() {
      String netty_ssl_endpoint =
         "netty:tcp://0.0.0.0:5150?serverInitializerFactory=#spf"
      String return_string =
         "When You Go Home, Tell Them Of Us And Say,"
         + "For Your Tomorrow, We Gave Our Today.";

      from(netty_ssl_endpoint)
       .process(new Processor() {
          public void process(Exchange exchange) throws Exception {
            exchange.getOut().setBody(return_string);
          }
       }
  }
});
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.