camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject [CONF] Apache Camel > Cache
Date Mon, 14 Sep 2009 21:56:00 GMT
    <base href="">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="">Cache</a></h2>
    <h4>Page  <b>added</b> by             <a href="">Ashwin
    <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>The Cache component has the following abilities</p>

<p>a&gt; In Producer mode, the component provides the ability to direct payloads
in exchanges to a stored in a pre-existing or created-on-demand Cache. The producer mode supports
operations to ADD/UPDATE/DELETE/DELETEALL elements in a cache. (Examples given below)</p>

<p>b&gt; In Consumer mode, the component provides the ability 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, an exchange containing header elements describing the operation and cachekey and a
body containing the just added/updated payload is placed and sent. In case of a DELETEALL
operation the body of the exchanage is not populated.</p>

<p>c&gt; There are a set of nice processors to the camel-cache component to provide
the ability to perform cache lookups and selectively replace payload content at the</p>
<ul class="alternate" type="square">
	<li>xpath level</li>

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

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

<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>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Default Value </th>
<th class='confluenceTh'> Description </th>
<td class='confluenceTd'> <tt>readSize</tt> </td>
<td class='confluenceTd'> <tt>0</tt> / <tt>2000</tt> </td>
<td class='confluenceTd'> The default maximum number of rows that can be read by a polling
query. The default value is 2000 for Camel 1.5.0 or older. In newer releases the default value
is 0. </td>

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

<p>The result is returned in the OUT body as an <tt>ArrayList&lt;HashMap&lt;String,
Object&gt;&gt;</tt>. The <tt>List</tt> object contains the list
of rows and the <tt>Map</tt> objects contain each row with the <tt>String</tt>
key as the column name.</p>

<p><b>Note:</b> This component fetches <tt>ResultSetMetaData</tt>
to be able to return the column name as the key in the <tt>Map</tt>.</p>

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

<table class='confluenceTable'><tbody>
<th class='confluenceTh'> Header </th>
<th class='confluenceTh'> Description </th>
<td class='confluenceTd'> <tt>CamelJdbcRowCount</tt> </td>
<td class='confluenceTd'> If the query is a <tt>SELECT</tt>, query the row
count is returned in this OUT header. </td>
<td class='confluenceTd'> <tt>CamelJdbcUpdateCount</tt> </td>
<td class='confluenceTd'> If the query is an <tt>UPDATE</tt>, query the
update count is returned in this OUT header. </td>

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

<p>In the following example, we fetch the rows from the customer table.</p>

<p>First we register our datasource in the Camel registry as <tt>testdb</tt>:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">JndiRegistry reg = <span class="code-keyword">super</span>.createRegistry();
reg.bind(<span class="code-quote">"testdb"</span>, ds);
<span class="code-keyword">return</span> reg;

<p>Then we configure a route that routes to the JDBC component, so the SQL will be executed.
Note how we refer to the <tt>testdb</tt> datasource that was bound in the previous

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-comment">// lets add simple route
</span><span class="code-keyword">public</span> void configure() <span
class="code-keyword">throws</span> Exception {
    from(<span class="code-quote">"direct:hello"</span>).to(<span class="code-quote">"jdbc:testdb?readSize=100"</span>);

<p>Or you can create a <tt>DataSource</tt> in Spring like this:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">&lt;camelContext id=<span class="code-quote">"camel"</span>
xmlns=<span class="code-quote">"http:<span class="code-comment">//"</span>&gt;
</span>  &lt;route&gt;
     &lt;from uri=<span class="code-quote">"timer:<span class="code-comment">//kickoff?period=10000"</span>/&gt;
</span>     &lt;setBody&gt;
       &lt;constant&gt;select * from customer&lt;/constant&gt;
     &lt;to uri=<span class="code-quote">"jdbc:testdb"</span>/&gt;
     &lt;to uri=<span class="code-quote">"mock:result"</span>/&gt;
&lt;!-- Just add a demo to show how to bind a date source <span class="code-keyword">for</span>
camel in Spring--&gt;
&lt;bean id=<span class="code-quote">"testdb"</span> class=<span class="code-quote">"org.springframework.jdbc.datasource.DriverManagerDataSource"</span>&gt;
	&lt;property name=<span class="code-quote">"driverClassName"</span> value=<span
	&lt;property name=<span class="code-quote">"url"</span> value=<span class="code-quote">"jdbc:hsqldb:mem:camel_jdbc"</span>
	&lt;property name=<span class="code-quote">"username"</span> value=<span
class="code-quote">"sa"</span> /&gt;
  &lt;property name=<span class="code-quote">"password"</span> value="" /&gt;


<p>We create an endpoint, add the SQL query to the body of the IN message, and then
send the exchange. The result of the query is returned in the OUT body:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-comment">// first we create our exchange
using the endpoint
</span>Endpoint endpoint = context.getEndpoint(<span class="code-quote">"direct:hello"</span>);
Exchange exchange = endpoint.createExchange();
<span class="code-comment">// then we set the SQL on the in body
</span>exchange.getIn().setBody(<span class="code-quote">"select * from customer
order by ID"</span>);

<span class="code-comment">// now we send the exchange to the endpoint, and receives
the response from Camel
</span>Exchange out = template.send(endpoint, exchange);

<span class="code-comment">// assertions of the response
ArrayList&lt;HashMap&lt;<span class="code-object">String</span>, <span
class="code-object">Object</span>&gt;&gt; data = out.getOut().getBody(ArrayList.class);
assertNotNull(<span class="code-quote">"out body could not be converted to an ArrayList
- was: "</span>
    + out.getOut().getBody(), data);
assertEquals(2, data.size());
HashMap&lt;<span class="code-object">String</span>, <span class="code-object">Object</span>&gt;
row = data.get(0);
assertEquals(<span class="code-quote">"cust1"</span>, row.get(<span class="code-quote">"ID"</span>));
assertEquals(<span class="code-quote">"jstrachan"</span>, row.get(<span class="code-quote">"NAME"</span>));
row = data.get(1);
assertEquals(<span class="code-quote">"cust2"</span>, row.get(<span class="code-quote">"ID"</span>));
assertEquals(<span class="code-quote">"nsandhu"</span>, row.get(<span class="code-quote">"NAME"</span>));

<h3><a name="Cache-SamplePollingthedatabaseeveryminute"></a>Sample - Polling
the database every minute</h3>

<p>If we want to poll a database using the JDBC component, we need to combine it with
a polling scheduler such as the <a href="/confluence/display/CAMEL/Timer" title="Timer">Timer</a>
or <a href="/confluence/display/CAMEL/Quartz" title="Quartz">Quartz</a> etc. In
the following example, we retrieve data from the database every 60 seconds:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"timer:<span class="code-comment">//foo?period=60000"</span>).setBody(constant(<span
class="code-quote">"select * from customer"</span>)).to(<span class="code-quote">"jdbc:testdb"</span>).to(<span

<h3><a name="Cache-SeeAlso"></a>See Also</h3>
	<li><a href="/confluence/display/CAMEL/Configuring+Camel" title="Configuring Camel">Configuring
	<li><a href="/confluence/display/CAMEL/Component" title="Component">Component</a></li>
	<li><a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a></li>
	<li><a href="/confluence/display/CAMEL/Getting+Started" title="Getting Started">Getting

<ul class="alternate" type="square">
	<li><a href="/confluence/display/CAMEL/SQL" title="SQL">SQL</a></li>

    <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href=""
class="grey">Change Notification Preferences</a>
       <a href="">View Online</a>
       <a href=";showCommentArea=true#addcomment">Add

View raw message