265.4. ルートボックスとのメッセージの送受信
リクエストを送信する前に、以下に示すように必要な URI パラメーターをレジストリーにロードして、ルートボックスを適切に設定する必要があります。Spring の場合、必要な Bean が正しく宣言されていれば、レジストリーは Camel によって自動的に入力されます。
265.4.1. ステップ 1: 内部ルートの詳細をレジストリーにロードする
@Override
protected JndiRegistry createRegistry() throws Exception {
JndiRegistry registry = new JndiRegistry(createJndiContext());
// Wire the routeDefinitions & dispatchStrategy to the outer camelContext where the routebox is declared
List<RouteBuilder> routes = new ArrayList<RouteBuilder>();
routes.add(new SimpleRouteBuilder());
registry.bind("registry", createInnerRegistry());
registry.bind("routes", routes);
// Wire a dispatch map to registry
HashMap<String, String> map = new HashMap<String, String>();
map.put("addToCatalog", "seda:addToCatalog");
map.put("findBook", "seda:findBook");
registry.bind("map", map);
// Alternatively wiring a dispatch strategy to the registry
registry.bind("strategy", new SimpleRouteDispatchStrategy());
return registry;
}
private JndiRegistry createInnerRegistry() throws Exception {
JndiRegistry innerRegistry = new JndiRegistry(createJndiContext());
BookCatalog catalogBean = new BookCatalog();
innerRegistry.bind("library", catalogBean);
return innerRegistry;
}
...
CamelContext context = new DefaultCamelContext(createRegistry());
Copy to clipboardCopied265.4.2. ステップ 2: オプションで Dispatch Map の代わりに Dispatch Strategy を使用する
ディスパッチストラテジーを使用するには、以下の例に示すように、インターフェイス org.apache.camel.component.routebox.strategy.RouteboxDispatchStrategy を実装する必要があります。
public class SimpleRouteDispatchStrategy implements RouteboxDispatchStrategy {
/* (non-Javadoc)
* @see org.apache.camel.component.routebox.strategy.RouteboxDispatchStrategy#selectDestinationUri(java.util.List, org.apache.camel.Exchange)
*/
public URI selectDestinationUri(List<URI> activeDestinations,
Exchange exchange) {
URI dispatchDestination = null;
String operation = exchange.getIn().getHeader("ROUTE_DISPATCH_KEY", String.class);
for (URI destination : activeDestinations) {
if (destination.toASCIIString().equalsIgnoreCase("seda:" + operation)) {
dispatchDestination = destination;
break;
}
}
return dispatchDestination;
}
}
Copy to clipboardCopied265.4.3. ステップ 2: routebox コンシューマーの起動
ルートコンシューマーを作成するときは、routeboxUri の # エントリーが、作成された内部レジストリー、routebuilder リスト、および CamelContext レジストリーの dispatchStrategy/dispatchMap と一致することに注意してください。すべてのルートビルダーと関連するルートは、ルートボックスで作成された内部コンテキストで起動されることに注意してください
private String routeboxUri = "routebox:multipleRoutes?innerRegistry=#registry&routeBuilders=#routes&dispatchMap=#map";
public void testRouteboxRequests() throws Exception {
CamelContext context = createCamelContext();
template = new DefaultProducerTemplate(context);
template.start();
context.addRoutes(new RouteBuilder() {
public void configure() {
from(routeboxUri)
.to("log:Routes operation performed?showAll=true");
}
});
context.start();
// Now use the ProducerTemplate to send the request to the routebox
template.requestBodyAndHeader(routeboxUri, book, "ROUTE_DISPATCH_KEY", "addToCatalog");
}
Copy to clipboardCopied265.4.4. ステップ 3: routebox プロデューサーを使用する
リクエストをルートボックスに送信する場合、プロデューサーは内部ルートエンドポイント URI を知る必要はなく、以下に示すように、ディスパッチストラテジーまたは dispatchMap を使用してルートボックス URI エンドポイントを呼び出すだけで済みます。
リクエストが正しい内部ルートに送信されるように、ROUTE_DISPATCH_KEY (Dispatch Strategy のオプション) と呼ばれる特別なエクチェンジヘッダーをディスパッチマップのキーと一致するキーに設定する必要があります。
from("direct:sendToStrategyBasedRoutebox")
.to("routebox:multipleRoutes?innerRegistry=#registry&routeBuilders=#routes&dispatchStrategy=#strategy")
.to("log:Routes operation performed?showAll=true");
from ("direct:sendToMapBasedRoutebox")
.setHeader("ROUTE_DISPATCH_KEY", constant("addToCatalog"))
.to("routebox:multipleRoutes?innerRegistry=#registry&routeBuilders=#routes&dispatchMap=#map")
.to("log:Routes operation performed?showAll=true");
Copy to clipboardCopied