camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Cache
Date Tue, 15 Sep 2009 04:14:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="http://cwiki.apache.org/confluence/display/CAMEL/Cache">Cache</a></h2>
     <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~akarpe">Ashwin
Karpe</a>
    </h4>
     
          <br/>
     <div class="notificationGreySide">
         <h2><a name="Cache-CacheComponent"></a>Cache Component</h2>

<p>The <b>cache</b> component enables you to perform caching operations
using EHCache as the Cache Implementation. The cache itself is created on demand or if a cache
of that name already exists then it is simply utilized with its original settings.</p>

<p>This component supports producer and event based consumer endpoints.</p>

<p>The Cache consumer is an event based consumer and can be used to listen and respond
to specific cache activities. If you need to perform selections from a pre-existing cache,
used the processors defined for the cache component.</p>

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

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
cache:cacheName[?options]
</pre>
</div></div>

<p>You can append query options to the URI in the following format, <tt>?option=value&amp;option=value&amp;...</tt></p>

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

<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>maxElementsInMemory</tt> </td>
<td class='confluenceTd'> <tt>1000</tt> </td>
<td class='confluenceTd'> The numer of elements that may be stored in the defined cache
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>memoryStoreEvictionPolicy</tt> </td>
<td class='confluenceTd'> <tt>MemoryStoreEvictionPolicy.LFU</tt> </td>
<td class='confluenceTd'> The number of elements that may be stored in the defined cache.
Options include
<ul class="alternate" type="square">
	<li>MemoryStoreEvictionPolicy.LFU - Least frequently used</li>
	<li>MemoryStoreEvictionPolicy.LRU - Least recently used</li>
	<li>MemoryStoreEvictionPolicy.FIFO - first in first out, the oldest element by creation
time</li>
</ul>
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>overflowToDisk</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> Specifies whether cache may overflow to disk</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>eternal</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> Sets whether elements are eternal. If eternal, timeouts are
ignored and the<br/>
element is never expired. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>timeToLiveSeconds</tt> </td>
<td class='confluenceTd'> <tt>300</tt> </td>
<td class='confluenceTd'> The maximum time between creation time and when an element
expires.<br/>
Is only used if the element is not eternal </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>timeToIdleSeconds</tt> </td>
<td class='confluenceTd'> <tt>300</tt> </td>
<td class='confluenceTd'> The maximum amount of time between accesses before an element
expires </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>diskPersistent</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> Whether the disk store persists between restarts of the Virtual
Machine.<br/>
The default value is false. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>diskExpiryThreadIntervalSeconds</tt> </td>
<td class='confluenceTd'> <tt>120</tt> </td>
<td class='confluenceTd'> The number of seconds between runs of the disk expiry thread.
The default value<br/>
is 120 seconds </td>
</tr>
</tbody></table>

<h3><a name="Cache-Sending%2FReceivingMessagesto%2Ffromthecache"></a>Sending/Receiving
Messages to/from the cache</h3>

<h4><a name="Cache-MessageHeaders"></a>Message Headers</h4>

<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Header </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>CACHE_OPERATION</tt> </td>
<td class='confluenceTd'> The operation to be performed on the cache. The valid options
are
<ul class="alternate" type="square">
	<li>ADD</li>
	<li>UPDATE</li>
	<li>DELETE</li>
	<li>DELETEALL</li>
</ul>
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CACHE_KEY</tt> </td>
<td class='confluenceTd'> The cache key used to store the Message in the cache. The
cache key is optional if the CACHE_OPERATION is DELETEALL </td>
</tr>
</tbody></table>
<h4><a name="Cache-CacheProducer"></a>Cache Producer </h4>
<p>Sending data to the cache involves the ability to direct payloads in exchanges to
be stored in a pre-existing or created-on-   demand cache. The mechanics of doing this involve</p>
<ul class="alternate" type="square">
	<li>setting the Message Exchange Headers shown above.</li>
	<li>ensuring that the Message Exchange Body contains the message directed to the cache</li>
</ul>


<h4><a name="Cache-CacheConsumer"></a>Cache Consumer </h4>
<p>Receiving data from the cache involves the ability of the CacheConsumer to listen
on a pre-existing or created-on-demand Cache using an event Listener and receive automatic
notifications when any cache activity take place (i.e ADD/UPDATE/DELETE/DELETEALL). Upon such
an activity taking place</p>
<ul class="alternate" type="square">
	<li>an exchange containing Message Exchange Headers and a Message Exchange Body containing
the just added/updated payload is placed and sent.</li>
	<li>in case of a DELETEALL operation, the Message Exchange Header CACHE_KEY and the
Message Exchange Body are not populated.</li>
</ul>


<h4><a name="Cache-CacheProcessors"></a>Cache Processors </h4>
<p>There are a set of nice processors with the ability to perform cache lookups and
selectively replace payload content at the</p>
<ul class="alternate" type="square">
	<li>body</li>
	<li>token</li>
	<li>xpath level</li>
</ul>


<h3><a name="Cache-CacheUsageSamples"></a>Cache Usage Samples</h3>

<h4><a name="Cache-Example1%3AConfiguringthecache"></a>Example 1: Configuring
the cache</h4>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"cache:<span class="code-comment">//MyApplicationCache"</span>
+
</span>          <span class="code-quote">"?maxElementsInMemory=1000"</span>
+ 
          <span class="code-quote">"&amp;memoryStoreEvictionPolicy="</span>
