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 Sun, 08 Jul 2012 06:46: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/Cache">Cache</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</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" >{div} <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">The
{{CamelCacheAdd}} and {{CamelCacheUpdate}} supports additional headers to control time to
live, time to idle, and eternal.  <br>{div:class=confluenceTableSmall} <br>||
Header || Type ||  Description || <br>| {{CamelCacheTimeToLive}} | {{Integer}} | *Camel
2.11:* Time to live. | <br>| {{CamelCacheTimeToIdle}} | {{Integer}} | *Camel 2.11:*
Time to idle. | <br>| {{CamelCacheEternal}} | {{Boolean}} | *Camel 2.11:* Whether the
content is eternal. | <br>{div} <br> <br> <br></td></tr>
            <tr><td class="diff-unchanged" >h4. Cache Producer <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="Cache-CacheComponent"></a>Cache Component</h2>

<p><b>Available as of Camel 2.1</b></p>

<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,
use the processors defined for the cache component.</p>

<p>Maven users will need to add the following dependency to their <tt>pom.xml</tt>
for this component:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;dependency&gt;</span>
    <span class="code-tag">&lt;groupId&gt;</span>org.apache.camel<span
class="code-tag">&lt;/groupId&gt;</span>
    <span class="code-tag">&lt;artifactId&gt;</span>camel-cache<span
class="code-tag">&lt;/artifactId&gt;</span>
    <span class="code-tag">&lt;version&gt;</span>x.x.x<span class="code-tag">&lt;/version&gt;</span>
    <span class="code-tag"><span class="code-comment">&lt;!-- use the same
version as your Camel core version --&gt;</span></span>
<span class="code-tag">&lt;/dependency&gt;</span>
</pre>
</div></div>

<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:<span class="code-comment">//cacheName[?options]</span>
</pre>
</div></div>

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

<h3><a name="Cache-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>maxElementsInMemory</tt> </td>
<td class='confluenceTd'> <tt>1000</tt> </td>
<td class='confluenceTd'> The number 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 class="atl-forced-newline" />
 element never expires. </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 class="atl-forced-newline" />
 Is used only 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>false</tt> </td>
<td class='confluenceTd'> Whether the disk store persists between restarts of the Virtual
Machine. </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.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>cacheManagerFactory</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 2.8:</b> If you want to use a custom
factory which instantiates and creates the EHCache <tt>net.sf.ehcache.CacheManager</tt>.
<br class="atl-forced-newline" />  <br class="atl-forced-newline" />  <em>Type:</em>
abstract org.apache.camel.component.cache.CacheManagerFactory </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>eventListenerRegistry</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 2.8:</b> Sets a list of EHCache <tt>net.sf.ehcache.event.CacheEventListener</tt>
for all new caches&#45; no need to define it per cache in EHCache xml config anymore.
<br class="atl-forced-newline" />  <br class="atl-forced-newline" />  <em>Type:</em>
org.apache.camel.component.cache.CacheEventListenerRegistry </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>cacheLoaderRegistry</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 2.8:</b> Sets a list of <tt>org.apache.camel.component.cache.CacheLoaderWrapper</tt>
that extends EHCache <tt>net.sf.ehcache.loader.CacheLoader</tt> for all new caches&#45;
no need to define it per cache in EHCache xml config anymore. <br class="atl-forced-newline"
/>  <br class="atl-forced-newline" />  <em>Type:</em> org.apache.camel.component.cache.CacheLoaderRegistry
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>key</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 2.10:</b> To configure using a cache
key by default. If a key is provided in the message header, then the key from the header takes
precedence. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>operation</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 2.10:</b> To configure using an cache
operation by default. If an operation in the message header, then the operation from the header
takes precedence. </td>
</tr>
</tbody></table>
</div>
</div>

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

<h4><a name="Cache-MessageHeadersuptoCamel2.7"></a>Message Headers up to
Camel 2.7</h4>
<div class="confluenceTableSmall"><div class='table-wrap'>
<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. Valid options
are
<ul class="alternate" type="square">
	<li>GET</li>
	<li>CHECK</li>
	<li>ADD</li>
	<li>UPDATE</li>
	<li>DELETE</li>
	<li>DELETEALL<br class="atl-forced-newline" />
