検索

242.8. カスタムパイプライン

download PDF

カスタムチャネルパイプラインは、Netty エンドポイント URL で非常に簡単な方法で指定することなく、カスタムハンドラー、エンコーダー、デコーダーを挿入することで、ハンドラー/インターセプターチェーンを完全に制御します。

カスタムパイプラインを追加するには、カスタムチャネルパイプラインファクトリーを作成し、コンテキストレジストリー (Registry、または camel-spring ApplicationContextRegistry など) を介してコンテキストに登録する必要があります。

カスタムパイプラインファクトリーは、次のように構築する必要があります。

  • producer にリンクされたチャネルパイプラインファクトリーは、抽象クラス ClientPipelineFactory を拡張する必要があります。
  • consumer リンクチャネルパイプラインファクトリーは、抽象クラス ServerInitializerFactory を拡張する必要があります。
  • カスタムハンドラー、エンコーダ、およびデコーダを挿入するには、クラスで initChannel () メソッドをオーバーライドする必要があります。initChannel() メソッドをオーバーライドしないと、パイプラインに接続されたハンドラー、エンコーダー、またはデコーダーのないパイプラインが作成されます。

以下の例は、ServerInitializerFactory ファクトリーを作成する方法を示しています。

242.8.1. カスタムパイプラインファクトリーの使用

import io.netty.channel.Channel;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;
import org.apache.camel.component.netty4.NettyConsumer;
import org.apache.camel.component.netty4.ServerInitializerFactory;
import org.apache.camel.component.netty4.handlers.ServerChannelHandler;

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

    public SampleServerInitializerFactory(NettyConsumer consumer) {
        this.consumer = consumer;
    }

    @Override
    public ServerInitializerFactory createPipelineFactory(NettyConsumer consumer) {
        return new SampleServerInitializerFactory(consumer);
    }

    @Override
    protected void initChannel(Channel channel) throws Exception {
        ChannelPipeline channelPipeline = channel.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 registry = camelContext.getRegistry();
 ServerInitializerFactory factory = new TestServerInitializerFactory(nettyConsumer);
 registry.bind("spf", factory);
context.addRoutes(new RouteBuilder() {
  public void configure() {
      String netty_ssl_endpoint =
         "netty4: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

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

© 2024 Red Hat, Inc.