+
              <span class="code-quote">"MemoryStoreEvictionPolicy.LFU"</span>
+
          <span class="code-quote">"&amp;overflowToDisk=<span class="code-keyword">true</span>"</span>
+
          <span class="code-quote">"&amp;eternal=<span class="code-keyword">true</span>"</span>
+
          <span class="code-quote">"&amp;timeToLiveSeconds=300"</span> + 
          <span class="code-quote">"&amp;timeToIdleSeconds=<span class="code-keyword">true</span>"</span>
+
          <span class="code-quote">"&amp;diskPersistent=<span class="code-keyword">true</span>"</span>
+
          <span class="code-quote">"&amp;diskExpiryThreadIntervalSeconds=300"</span>)
</pre>
</div></div>

<h4><a name="Cache-Example2%3AAddingkeystothecache"></a>Example 2: Adding
keys to the cache </h4>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
RouteBuilder builder = <span class="code-keyword">new</span> RouteBuilder() {
    <span class="code-keyword">public</span> void configure() {
     from(<span class="code-quote">"direct:start"</span>)
     .setHeader(<span class="code-quote">"CACHE_OPERATION"</span>, 
        constant(<span class="code-quote">"ADD"</span>))
     .setHeader(<span class="code-quote">"CACHE_KEY"</span>, 
        constant(<span class="code-quote">"Ralph_Waldo_Emerson"</span>))
     .to(<span class="code-quote">"cache:<span class="code-comment">//TestCache1"</span>)
</span>   }
};
</pre>
</div></div>

<h4><a name="Cache-Example2%3AUpdatingexistingkeysinacache"></a>Example
2: Updating existing keys in a cache </h4>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
RouteBuilder builder = <span class="code-keyword">new</span> RouteBuilder() {
    <span class="code-keyword">public</span> void configure() {
     from(<span class="code-quote">"direct:start"</span>)
     .setHeader(<span class="code-quote">"CACHE_OPERATION"</span>, 
          constant(<span class="code-quote">"UPDATE"</span>))
     .setHeader(<span class="code-quote">"CACHE_KEY"</span>, 
          constant(<span class="code-quote">"Ralph_Waldo_Emerson"</span>))
     .to(<span class="code-quote">"cache:<span class="code-comment">//TestCache1"</span>)
</span>   }
};
</pre>
</div></div>

<h4><a name="Cache-Example3%3ADeletingexistingkeysinacache"></a>Example
3: Deleting existing keys in a cache </h4>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
RouteBuilder builder = <span class="code-keyword">new</span> RouteBuilder() {
    <span class="code-keyword">public</span> void configure() {
     from(<span class="code-quote">"direct:start"</span>)
     .setHeader(<span class="code-quote">"CACHE_OPERATION"</span>, 
          constant(<span class="code-quote">"DELETE"</span>))
     .setHeader(<span class="code-quote">"CACHE_KEY"</span>, 
          constant(<span class="code-quote">"Ralph_Waldo_Emerson"</span>))
     .to(<span class="code-quote">"cache:<span class="code-comment">//TestCache1"</span>)
</span>   }
};
</pre>
</div></div>

<h4><a name="Cache-Example4%3ADeletingallexistingkeysinacache"></a>Example
4: Deleting all existing keys in a cache </h4>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
RouteBuilder builder = <span class="code-keyword">new</span> RouteBuilder() {
    <span class="code-keyword">public</span> void configure() {
     from(<span class="code-quote">"direct:start"</span>)
     .setHeader(<span class="code-quote">"CACHE_OPERATION"</span>, 
          constant(<span class="code-quote">"DELETEALL"</span>))
     .to(<span class="code-quote">"cache:<span class="code-comment">//TestCache1"</span>);
</span>    }
};
</pre>
</div></div>

<h4><a name="Cache-Example5%3ANotifyinganychangesregisteringinaCachetoProcessorsandotherProducers"></a>Example
5: Notifying any changes registering in a Cache to Processors and other Producers</h4>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
Note: in <span class="code-keyword">this</span> example the consumer is 
     created first and then 3 routes 
     send different message as Cache Producers

RouteBuilder builder = <span class="code-keyword">new</span> RouteBuilder() {
    <span class="code-keyword">public</span> void configure() {
     from(<span class="code-quote">"cache:<span class="code-comment">//TestCache1"</span>)
</span>     .process(<span class="code-keyword">new</span> Processor() {
        <span class="code-keyword">public</span> void process(Exchange exchange)

               <span class="code-keyword">throws</span> Exception {
           <span class="code-object">String</span> operation =
            (<span class="code-object">String</span>) exchange.getIn()
                .getHeader(<span class="code-quote">"CACHE_OPERATION"</span>);
           <span class="code-object">String</span> key =
            (<span class="code-object">String</span>) exchange.getIn()
                .getHeader(<span class="code-quote">"CACHE_KEY"</span>);
           <span class="code-object">Object</span> body = exchange.getIn()
                .getBody();
           <span class="code-comment">// Do something
</span>        } 
     })
   } 
};
</pre>
</div></div>
     </div>
     <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>

       <a href="http://cwiki.apache.org/confluence/display/CAMEL/Cache">View Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=3473777&revisedVersion=5&originalVersion=4">View
Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/CAMEL/Cache?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message