camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject [CONF] Apache Camel > Walk through an Example
Date Tue, 04 Dec 2012 21:51:00 GMT
    <base href="">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
<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="">Walk
through an Example</a></h2>
    <h4>Page <b>edited</b> by             <a href="">Arnout
        <div id="versionComment">
        Fix link to activeMQComponent<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/}
<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()
<span class="diff-added-words"style="background-color: #dfd;">[{{activeMQComponent()}}
while specifying the [brokerURL|] 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/}
            <tr><td class="diff-snipped" >...<br></td></tr>
    </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=""
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=""
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

<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>

<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"

<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>    }

<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
</span><span class="code-comment">// Note we can explicit name the component
</span>context.addComponent(<span class="code-quote">"test-jms"</span>,

<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=""
class="external-link" rel="nofollow"><tt>activeMQComponent()</tt> method</a>
while specifying the <a href="" 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

<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=""
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();

<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">

<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);

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

<p>From the <a href=""
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 id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href=""
class="grey">Change Notification Preferences</a>
        <a href="">View
        <a href="">View
        <a href=";showCommentArea=true#addcomment">Add

View raw message