camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > JDBC-AggregationRepository
Date Sun, 09 Jan 2011 13:48:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1810/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/JDBC-AggregationRepository">JDBC-AggregationRepository</a></h2>
    <h4>Page  <b>added</b> by             <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
         <br/>
    <div class="notificationGreySide">
         <h2><a name="JDBC-AggregationRepository-JDBCAggregationRepository"></a>JDBC-AggregationRepository</h2>
<p><b>Available as of Camel 2.6</b></p>

<p>The <tt>camel-jdbc-aggregator</tt> component allows together with Camel
to provide persistent support for the <a href="/confluence/display/CAMEL/Aggregator2" title="Aggregator2">Aggregator</a>.
</p>

<h3><a name="JDBC-AggregationRepository-UsingJdbcAggregationRepository"></a>Using
JdbcAggregationRepository</h3>
<p><tt>JdbcAggregationRepository</tt> is an <tt>AggregationRepository</tt>
which on the fly persists the aggregated messages. This ensures that you will not loose messages,
as the default aggregator will use an in memory only <tt>AggregationRepository</tt>.</p>

<p>It has the following options:</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Option </th>
<th class='confluenceTh'> Type </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>dataSource</tt> </td>
<td class='confluenceTd'> <tt>DataSource</tt> </td>
<td class='confluenceTd'> <b>Mandatory:</b> The <tt>javax.sql.DataSource</tt>
to use for accessing the database. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>repositoryName</tt> </td>
<td class='confluenceTd'> <tt>String</tt> </td>
<td class='confluenceTd'> <b>Mandatory:</b> The name of the repository.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>transactionManager</tt> </td>
<td class='confluenceTd'> <tt>TransactionManager</tt> </td>
<td class='confluenceTd'> <b>Mandatory:</b> The <tt>org.springframework.transaction.PlatformTransactionManager</tt>
to mange transactions for the database. The TransactionManager must be able to support databases.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>lobHandler</tt> </td>
<td class='confluenceTd'> <tt>LobHandler</tt> </td>
<td class='confluenceTd'> A <tt>org.springframework.jdbc.support.lob.LobHandler</tt>
to handle Lob types in the database. Use this option to use a vendor specific LobHandler,
for example when using Oracle. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>returnOldExchange</tt> </td>
<td class='confluenceTd'> boolean </td>
<td class='confluenceTd'> Whether the get operation should return the old existing Exchange
if any existed. By default this option is <tt>false</tt> to optimize as we do
not need the old exchange when aggregating. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>useRecovery</tt> </td>
<td class='confluenceTd'> boolean </td>
<td class='confluenceTd'> Whether or not recovery is enabled. This option is by default
<tt>true</tt>. When enabled the Camel <a href="/confluence/display/CAMEL/Aggregator2"
title="Aggregator2">Aggregator</a> automatic recover failed aggregated exchange and
have them resubmitted. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>recoveryInterval</tt> </td>
<td class='confluenceTd'> long </td>
<td class='confluenceTd'> If recovery is enabled then a background task is run every
x'th time to scan for failed exchanges to recover and resubmit. By default this interval is
5000 millis. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>maximumRedeliveries</tt> </td>
<td class='confluenceTd'> int </td>
<td class='confluenceTd'> Allows you to limit the maximum number of redelivery attempts
for a recovered exchange. If enabled then the Exchange will be moved to the dead letter channel
if all redelivery attempts failed. By default this option is disabled. If this option is used
then the <tt>deadLetterUri</tt> option must also be provided. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>deadLetterUri</tt> </td>
<td class='confluenceTd'> String </td>
<td class='confluenceTd'> An endpoint uri for a <a href="/confluence/display/CAMEL/Dead+Letter+Channel"
title="Dead Letter Channel">Dead Letter Channel</a> where exhausted recovered Exchanges
will be moved. If this option is used then the <tt>maximumRedeliveries</tt> option
must also be provided. </td>
</tr>
</tbody></table>
</div>



