camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Guava EventBus
Date Fri, 05 Apr 2013 19:30:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/Guava+EventBus">Guava
EventBus</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~hekonsek">Henryk
Konsek</a>
    </h4>
        <div id="versionComment">
        <b>Comment:</b>
        [Guava EventBus] Added section on consuming multiple type of events<br />
    </div>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >from(&quot;guava-eventbus:busName?listenerInterface=com.example.CustomListener&quot;).to(&quot;seda:queue&quot;);
<br>{code} <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">
<br>h3. Consuming multiple type of events <br> <br>In order to define multiple
type of events to be consumed by Guava EventBus consumer use {{listenerInterface}} endpoint
option, as listener interface could provide multiple methods marked with the {{@Subscribe}}
annotation. <br> <br>{code} <br>package com.example; <br> <br>public
interface MultipleEventsListener { <br> <br>  @Subscribe <br>  void someEventReceived(SomeEvent
event); <br> <br>  @Subscribe <br>  void anotherEventReceived(AnotherEvent
event); <br> <br>} <br>{code} <br> <br>The listener presented
above could be used in the endpoint definition as follows. <br> <br>{code} <br>from(&quot;guava-eventbus:busName?listenerInterface=com.example.MultipleEventsListener&quot;).to(&quot;seda:queue&quot;);
<br>{code} <br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="GuavaEventBus-GuavaEventBusComponent"></a>Guava EventBus
Component</h2>

<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><b>Available since Camel
2.10.0</b><br /></td></tr></table></div>

<p>The <a href="http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/eventbus/package-summary.html"
class="external-link" rel="nofollow">Google Guava EventBus</a> 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 <b>guava-eventbus:</b> component
provides integration bridge between Camel and <a href="http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/eventbus/package-summary.html"
class="external-link" rel="nofollow">Google Guava EventBus</a> infrastructure. With
the latter component, messages exchanged with the Guava <tt>EventBus</tt> can
be transparently forwarded to the Camel routes. EventBus component allows also to route body
of Camel exchanges to the Guava <tt>EventBus</tt>.</p>

<p>Maven users will need to add the following dependency to their <tt>pom.xml</tt>
for this component:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;dependency&gt;</span>
    <span class="code-tag">&lt;groupId&gt;</span>org.apache.camel<span
class="code-tag">&lt;/groupId&gt;</span>
    <span class="code-tag">&lt;artifactId&gt;</span>camel-guava-eventbus<span
class="code-tag">&lt;/artifactId&gt;</span>
    <span class="code-tag">&lt;version&gt;</span>x.x.x<span class="code-tag">&lt;/version&gt;</span>
    <span class="code-tag"><span class="code-comment">&lt;!-- use the same
version as your Camel core version --&gt;</span></span>
<span class="code-tag">&lt;/dependency&gt;</span>
</pre>
</div></div>

<h3><a name="GuavaEventBus-URIformat"></a>URI format</h3>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
guava-eventbus:busName[?options]
</pre>
</div></div>
<p>Where <b>busName</b> represents the name of the <tt>com.google.common.eventbus.EventBus</tt>
instance located in the Camel registry.</p>

<h3><a name="GuavaEventBus-Options"></a>Options</h3>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Default Value </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>eventClass</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 2.10:</b> If used on the consumer side
of the route, will filter events received from the <tt>EventBus</tt> to the instances
of the class and superclasses of <tt>eventClass</tt>. Null value of this option
is equal to setting it to the <tt>java.lang.Object</tt> i.e. the consumer will
capture all messages incoming to the event bus. This option cannot be used together with <tt>listenerInterface</tt>
option. </td>
</tr>
<tr>
<td class='confluenceTd'><tt>listenerInterface</tt></td>
<td class='confluenceTd'><tt>null</tt></td>
<td class='confluenceTd'> <b>Camel 2.11:</b> The interface with method(s)
marked with the <tt>@Subscribe</tt> annotation. Dynamic proxy will be created
over the interface so it could be registered as the <tt>EventBus</tt> listener.
Particularly useful when creating multi-event listeners and for handling <tt>DeadEvent</tt>
properly. This option cannot be used together with <tt>eventClass</tt> option.</td>
</tr>
</tbody></table>
</div>


<h3><a name="GuavaEventBus-Usage"></a>Usage</h3>

