...
Code Block |
---|
|
// Send message to the cluster named 'clusterName'
from("direct:start").to("jgroups:clusterName");
|
Predefined filters
Starting from version 2.13.0 of Camel, JGroups component comes with predefined filters factory class named JGroupsFilters.
...
Code Block |
---|
|
import static org.apache.camel.component.jgroups.JGroupsFilters.dropNonCoordinatorViews;
...
from("jgroups:clusterName?enableViewMessages=true").
filter(dropNonCoordinatorViews()).
to("seda:masterNodeEventsQueue");
|
Predefined expressions
Starting from version 2.13.0 of Camel, JGroups component comes with predefined expressions factory class named JGroupsExpressions.
...
Code Block |
---|
|
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("timer://master?repeatCount=1").routeId("masterRoute").autoStartup(false).to(masterMockUri); |
Examples
Sending (receiving) messages to (from) the JGroups cluster
In order to send message to the JGroups cluster use producer endpoint, just as demonstrated on the snippet below.
Code Block |
---|
|
from("direct:start").to("jgroups:myCluster");
...
producerTemplate.sendBody("direct:start", "msg") |
To receive the message from the snippet above (on the same or the other physical machine) listen on the messages coming from the given cluster, just as demonstrated on the code fragment below.
Code Block |
---|
|
mockEndpoint.setExpectedMessageCount(1);
mockEndpoint.message(0).body().isEqualTo("msg");
...
from("jgroups:myCluster").to("mock:messagesFromTheCluster");
...
mockEndpoint.assertIsSatisfied(); |
Receive cluster view change notifications
...
Code Block |
---|
|
mockEndpoint.setExpectedMessageCount(1);
mockEndpoint.message(0).body().isInstanceOf(org.jgroups.View.class);
...
from("jgroups:clusterName?enableViewMessages=true").to(mockEndpoint);
...
mockEndpoint.assertIsSatisfied(); |
...