<h3><a name="JDBC-AggregationRepository-Whatispreservedwhenpersisting"></a>What
is preserved when persisting</h3>
<p><tt>JdbcAggregationRepository</tt> will only preserve any <tt>Serializable</tt>
compatible data types. If a data type is not such a type its dropped and a <tt>WARN</tt>
is logged. And it only persists the <tt>Message</tt> body and the <tt>Message</tt>
headers. The <tt>Exchange</tt> properties are <b>not</b> persisted.
</p>

<h3><a name="JDBC-AggregationRepository-Recovery"></a>Recovery</h3>
<p>The <tt>JdbcAggregationRepository</tt> will by default recover any failed
<a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>. It
does this by having a background tasks that scans for failed <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a>s in the persistent store. You can use the <tt>checkInterval</tt>
option to set how often this task runs. The recovery works as transactional which ensures
that Camel will try to recover and redeliver the failed <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a>. Any <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a> which was found to be recovered will be restored from
the persistent store and resubmitted and send out again. </p>

<p>The following headers is set when an <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a> is being recovered/redelivered:</p>
<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>Exchange.REDELIVERED</tt> </td>
<td class='confluenceTd'> Boolean </td>
<td class='confluenceTd'> Is set to true to indicate the <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a> is being redelivered. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>Exchange.REDELIVERY_COUNTER</tt> </td>
<td class='confluenceTd'> Integer </td>
<td class='confluenceTd'> The redelivery attempt, starting from 1. </td>
</tr>
</tbody></table>
</div>


<p>Only when an <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>
has been successfully processed it will be marked as complete which happens when the <tt>confirm</tt>
method is invoked on the <tt>AggregationRepository</tt>. This means if the same
<a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a> fails
again it will be kept retried until it success.</p>

<p>You can use option <tt>maximumRedeliveries</tt> to limit the maximum
number of redelivery attempts for a given recovered <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a>. You must also set the <tt>deadLetterUri</tt>
option so Camel knows where to send the <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>
when the <tt>maximumRedeliveries</tt> was hit. </p>

<p>You can see some examples in the unit tests of camel-jdbc-aggregagor, for example
<a href="https://svn.apache.org/repos/asf/camel/trunk/components/camel-jdbc-aggregator/src/test/java/org/apache/camel/component/jdbc/aggregationrepository/JdbcAggregateRecoverDeadLetterChannelTest.java"
class="external-link" rel="nofollow">this test</a>.</p>

<h3><a name="JDBC-AggregationRepository-Database"></a>Database</h3>

<p>To be operational, each aggregator uses two table: the aggregation and completed
one. By convention the completed has the same name as the aggregation one suffixed with <tt>"_COMPLETED"</tt>.
The name must be configured in the Spring bean with the <tt>RepositoryName</tt>
property. In the following example aggregation will be used.</p>

<p>The table structure definition of both table are identical: in both case a String
value is used as key (<b>id</b>) whereas a Blob contains the exchange serialized
in byte array.<br/>
However one difference should be remembered: the <b>id</b> field does not have
the same content depending on the table.<br/>
In the aggregation table <b>id</b> holds the correlation Id used by the component
to aggregate the messages. In the completed table, <b>id</b> holds the id of the
exchange stored in corresponding the blob field.</p>

<p>Here is the SQL query used to create the tables, just replace <tt>"aggregation"</tt>
with your aggregator repository name.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
CREATE TABLE aggregation (
    id varchar(255) NOT NULL,
    exchange blob NOT NULL,
    constraint aggregation_pk PRIMARY KEY (id)
);
CREATE TABLE aggregation_completed (
    id varchar(255) NOT NULL,
    exchange blob NOT NULL,
    constraint aggregation_completed_pk PRIMARY KEY (id)
);
</pre>
</div></div>

<h3><a name="JDBC-AggregationRepository-Codec%28Serialization%29"></a>Codec
(Serialization)</h3>
<p>Since they can contain any type of payload, Exchanges are not serializable by design.
It is converted into a byte array to be stored in a database BLOB field. All those conversions
are handled by the <tt>JdbcCodec</tt> class. One detail of the code requires your
attention: the <tt>ClassLoadingAwareObjectInputStream</tt>.</p>