<p>Using <tt>guava-eventbus</tt> component on the consumer side of the route
will capture messages sent to the Guava <tt>EventBus</tt> and forward them to
the Camel route. Guava EventBus consumer processes incoming messages <a href="http://camel.apache.org/asynchronous-routing-engine.html"
class="external-link" rel="nofollow">asynchronously</a>. </p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
SimpleRegistry registry = <span class="code-keyword">new</span> SimpleRegistry();
EventBus eventBus = <span class="code-keyword">new</span> EventBus();
registry.put(<span class="code-quote">"busName"</span>, eventBus);
CamelContext camel = <span class="code-keyword">new</span> DefaultCamelContext(registry);

from(<span class="code-quote">"guava-eventbus:busName"</span>).to(<span class="code-quote">"seda:queue"</span>);

eventBus.post(<span class="code-quote">"Send me to the SEDA queue."</span>);
</pre>
</div></div>

<p>Using <tt>guava-eventbus</tt> component on the producer side of the route
will forward body of the Camel exchanges to the Guava <tt>EventBus</tt> instance.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
SimpleRegistry registry = <span class="code-keyword">new</span> SimpleRegistry();
EventBus eventBus = <span class="code-keyword">new</span> EventBus();
registry.put(<span class="code-quote">"busName"</span>, eventBus);
CamelContext camel = <span class="code-keyword">new</span> DefaultCamelContext(registry);

from(<span class="code-quote">"direct:start"</span>).to(<span class="code-quote">"guava-eventbus:busName"</span>);

ProducerTemplate producerTemplate = camel.createProducerTemplate();
producer.sendBody(<span class="code-quote">"direct:start"</span>, <span class="code-quote">"Send
me to the Guava EventBus."</span>);

eventBus.register(<span class="code-keyword">new</span> <span class="code-object">Object</span>(){
  @Subscribe
  <span class="code-keyword">public</span> void messageHander(<span class="code-object">String</span>
message) {
    <span class="code-object">System</span>.out.println(<span class="code-quote">"Message
received from the Camel: "</span> + message);
  }
});
</pre>
</div></div>

<h3><a name="GuavaEventBus-DeadEventconsiderations"></a>DeadEvent considerations</h3>

<p>Keep in mind that due to the limitations caused by the design of the Guava EventBus,
you cannot specify event class to be received by the listener without creating class annotated
with <tt>@Subscribe</tt> method. This limitation implies that endpoint with <tt>eventClass</tt>
option specified actually listens to all possible events (<tt>java.lang.Object</tt>)
and filter appropriate messages programmatically at runtime. The snipped below demonstrates
an appropriate excerpt from the Camel code base.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
@Subscribe
<span class="code-keyword">public</span> void eventReceived(<span class="code-object">Object</span>
event) {
  <span class="code-keyword">if</span> (eventClass == <span class="code-keyword">null</span>
|| eventClass.isAssignableFrom(event.getClass())) {
    doEventReceived(event);
...
</pre>
</div></div>

<p>This drawback of this approach is that <tt>EventBus</tt> instance used
by Camel will never generate <tt>com.google.common.eventbus.DeadEvent</tt> notifications.
If you want Camel to listen only to the precisely specified event (and therefore enable <tt>DeadEvent</tt>
support), use <tt>listenerInterface</tt> endpoint option. Camel will create dynamic
proxy over the interface you specify with the latter option and listen only to messages specified
by the interface handler methods. The example of the listener interface with single method
handling only <tt>SpecificEvent</tt> instances is demonstrated below.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">package</span> com.example;

<span class="code-keyword">public</span> <span class="code-keyword">interface</span>
CustomListener {

  @Subscribe
  void eventReceived(SpecificEvent event);

}
</pre>
</div></div>

<p>The listener presented above could be used in the endpoint definition as follows.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"guava-eventbus:busName?listenerInterface=com.example.CustomListener"</span>).to(<span
class="code-quote">"seda:queue"</span>);
</pre>
</div></div>

<h3><a name="GuavaEventBus-Consumingmultipletypeofevents"></a>Consuming
multiple type of events</h3>

<p>In order to define multiple type of events to be consumed by Guava EventBus consumer
use <tt>listenerInterface</tt> endpoint option, as listener interface could provide
multiple methods marked with the <tt>@Subscribe</tt> annotation.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">package</span> com.example;

<span class="code-keyword">public</span> <span class="code-keyword">interface</span>
MultipleEventsListener {

  @Subscribe
  void someEventReceived(SomeEvent event);

  @Subscribe
  void anotherEventReceived(AnotherEvent event);

}
</pre>
</div></div>

<p>The listener presented above could be used in the endpoint definition as follows.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"guava-eventbus:busName?listenerInterface=com.example.MultipleEventsListener"</span>).to(<span
class="code-quote">"seda:queue"</span>);
</pre>
</div></div>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Guava+EventBus">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=27849882&revisedVersion=7&originalVersion=6">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Guava+EventBus?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message