36.9. カスタムパイプライン
カスタムチャネルパイプラインは、カスタムハンドラーを挿入してハンドラー/インターセプターチェーンを完全に制御します。これは、Netty エンドポイント URL でを簡単に指定しなくても、エンコーダー、エンコーダー、デコーダー(s)を持ちます。
カスタムパイプラインを追加するには、カスタムチャネルパイプラインファクトリーを作成し、コンテキストレジストリー(Registry または camel-spring ApplicationContextRegistry など)でコンテキストに登録する必要があります。
カスタムパイプラインファクトリーは、次のように構築する必要があります。
-
Producer リンクされたチャネルパイプラインファクトリーは、
ClientPipelineFactoryの抽象クラスを拡張する必要があります。 -
Consumer リンクされたチャネルパイプラインファクトリーは、
ServerInitializerFactoryの抽象クラスを拡張する必要があります。 -
クラスは、カスタムハンドラー、エンコーダー、およびデコーダーを挿入するために initChannel ()メソッドを上書きする必要があります。
initChannel ()メソッドをオーバーライドしない場合、ハンドラー、エンコーダー、またはデコーダーのないパイプラインがパイプラインに有線されます。
以下の例は、ServerInitializerFactory ファクトリーを作成する方法を示しています。
36.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));
}
}
次に、カスタムチャネルパイプラインファクトリーをレジストリーに追加し、以下のように 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);
}
}
}
});