camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Sort
Date Fri, 07 Sep 2012 20:37: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/Sort">Sort</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~muellerc">Christian
Mueller</a>
    </h4>
        <div id="versionComment">
        <b>Comment:</b>
        Removed the deprecated Camel 1.x documentations<br />
    </div>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-unchanged" >h2. Sort <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">*Available
as of Camel 2.0* <br></td></tr>
            <tr><td class="diff-unchanged" > <br>Sort can be used to sort
a message. Imagine you consume text files and before processing each file you want to be sure
the content is sorted. <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="Sort-Sort"></a>Sort</h2>

<p>Sort can be used to sort a message. Imagine you consume text files and before processing
each file you want to be sure the content is sorted.</p>

<p>Sort will by default sort the body using a default comparator that handles numeric
values or uses the string representation. You can provide your own comparator, and even an
expression to return the value to be sorted. Sort requires the value returned from the expression
evaluation is convertible to <tt>java.util.List</tt> as this is required by the
JDK sort operation.</p>

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

<div class="confluenceTableSmall"><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>comparatorRef</tt> </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> Refers to a custom <tt>java.util.Comparator</tt>
to use for sorting the message body. Camel will by default use a comparator which does a A..Z
sorting. </td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="Sort-UsingfromJavaDSL"></a>Using from Java DSL</h3>
<p>In the route below it will read the file content and tokenize by line breaks so each
line can be sorted. </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"file:<span class="code-comment">//inbox"</span>).sort(body().tokenize(<span
class="code-quote">"\n"</span>)).to(<span class="code-quote">"bean:MyServiceBean.processLine"</span>);</span>
</pre>
</div></div>

<p>You can pass in your own comparator as a 2nd argument:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"file:<span class="code-comment">//inbox"</span>).sort(body().tokenize(<span
class="code-quote">"\n"</span>), <span class="code-keyword">new</span>
MyReverseComparator()).to(<span class="code-quote">"bean:MyServiceBean.processLine"</span>);</span>
</pre>
</div></div>


<h3><a name="Sort-UsingfromSpringDSL"></a>Using from Spring DSL</h3>
<p>In the route below it will read the file content and tokenize by line breaks so each
line can be sorted. </p>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>Camel 2.7 or better</b></div><div
class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;route&gt;</span>
  <span class="code-tag">&lt;from uri=<span class="code-quote">"file://inbox"</span>/&gt;</span>
  <span class="code-tag">&lt;sort&gt;</span>
    <span class="code-tag">&lt;simple&gt;</span>body<span class="code-tag">&lt;/simple&gt;</span>
  <span class="code-tag">&lt;/sort&gt;</span>
  <span class="code-tag">&lt;beanRef ref=<span class="code-quote">"myServiceBean"</span>
method=<span class="code-quote">"processLine"</span>/&gt;</span>
<span class="code-tag">&lt;/route&gt;</span>
</pre>
</div></div>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>Camel 2.6 or older</b></div><div
class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;route&gt;</span>
  <span class="code-tag">&lt;from uri=<span class="code-quote">"file://inbox"</span>/&gt;</span>
  <span class="code-tag">&lt;sort&gt;</span>
    <span class="code-tag">&lt;expression&gt;</span>
      <span class="code-tag">&lt;simple&gt;</span>body<span class="code-tag">&lt;/simple&gt;</span>
    <span class="code-tag">&lt;/expression&gt;</span>
  <span class="code-tag">&lt;/sort&gt;</span>
  <span class="code-tag">&lt;beanRef ref=<span class="code-quote">"myServiceBean"</span>
method=<span class="code-quote">"processLine"</span>/&gt;</span>
<span class="code-tag">&lt;/route&gt;</span>
</pre>
</div></div>

<p>And to use our own comparator we can refer to it as a spring bean:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>Camel 2.7 or better</b></div><div
class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;route&gt;</span>
  <span class="code-tag">&lt;from uri=<span class="code-quote">"file://inbox"</span>/&gt;</span>
  <span class="code-tag">&lt;sort comparatorRef=<span class="code-quote">"myReverseComparator"</span>&gt;</span>
    <span class="code-tag">&lt;simple&gt;</span>body<span class="code-tag">&lt;/simple&gt;</span>
  <span class="code-tag">&lt;/sort&gt;</span>
  <span class="code-tag">&lt;beanRef ref=<span class="code-quote">"MyServiceBean"</span>
method=<span class="code-quote">"processLine"</span>/&gt;</span>
<span class="code-tag">&lt;/route&gt;</span>

<span class="code-tag">&lt;bean id=<span class="code-quote">"myReverseComparator"</span>
class=<span class="code-quote">"com.mycompany.MyReverseComparator"</span>/&gt;</span>
</pre>
</div></div>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>Camel 2.6 or older</b></div><div
class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;route&gt;</span>
  <span class="code-tag">&lt;from uri=<span class="code-quote">"file://inbox"</span>/&gt;</span>
  <span class="code-tag">&lt;sort comparatorRef=<span class="code-quote">"myReverseComparator"</span>&gt;</span>
    <span class="code-tag">&lt;expression&gt;</span>
      <span class="code-tag">&lt;simple&gt;</span>body<span class="code-tag">&lt;/simple&gt;</span>
    <span class="code-tag">&lt;/expression&gt;</span>
  <span class="code-tag">&lt;/sort&gt;</span>
  <span class="code-tag">&lt;beanRef ref=<span class="code-quote">"MyServiceBean"</span>
method=<span class="code-quote">"processLine"</span>/&gt;</span>
<span class="code-tag">&lt;/route&gt;</span>

<span class="code-tag">&lt;bean id=<span class="code-quote">"myReverseComparator"</span>
class=<span class="code-quote">"com.mycompany.MyReverseComparator"</span>/&gt;</span>
</pre>
</div></div>

<p>Besides <tt>&lt;simple&gt;</tt>, you can supply an expression
using any <a href="/confluence/display/CAMEL/Languages" title="Languages">language</a>
you like, so long as it returns a list.</p>

<h4><a name="Sort-UsingThisPattern"></a>Using This Pattern</h4>

<p>If you would like to use this EIP Pattern then please read the <a href="/confluence/display/CAMEL/Getting+Started"
title="Getting Started">Getting Started</a>, you may also find the <a href="/confluence/display/CAMEL/Architecture"
title="Architecture">Architecture</a> useful particularly the description of <a
href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a> and <a
href="/confluence/display/CAMEL/URIs" title="URIs">URIs</a>. Then you could try out
some of the <a href="/confluence/display/CAMEL/Examples" title="Examples">Examples</a>
first before trying this pattern out.</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/Sort">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=110809&revisedVersion=8&originalVersion=7">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Sort?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message