<p>The <tt>ClassLoadingAwareObjectInputStream</tt> has been reused from
the <a href="http://activemq.apache.org/" class="external-link" rel="nofollow">Apache
ActiveMQ</a> project. It wraps an <tt>ObjectInputStream</tt> and use it
with the <tt>ContextClassLoader</tt> rather than the <tt>currentThread</tt>
one. The benefit is to be able to load classes exposed by other bundles. This allows the exchange
body and headers to have custom types object references.</p>

<h3><a name="JDBC-AggregationRepository-Transaction"></a>Transaction</h3>
<p>A Spring <tt>PlatformTransactionManager</tt> is required to orchestrate
transaction.</p>

<h3><a name="JDBC-AggregationRepository-Service%28Start%2FStop%29"></a>Service
(Start/Stop)</h3>
<p>The <tt>start</tt> method verify the connection of the database and the
presence of the required tables. If anything is wrong it will fail during starting.</p>

<h3><a name="JDBC-AggregationRepository-Aggregatorconfiguration"></a>Aggregator
configuration</h3>
<p>Depending on the targeted environment, the aggregator might need some configuration.
As you already know, each aggregator should have its own repository (with the corresponding
pair of table created in the database) and a data source. If the default lobHandler is not
adapted to your database system, it can be injected with the <tt>lobHandler</tt>
property.</p>

<p>Here is the declaration for Oracle:</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">"lobHandler"</span>
class=<span class="code-quote">"org.springframework.jdbc.support.lob.OracleLobHandler"</span>&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"nativeJdbcExtractor"</span>
ref=<span class="code-quote">"nativeJdbcExtractor"</span>/&gt;</span>
    <span class="code-tag">&lt;/bean&gt;</span>

    <span class="code-tag">&lt;bean id=<span class="code-quote">"nativeJdbcExtractor"</span>
class=<span class="code-quote">"org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor"</span>/&gt;</span>

    <span class="code-tag">&lt;bean id=<span class="code-quote">"repo"</span>
class=<span class="code-quote">"org.apache.camel.component.jdbc.aggregationRepository.JdbcAggregationRepository"</span>&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"transactionManager"</span>
ref=<span class="code-quote">"transactionManager"</span>/&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"repositoryName"</span>
value=<span class="code-quote">"aggregation"</span>/&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"dataSource"</span>
ref=<span class="code-quote">"dataSource"</span>/&gt;</span>
        <span class="code-tag"><span class="code-comment">&lt;!-- Only with
Oracle, else use default --&gt;</span></span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"lobHandler"</span>
ref=<span class="code-quote">"lobHandler"</span>/&gt;</span>
    <span class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div>

<h3><a name="JDBC-AggregationRepository-Dependencies"></a>Dependencies</h3>
<p>To use <a href="/confluence/display/CAMEL/JDBC-AggregationRepository" title="JDBC-AggregationRepository">JDBC&#45;AggregationRepository</a>
in your camel routes you need to add the a dependency on <b>camel-jdbc-aggregator</b>.
</p>

<p>If you use maven you could just add the following to your pom.xml, substituting the
version number for the latest &amp; greatest release (see <a href="/confluence/display/CAMEL/Download"
title="Download">the download page for the latest versions</a>).</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-jdbc-aggregator<span
class="code-tag">&lt;/artifactId&gt;</span>
  <span class="code-tag">&lt;version&gt;</span>2.6.0<span class="code-tag">&lt;/version&gt;</span>
<span class="code-tag">&lt;/dependency&gt;</span>
</pre>
</div></div>


<h3><a name="JDBC-AggregationRepository-SeeAlso"></a>See Also</h3>
<ul>
	<li><a href="/confluence/display/CAMEL/Configuring+Camel" title="Configuring Camel">Configuring
Camel</a></li>
	<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
Started</a></li>
</ul>

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

    </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/JDBC-AggregationRepository">View
Online</a>
              |
       <a href="https://cwiki.apache.org/confluence/display/CAMEL/JDBC-AggregationRepository?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
           </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message