...
Header | Constant | Since version | Description |
---|
JGROUPS_ORIGINAL_MESSAGE | JGroupsEndpoint.HEADER_JGROUPS_ORIGINAL_MESSAGE | 2.13.0 | The original org.jgroups.Message instance from which the body of the consumed message has been extracted. |
JGROUPS_SRC | JGroupsEndpoint. HEADER_JGROUPS_SRC
| 2.10.0 | Consumer: The org.jgroups.Address instance extracted by org.jgroups.Message .getSrc() method of the consumed message. Producer: The custom source org.jgroups.Address of the message to be sent. |
JGROUPS_DEST | JGroupsEndpoint. HEADER_JGROUPS_DEST
| 2.10.0 | Consumer: The org.jgroups.Address instance extracted by org.jgroups.Message .getDest() method of the consumed message. Producer: The custom destination org.jgroups.Address of the message to be sent. |
JGROUPS_CHANNEL_ADDRESS | JGroupsEndpoint. HEADER_JGROUPS_CHANNEL_ADDRESS
| 2.13.0 | Address (org.jgroups.Address ) of the channel associated with the endpoint. |
...
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(); |
Keeping singleton route within the cluster
The snippet below demonstrates how to keep the singleton consumer route in the cluster of Camel Contexts. As soon as the master node dies, one of the slaves will be elected as a new master and started. In this particular example we want to keep singleton jetty instance listening for the requests on address http://localhost:8080/orders
.
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("jetty:http://localhost:8080/orders").routeId("masterRoute").autoStartup(false).to("jms:orders"); |