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 clipboard

265.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 clipboard

265.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 clipboard

265.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 clipboard
トップに戻る
Red Hat logoGithubredditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

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

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

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

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

会社概要

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

Theme

© 2025 Red Hat, Inc.