Chapter 35. Guava EventBus
Guava EventBus Component
Available since Camel 2.10.0
The Google Guava EventBus allows publish-subscribe-style communication between components without requiring the components to explicitly register with one another (and thus be aware of each other). The guava-eventbus: component provides integration bridge between Camel and Google Guava EventBus infrastructure. With the latter component, messages exchanged with the Guava
EventBus
can be transparently forwarded to the Camel routes. EventBus component allows also to route body of Camel exchanges to the Guava EventBus
.
Maven users will need to add the following dependency to their
pom.xml
for this component:
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-guava-eventbus</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency>
URI format
guava-eventbus:busName[?options]
Where busName represents the name of the
com.google.common.eventbus.EventBus
instance located in the Camel registry.
Options
Name | Default Value | Description |
---|---|---|
eventClass
|
null
|
*Camel 2.10:* If used on the consumer side of the route, will filter events received from the EventBus to the instances of the class and superclasses of eventClass . Null value of this option is equal to setting it to the java.lang.Object i.e. the consumer will capture all messages incoming to the event bus.
|
Usage
Using
guava-eventbus
component on the consumer side of the route will capture messages sent to the Guava EventBus
and forward them to the Camel route. Guava EventBus consumer processes incoming messages asynchronously.
SimpleRegistry registry = new SimpleRegistry(); EventBus eventBus = new EventBus(); registry.put("busName", eventBus); CamelContext camel = new DefaultCamelContext(registry); from("guava-eventbus:busName").to("seda:queue"); eventBus.post("Send me to the SEDA queue.");
Using
guava-eventbus
component on the producer side of the route will forward body of the Camel exchanges to the Guava EventBus
instance.
SimpleRegistry registry = new SimpleRegistry(); EventBus eventBus = new EventBus(); registry.put("busName", eventBus); CamelContext camel = new DefaultCamelContext(registry); from("direct:start").to("guava-eventbus:busName"); ProducerTemplate producerTemplate = camel.createProducerTemplate(); producer.sendBody("direct:start", "Send me to the Guava EventBus."); eventBus.register(new Object(){ @Subscribe public void messageHander(String message) { System.out.println("Message received from the Camel: " + message); } });