第8章 メッセージのルーティング
概要
メッセージルーティングパターンでは、メッセージチャネルを互いにリンクするさまざまな方法について説明します。これには、メッセージボディーは変更せずに、メッセージストリームに適用できる、さまざまなアルゴリズムが含まれています。
8.1. Content-Based Router
概要
図8.1「Content-Based Router パターン」 に示されている Content-Based Router を使用すると、メッセージの内容に基づいて適切な宛先にメッセージをルーティングすることができます。
図8.1 Content-Based Router パターン
![Content-Based Router パターン](https://access.redhat.com/webassets/avalon/d/Red_Hat_Fuse-7.9-Apache_Camel_Development_Guide-ja-JP/images/fde359fba72016f65226a95d9a6e163c/content_based_router.gif)
Java DSL の例
以下の例は、入力エンドポイント seda:a
からのリクエストを、さまざまな述語式の評価に応じて seda:b
、queue:c
、または seda:d
のいずれかにルーティングする方法を示しています。
RouteBuilder builder = new RouteBuilder() { public void configure() { from("seda:a").choice() .when(header("foo").isEqualTo("bar")).to("seda:b") .when(header("foo").isEqualTo("cheese")).to("seda:c") .otherwise().to("seda:d"); } };
XML 設定の例
以下の例は、XML で同じルートを設定する方法を示しています。
<camelContext id="buildSimpleRouteWithChoice" xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="seda:a"/> <choice> <when> <xpath>$foo = 'bar'</xpath> <to uri="seda:b"/> </when> <when> <xpath>$foo = 'cheese'</xpath> <to uri="seda:c"/> </when> <otherwise> <to uri="seda:d"/> </otherwise> </choice> </route> </camelContext>