166.6. 例
166.6.1. JGroups クラスターへの (からの) メッセージの送信 (受信) リンクのコピーリンクがクリップボードにコピーされました!
JGroups クラスターにメッセージを送信するには、以下のスニペットで示されているように、プロデューサーエンドポイントを使用します。
from("direct:start").to("jgroups:myCluster");
...
producerTemplate.sendBody("direct:start", "msg")
上記のスニペットからメッセージを受信するには (同じ物理マシンまたは別の物理マシンで)、以下のコードフラグメントで示されているように、特定のクラスターからのメッセージをリッスンします。
mockEndpoint.setExpectedMessageCount(1);
mockEndpoint.message(0).body().isEqualTo("msg");
...
from("jgroups:myCluster").to("mock:messagesFromTheCluster");
...
mockEndpoint.assertIsSatisfied();
166.6.2. クラスタービューの変更通知を受け取る リンクのコピーリンクがクリップボードにコピーされました!
以下のスニペットは、クラスターメンバーシップの変更に関する通知をリッスンするコンシューマーエンドポイントを作成する方法を示しています。デフォルトでは、通常のメッセージのみがエンドポイントによって消費されます。
mockEndpoint.setExpectedMessageCount(1);
mockEndpoint.message(0).body().isInstanceOf(org.jgroups.View.class);
...
from("jgroups:clusterName?enableViewMessages=true").to(mockEndpoint);
...
mockEndpoint.assertIsSatisfied();
166.6.3. クラスター内にシングルトンルートを保持する リンクのコピーリンクがクリップボードにコピーされました!
以下のスニペットは、キャメルコンテキストのクラスターにシングルトンコンシューマールートを保持する方法を示しています。マスターノードが停止するとすぐに、スレーブの 1 つが新しいマスターとして選出され、開始されます。この特定の例では、アドレス ` http://localhost:8080/orders` でリクエストをリッスンするシングルトン jetty インスタンスを維持したいと考えています。
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.apache.camel.component.jgroups.JGroupsExpressions.delayIfContextNotStarted;
import static org.apache.camel.component.jgroups.JGroupsFilters.dropNonCoordinatorViews;
...
from("jgroups:clusterName?enableViewMessages=true").
filter(dropNonCoordinatorViews()).
threads().delay(delayIfContextNotStarted(SECONDS.toMillis(5))). // run in separated and delayed thread. Delay only if the context hasn't been started already.
to("controlbus:route?routeId=masterRoute&action=start&async=true");
from("jetty:http://localhost:8080/orders").routeId("masterRoute").autoStartup(false).to("jms:orders");