<tt>GET</tt> and <tt>CHECK</tt> requires <b>Camel 2.3</b>
onwards. </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>
</div>
</div>

<h4><a name="Cache-MessageHeadersCamel2.8%5C"></a>Message Headers Camel
2.8&#43;</h4>

<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><b>Header changes
in Camel 2.8</b><br />The header names and supported values have changed to be
prefixed with 'CamelCache' and use mixed case.  This makes them easier to identify and keep
separate from other headers.  The CacheConstants variable names remain unchanged, just their
values have been changed.  Also, these headers are now removed from the exchange after the
cache operation is performed.</td></tr></table></div>

<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Header </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelCacheOperation</tt> </td>
<td class='confluenceTd'> The operation to be performed on the cache. The valid options
are
<ul class="alternate" type="square">
	<li>CamelCacheGet</li>
	<li>CamelCacheCheck</li>
	<li>CamelCacheAdd</li>
	<li>CamelCacheUpdate</li>
	<li>CamelCacheDelete</li>
	<li>CamelCacheDeleteAll</li>
</ul>
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelCacheKey</tt> </td>
<td class='confluenceTd'> The cache key used to store the Message in the cache. The
cache key is optional if the CamelCacheOperation is CamelCacheDeleteAll </td>
</tr>
</tbody></table>
</div>
</div>

<p>The <tt>CamelCacheAdd</tt> and <tt>CamelCacheUpdate</tt>
supports additional headers to control time to live, time to idle, and eternal. </p>
<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Header </th>
<th class='confluenceTh'> Type </th>
<th class='confluenceTh'>  Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelCacheTimeToLive</tt> </td>
<td class='confluenceTd'> <tt>Integer</tt> </td>
<td class='confluenceTd'> <b>Camel 2.11:</b> Time to live. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelCacheTimeToIdle</tt> </td>
<td class='confluenceTd'> <tt>Integer</tt> </td>
<td class='confluenceTd'> <b>Camel 2.11:</b> Time to idle. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelCacheEternal</tt> </td>
<td class='confluenceTd'> <tt>Boolean</tt> </td>
<td class='confluenceTd'> <b>Camel 2.11:</b> Whether the content is eternal.
</td>
</tr>
</tbody></table>
</div>
</div>


<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 CamelCacheGet/CamelCacheUpdate/CamelCacheDelete/CamelCacheDeleteAll).
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 CamelCacheDeleteAll operation, the Message Exchange Header CamelCacheKey
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(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_OPERATION_ADD))
     .setHeader(CacheConstants.CACHE_KEY, 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(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_OPERATION_UPDATE))
     .setHeader(CacheConstants.CACHE_KEY, 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(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_DELETE))
     .setHeader(CacheConstants.CACHE_KEY<span class="code-quote">", constant("</span>Ralph_Waldo_Emerson"))
     .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(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_DELETEALL))
     .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">
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(CacheConstants.CACHE_OPERATION);
           <span class="code-object">String</span> key = (<span class="code-object">String</span>)
exchange.getIn().getHeader(CacheConstants.CACHE_KEY);
           <span class="code-object">Object</span> body = exchange.getIn().getBody();
           <span class="code-comment">// Do something
</span>        }
     })
   }
};
</pre>
</div></div>

