camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Walk through an Example
Date Tue, 04 Dec 2012 21:51: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/Walk+through+an+Example">Walk
through an Example</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~raboof">Arnout
Engelen</a>
    </h4>
        <div id="versionComment">
        <b>Comment:</b>
        Fix link to activeMQComponent<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" >{snippet:id=e2|lang=java|url=camel/trunk/examples/camel-example-jms-file/src/main/java/org/apache/camel/example/jmstofile/CamelJmsToFileExample.java}
<br> <br></td></tr>
            <tr><td class="diff-changed-lines" >The above works with any JMS provider.
If we know we are using [ActiveMQ] we can use an even simpler form using the <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">[activeMQComponent()
method|http://camel.apache.org/maven/current/camel-activemq/apidocs/org/apache/camel/component/activemq/ActiveMQComponent.html#activeMQComponent(java.lang.String)]</span>
<span class="diff-added-words"style="background-color: #dfd;">[{{activeMQComponent()}}
method|http://activemq.apache.org/maven/5.5.0/activemq-camel/apidocs/org/apache/activemq/camel/component/ActiveMQComponent.html#activeMQComponent%28java.lang.String%29]</span>
while specifying the [brokerURL|http://activemq.apache.org/configuring-transports.html] used
to connect to ActiveMQ <br></td></tr>
            <tr><td class="diff-unchanged" > <br>{snippet:id=example|lang=java|url=activemq/trunk/activemq-camel/src/test/java/org/apache/activemq/camel/component/ActiveMQRouteTest.java}
<br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="WalkthroughanExample-WalkthroughanExampleCode"></a>Walk
through an Example Code</h2>

<p>This mini-guide takes you through the source code of a <a href="https://svn.apache.org/repos/asf/camel/trunk/examples/camel-example-jms-file/src/main/java/org/apache/camel/example/jmstofile/CamelJmsToFileExample.java"
class="external-link" rel="nofollow">simple example</a>.</p>

<p>Camel can be configured either by using <a href="/confluence/display/CAMEL/Spring"
title="Spring">Spring</a> or directly in Java - which <a href="https://svn.apache.org/repos/asf/camel/trunk/examples/camel-example-jms-file/src/main/java/org/apache/camel/example/jmstofile/CamelJmsToFileExample.java"
class="external-link" rel="nofollow">this example does</a>.</p>

<p>This example is available in the <tt>examples\camel-example-jms-file</tt>
directory of the <a href="/confluence/display/CAMEL/Download" title="Download">Camel
distribution</a>.</p>

<p>We start with creating a <a href="/confluence/display/CAMEL/CamelContext" title="CamelContext">CamelContext</a>
- which is a container for <a href="/confluence/display/CAMEL/Components" title="Components">Components</a>,
<a href="/confluence/display/CAMEL/Routes" title="Routes">Routes</a> etc:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">CamelContext context = <span class="code-keyword">new</span>
DefaultCamelContext();
</pre>
</div></div>

<p>There is more than one way of adding a Component to the CamelContext. You can add
components implicitly - when we set up the routing - as we do here for the <a href="/confluence/display/CAMEL/File2"
title="File2">FileComponent</a>:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">context.addRoutes(<span class="code-keyword">new</span>
RouteBuilder() {
    <span class="code-keyword">public</span> void configure() {
        from(<span class="code-quote">"test-jms:queue:test.queue"</span>).to(<span
class="code-quote">"file:<span class="code-comment">//test"</span>);
</span>    }
});
</pre>
</div></div>


<p>or explicitly - as we do here when we add the JMS Component:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">ConnectionFactory connectionFactory = <span class="code-keyword">new</span>
ActiveMQConnectionFactory(<span class="code-quote">"vm:<span class="code-comment">//localhost?broker.persistent=<span
class="code-keyword">false</span>"</span>);
</span><span class="code-comment">// Note we can explicit name the component
</span>context.addComponent(<span class="code-quote">"test-jms"</span>,
JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
</pre>
</div></div>

<p>The above works with any JMS provider. If we know we are using <a href="/confluence/display/CAMEL/ActiveMQ"
title="ActiveMQ">ActiveMQ</a> we can use an even simpler form using the <a href="http://activemq.apache.org/maven/5.5.0/activemq-camel/apidocs/org/apache/activemq/camel/component/ActiveMQComponent.html#activeMQComponent%28java.lang.String%29"
class="external-link" rel="nofollow"><tt>activeMQComponent()</tt> method</a>
while specifying the <a href="http://activemq.apache.org/configuring-transports.html" class="external-link"
rel="nofollow">brokerURL</a> used to connect to ActiveMQ </p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">camelContext.addComponent(<span class="code-quote">"activemq"</span>,
activeMQComponent(<span class="code-quote">"vm:<span class="code-comment">//localhost?broker.persistent=<span
class="code-keyword">false</span>"</span>));</span>
</pre>
</div></div>

<p>In normal use, an external system would be firing messages or events directly into
Camel through one if its <a href="/confluence/display/CAMEL/Components" title="Components">Components</a>
 but we are going to use the <a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/ProducerTemplate.html"
class="external-link" rel="nofollow">ProducerTemplate</a> which is a really easy
way for testing your configuration:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">ProducerTemplate template = context.createProducerTemplate();
</pre>
</div></div>

<p>Next you <b>must</b> start the camel context. If you are using <a
href="/confluence/display/CAMEL/Spring" title="Spring">Spring</a> to configure the
camel context this is automatically done for you; though if you are using a pure Java approach
then you just need to call the start() method</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
camelContext.start();
</pre>
</div></div>

<p>This will start all of the configured routing rules.</p>

<p>So after starting the <a href="/confluence/display/CAMEL/CamelContext" title="CamelContext">CamelContext</a>,
we can fire some objects into camel:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">for</span> (<span class="code-object">int</span>
i = 0; i &lt; 10; i++) {
    template.sendBody(<span class="code-quote">"test-jms:queue:test.queue"</span>,
<span class="code-quote">"Test Message: "</span> + i);
}
</pre>
</div></div>


<h2><a name="WalkthroughanExample-Whathappens%3F"></a>What happens?</h2>

<p>From the <a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/ProducerTemplate.html"
class="external-link" rel="nofollow">ProducerTemplate</a> - we send objects (in this
case text) into the <a href="/confluence/display/CAMEL/CamelContext" title="CamelContext">CamelContext</a>
to the Component <em>test-jms:queue:test.queue</em>. These text objects will be
<a href="/confluence/display/CAMEL/Type+Converter" title="Type Converter">converted
automatically</a> into JMS Messages and posted to a JMS Queue named <em>test.queue</em>.
When we set up the <a href="/confluence/display/CAMEL/Routes" title="Routes">Route</a>,
we configured the <a href="/confluence/display/CAMEL/File2" title="File2">FileComponent</a>
to listen of the <em>test.queue</em>. </p>

<p>The File <a href="/confluence/display/CAMEL/File2" title="File2">FileComponent</a>
will take messages off the Queue, and save them to a directory named <em>test</em>.
Every message will be saved in a file that corresponds to its destination and message id.</p>

<p>Finally, we configured our own listener in the <a href="/confluence/display/CAMEL/Routes"
title="Routes">Route</a> - to take notifications from the <a href="/confluence/display/CAMEL/File2"
title="File2">FileComponent</a> and print them out as text.</p>

<p><b>That's it!</b></p>

<p>If you have the time then use 5 more minutes to <a href="/confluence/display/CAMEL/Walk+through+another+example"
title="Walk through another example">Walk through another example</a> that demonstrates
the Spring DSL (XML based) routing.</p>
    </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/Walk+through+an+Example">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=53326&revisedVersion=24&originalVersion=23">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Walk+through+an+Example?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message