camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > RX
Date Mon, 04 Mar 2013 07:09: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/RX">RX</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~jstrachan">James
Strachan</a>
    </h4>
        <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" >filtered = observable.filter(m -&gt;
m.getHeader(&quot;foo&quot;) != null).map(m -&gt; &quot;Hello &quot; +
m.getBody());  <br>{code} <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">
<br>If you know the type of the body of the message, you can use an overloaded version
of toObservable() to pass in the class and get a typesafe Observable&lt;T&gt; back:
<br>{code:java} <br>import org.apache.camel.rx.*; <br> <br>ReactiveCamel
rx = new ReactiveCamel(camelContext); <br>Observable&lt;Order&gt; observable
= rx.toObservable(&quot;seda:orders&quot;, Order.class); <br> <br>// now
lets filter and map using Java 7 <br>Observable&lt;String&gt; largeOrderIds
= observable.filter(new Func1&lt;Order, Boolean&gt;() { <br>    public Boolean
call(Order order) { <br>        return order.getAmount() &gt; 100.0; <br>
   } <br>}).map(new Func1&lt;Order, String&gt;() { <br>    public String
call(Order order) { <br>        return order.getId(); <br>    } <br>});
<br>{code} <br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h1><a name="RX-ReactiveCamel"></a>Reactive Camel</h1>

<p>The camel-rx library provides Camel support for the <a href="https://rx.codeplex.com/"
class="external-link" rel="nofollow">Reactive Extensions</a>&nbsp;(RX) using
the <a href="https://github.com/Netflix/RxJava/wiki" class="external-link" rel="nofollow">RxJava</a>
library.</p>

<p>You can think of RX as providing an API similar to Java 8 / Groovy / Scala like API
collections (methods like filter, forEach, map, flatMap etc) - but which operates on a stream
of events rather than a collection. So you could think of RX as like working with asynchronous
push based collections (rather than the traditional synchronous pull based collections).</p>

<p>In RX, if you have an <a href="http://netflix.github.com/RxJava/javadoc/rx/Observable.html"
class="external-link" rel="nofollow">Observable&lt;T&gt;</a> which behaves
quite like a Collection&lt;T&gt; in Java 8 so you can filter/map/concat and so forth.
The Observable&lt;T&gt; then acts as a typesafe composable API for working with asynchronous
events.</p>

<p>You can then create an Observable&lt;Message&gt; from any endpoint using
the ReactiveCamel helper class.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">import</span> org.apache.camel.rx.*;

ReactiveCamel rx = <span class="code-keyword">new</span> ReactiveCamel(camelContext);
Observable&lt;Message&gt; observable = rx.toObservable(<span class="code-quote">"activemq:MyMessages"</span>);

<span class="code-comment">// we can now call filter/map/concat etc
</span>filtered = observable.filter(m -&gt; m.getHeader(<span class="code-quote">"foo"</span>)
!= <span class="code-keyword">null</span>).map(m -&gt; <span class="code-quote">"Hello
"</span> + m.getBody()); 
</pre>
</div></div>

<p>If you know the type of the body of the message, you can use an overloaded version
of toObservable() to pass in the class and get a typesafe Observable&lt;T&gt; back:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">import</span> org.apache.camel.rx.*;

ReactiveCamel rx = <span class="code-keyword">new</span> ReactiveCamel(camelContext);
Observable&lt;Order&gt; observable = rx.toObservable(<span class="code-quote">"seda:orders"</span>,
Order.class);

<span class="code-comment">// now lets filter and map using Java 7
</span>Observable&lt;<span class="code-object">String</span>&gt;
largeOrderIds = observable.filter(<span class="code-keyword">new</span> Func1&lt;Order,
<span class="code-object">Boolean</span>&gt;() {
    <span class="code-keyword">public</span> <span class="code-object">Boolean</span>
call(Order order) {
        <span class="code-keyword">return</span> order.getAmount() &gt; 100.0;
    }
}).map(<span class="code-keyword">new</span> Func1&lt;Order, <span class="code-object">String</span>&gt;()
{
    <span class="code-keyword">public</span> <span class="code-object">String</span>
call(Order order) {
        <span class="code-keyword">return</span> order.getId();
    }
});
</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/RX">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=30756029&revisedVersion=5&originalVersion=4">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/RX?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message