<h4><a name="Cache-Example6%3AUsingProcessorstoselectivelyreplacepayloadwithcachevalues"></a>Example
6: Using Processors to selectively replace payload with cache values</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() {
     <span class="code-comment">//Message Body Replacer
</span>     from(<span class="code-quote">"cache:<span class="code-comment">//TestCache1"</span>)
</span>     .filter(header(CacheConstants.CACHE_KEY).isEqualTo(<span class="code-quote">"greeting"</span>))
     .process(<span class="code-keyword">new</span> CacheBasedMessageBodyReplacer(<span
class="code-quote">"cache:<span class="code-comment">//TestCache1"</span>,<span
class="code-quote">"farewell"</span>))
</span>     .to(<span class="code-quote">"direct:next"</span>);

    <span class="code-comment">//Message Token replacer
</span>    from(<span class="code-quote">"cache:<span class="code-comment">//TestCache1"</span>)
</span>    .filter(header(CacheConstants.CACHE_KEY).isEqualTo(<span class="code-quote">"quote"</span>))
    .process(<span class="code-keyword">new</span> CacheBasedTokenReplacer(<span
class="code-quote">"cache:<span class="code-comment">//TestCache1"</span>,<span
class="code-quote">"novel"</span>,<span class="code-quote">"#novel#"</span>))
</span>    .process(<span class="code-keyword">new</span> CacheBasedTokenReplacer(<span
class="code-quote">"cache:<span class="code-comment">//TestCache1"</span>,<span
class="code-quote">"author"</span>,<span class="code-quote">"#author#"</span>))
</span>    .process(<span class="code-keyword">new</span> CacheBasedTokenReplacer(<span
class="code-quote">"cache:<span class="code-comment">//TestCache1"</span>,<span
class="code-quote">"number"</span>,<span class="code-quote">"#number#"</span>))
</span>    .to(<span class="code-quote">"direct:next"</span>);

    <span class="code-comment">//Message XPath replacer
</span>    from(<span class="code-quote">"cache:<span class="code-comment">//TestCache1"</span>).
</span>    .filter(header(CacheConstants.CACHE_KEY).isEqualTo(<span class="code-quote">"XML_FRAGMENT"</span>))
    .process(<span class="code-keyword">new</span> CacheBasedXPathReplacer(<span
class="code-quote">"cache:<span class="code-comment">//TestCache1"</span>,<span
class="code-quote">"book1"</span>,<span class="code-quote">"/books/book1"</span>))
</span>    .process (<span class="code-keyword">new</span> CacheBasedXPathReplacer(<span
class="code-quote">"cache:<span class="code-comment">//TestCache1"</span>,<span
class="code-quote">"book2"</span>,<span class="code-quote">"/books/book2"</span>))
</span>    .to(<span class="code-quote">"direct:next"</span>);
   }
};
</pre>
</div></div>

<h4><a name="Cache-Example7%3AGettinganentryfromtheCache"></a>Example 7:
Getting an entry from the Cache</h4>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"direct:start"</span>)
    <span class="code-comment">// Prepare headers
</span>    .setHeader(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_OPERATION_GET))
    .setHeader(CacheConstants.CACHE_KEY, constant(<span class="code-quote">"Ralph_Waldo_Emerson"</span>)).
    .to(<span class="code-quote">"cache:<span class="code-comment">//TestCache1"</span>).
</span>    <span class="code-comment">// Check <span class="code-keyword">if</span>
entry was not found
</span>    .choice().when(header(CacheConstants.CACHE_ELEMENT_WAS_FOUND).isNull()).
        <span class="code-comment">// If not found, get the payload and put it to cache
</span>        .to(<span class="code-quote">"cxf:bean:someHeavyweightOperation"</span>).
        .setHeader(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_OPERATION_ADD))
        .setHeader(CacheConstants.CACHE_KEY, constant(<span class="code-quote">"Ralph_Waldo_Emerson"</span>))
        .to(<span class="code-quote">"cache:<span class="code-comment">//TestCache1"</span>)
</span>    .end()
    .to(<span class="code-quote">"direct:nextPhase"</span>);
</pre>
</div></div>

<h4><a name="Cache-Example8%3ACheckingforanentryintheCache"></a>Example
8: Checking for an entry in the Cache</h4>

<p>Note: The CHECK command tests existence of an entry in the cache but doesn't place
a message in the body.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"direct:start"</span>)
    <span class="code-comment">// Prepare headers
</span>    .setHeader(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_OPERATION_CHECK))
    .setHeader(CacheConstants.CACHE_KEY, constant(<span class="code-quote">"Ralph_Waldo_Emerson"</span>)).
    .to(<span class="code-quote">"cache:<span class="code-comment">//TestCache1"</span>).
</span>    <span class="code-comment">// Check <span class="code-keyword">if</span>
entry was not found
</span>    .choice().when(header(CacheConstants.CACHE_ELEMENT_WAS_FOUND).isNull()).
        <span class="code-comment">// If not found, get the payload and put it to cache
</span>        .to(<span class="code-quote">"cxf:bean:someHeavyweightOperation"</span>).
        .setHeader(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_OPERATION_ADD))
        .setHeader(CacheConstants.CACHE_KEY, constant(<span class="code-quote">"Ralph_Waldo_Emerson"</span>))
        .to(<span class="code-quote">"cache:<span class="code-comment">//TestCache1"</span>)
