Since we're on a major migration process of this website, some component documents here are out of sync right now. In the meantime you may want to look at the early version of the new website
https://camel.apache.org/staging/
We would very much like to receive any feedback on the new site, please join the discussion on the Camel user mailing list.
JGroups ComponentAvailable since Camel 2.10.0 JGroups is a toolkit for reliable multicast communication. The jgroups: component provides exchange of messages between Camel infrastructure and JGroups clusters. Maven users will need to add the following dependency to their <dependency> <groupId>org.apache-extras.camel-extra</groupId> <artifactId>camel-jgroups</artifactId> <!-- use the same version as your Camel core version --> <version>x.y.z</version> </dependency> Starting from the Camel 2.13.0, JGroups component has been moved from Camel Extra under the umbrella of the Apache Camel. If you are using Camel 2.13.0 or higher, please use the following POM entry instead. <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-jgroups</artifactId> <!-- use the same version as your Camel core version --> <version>x.y.z</version> </dependency> URI formatjgroups:clusterName[?options] Where clusterName represents the name of the JGroups cluster the component should connect to. Options
Headers
UsageUsing // Capture messages from cluster named // 'clusterName' and send them to Camel route. from("jgroups:clusterName").to("seda:queue"); Using // Send message to the cluster named 'clusterName' from("direct:start").to("jgroups:clusterName"); Predefined filtersStarting from version 2.13.0 of Camel, JGroups component comes with predefined filters factory class named If you would like to consume only view changes notifications sent to coordinator of the cluster (and ignore these sent to the "slave" nodes), use the import static org.apache.camel.component.jgroups.JGroupsFilters.dropNonCoordinatorViews; ... from("jgroups:clusterName?enableViewMessages=true"). filter(dropNonCoordinatorViews()). to("seda:masterNodeEventsQueue"); Predefined expressionsStarting from version 2.13.0 of Camel, JGroups component comes with predefined expressions factory class named If you would like to create delayer that would affect the route only if the Camel context has not been started yet, use the The snippet below demonstrates how to use conditional delaying with the JGroups component to delay the initial startup of master node in the cluster. 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); ExamplesSending (receiving) messages to (from) the JGroups clusterIn order to send message to the JGroups cluster use producer endpoint, just as demonstrated on the snippet below. 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. mockEndpoint.setExpectedMessageCount(1); mockEndpoint.message(0).body().isEqualTo("msg"); ... from("jgroups:myCluster").to("mock:messagesFromTheCluster"); ... mockEndpoint.assertIsSatisfied(); Receive cluster view change notificationsThe snippet below demonstrates how to create the consumer endpoint listening to the notifications regarding cluster membership changes. By default only regular messages are consumed by the endpoint. 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 clusterThe 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 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"); |