</span>    .end();
</pre>
</div></div>


<h3><a name="Cache-ManagementofEHCache"></a>Management of EHCache</h3>

<p><a href="http://ehcache.org/" class="external-link" rel="nofollow">EHCache</a>
has its own statistics and management from <a href="/confluence/display/CAMEL/Camel+JMX"
title="Camel JMX">JMX</a>.</p>

<p>Here's a snippet on how to expose them via JMX in a Spring application context:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;bean id=<span class="code-quote">"ehCacheManagementService"</span>
class=<span class="code-quote">"net.sf.ehcache.management.ManagementService"</span>
init-method=<span class="code-quote">"init"</span> lazy-init=<span class="code-quote">"false"</span>&gt;</span>
  <span class="code-tag">&lt;constructor-arg&gt;</span>
    <span class="code-tag">&lt;bean class=<span class="code-quote">"net.sf.ehcache.CacheManager"</span>
factory-method=<span class="code-quote">"getInstance"</span>/&gt;</span>
  <span class="code-tag">&lt;/constructor-arg&gt;</span>
  <span class="code-tag">&lt;constructor-arg&gt;</span>
    <span class="code-tag">&lt;bean class=<span class="code-quote">"org.springframework.jmx.support.JmxUtils"</span>
factory-method=<span class="code-quote">"locateMBeanServer"</span>/&gt;</span>
  <span class="code-tag">&lt;/constructor-arg&gt;</span>
  <span class="code-tag">&lt;constructor-arg value=<span class="code-quote">"true"</span>/&gt;</span>
  <span class="code-tag">&lt;constructor-arg value=<span class="code-quote">"true"</span>/&gt;</span>
  <span class="code-tag">&lt;constructor-arg value=<span class="code-quote">"true"</span>/&gt;</span>
  <span class="code-tag">&lt;constructor-arg value=<span class="code-quote">"true"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div>

<p>Of course you can do the same thing in straight Java:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
ManagementService.registerMBeans(CacheManager.getInstance(), mbeanServer, <span class="code-keyword">true</span>,
<span class="code-keyword">true</span>, <span class="code-keyword">true</span>,
<span class="code-keyword">true</span>);
</pre>
</div></div>

<p>You can get cache hits, misses, in-memory hits, disk hits, size stats this way. You
can also change CacheConfiguration parameters on the fly.</p>

<h3><a name="Cache-CachereplicationCamel2.8%5C"></a>Cache replication Camel
2.8&#43;</h3>

<p>The Camel Cache component is able to distribute a cache across server nodes using
several different replication mechanisms including: RMI, JGroups, JMS and Cache Server.</p>


<p>There are two different ways to make it work:</p>

<p><b>1.</b> You can configure <tt>ehcache.xml</tt> manually</p>

<p>OR</p>

<p><b>2.</b> You can configure these three options:</p>
<ul class="alternate" type="square">
	<li>cacheManagerFactory</li>
	<li>eventListenerRegistry</li>
	<li>cacheLoaderRegistry</li>
</ul>



<p>Configuring Camel Cache replication using the first option is a bit of hard work
as you have to configure all caches separately. So in a situation when the all names of caches
are not known, using <tt>ehcache.xml</tt> is not a good idea.</p>

<p>The second option is much better when you want to use many different caches as you
do not need to define options per cache. This is because replication options are set per <tt>CacheManager</tt>
and per <tt>CacheEndpoint</tt>. Also it is the only way when cache names are not
know at the development phase.</p>


<div class='panelMacro'><table class='noteMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/warning.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td>It might be useful to read the
<a href="http://ehcache.org/documentation" class="external-link" rel="nofollow">EHCache
manual</a> to get a better understanding of the Camel Cache replication mechanism.</td></tr></table></div>

<h4><a name="Cache-Example%3AJMScachereplication"></a>Example: JMS cache
replication</h4>

<p>JMS replication is the most powerful and secured replication method. Used together
with Camel Cache replication makes it also rather simple.<br/>
An example is available on <a href="/confluence/display/CAMEL/cacheReplicationJMSExample"
title="cacheReplicationJMSExample">a separate page</a>.</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/Cache">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=3473777&revisedVersion=21&originalVersion=20">View
Changes</a>
                |
        <a href="https://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