camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > SQL Component
Date Fri, 29 Apr 2011 21:08: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/SQL+Component">SQL
Component</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~muellerc">Christian
Mueller</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" >{warning} <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">{info}
<br>This component can be used as a [Transactional Client|http://camel.apache.org/transactional-client.html].
<br>{info} <br> <br></td></tr>
            <tr><td class="diff-unchanged" >The SQL component uses the following
endpoint URI notation: <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="SQLComponent-SQLComponent"></a>SQL Component</h2>

<p>The <b>sql:</b> component allows you to work with databases using JDBC
queries. The difference between this component and <a href="/confluence/display/CAMEL/JDBC"
title="JDBC">JDBC</a> component is that in case of SQL the query is a property of
the endpoint and it uses message payload as parameters passed to the query.</p>

<p>This component uses <b><tt>spring-jdbc</tt></b> behind the
scenes for the actual SQL handling.</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-sql<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>

<p>The SQL component also supports:</p>
<ul class="alternate" type="square">
	<li>a JDBC based repository for the <a href="/confluence/display/CAMEL/Idempotent+Consumer"
title="Idempotent Consumer">Idempotent Consumer</a> EIP pattern. See further below.</li>
	<li>a JDBC based repository for the <a href="/confluence/display/CAMEL/Aggregator2"
title="Aggregator2">Aggregator</a> EIP pattern. See further below.</li>
</ul>


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

<div class='panelMacro'><table class='warningMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/forbidden.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td>The SQL component
can only be used to define producer endpoints. In other words, you cannot define an SQL endpoint
in a <tt>from()</tt> statement.</td></tr></table></div>

<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>This component can
be used as a <a href="http://camel.apache.org/transactional-client.html" class="external-link"
rel="nofollow">Transactional Client</a>.</td></tr></table></div>

<p>The SQL component uses the following endpoint URI notation:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
sql:select * from table where id=# order by name[?options]
</pre>
</div></div>

<p>Notice that the standard <tt>?</tt> symbol that denotes the parameters
to an SQL query is substituted with the <tt>#</tt> symbol, because the <tt>?</tt>
symbol is used to specify options for the endpoint. The <tt>?</tt> symbol replacement
can be configured on endpoint basis.</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="SQLComponent-Options"></a>Options</h3>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Option </th>
<th class='confluenceTh'> Type </th>
<th class='confluenceTh'> Default </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>dataSourceRef</tt> </td>
<td class='confluenceTd'> <tt>String</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 1.5.1/2.0:</b> Reference to a <tt>DataSource</tt>
to look up in the registry. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>placeholder</tt> </td>
<td class='confluenceTd'> <tt>String</tt> </td>
<td class='confluenceTd'> <tt>#</tt> </td>
<td class='confluenceTd'> <b>Camel 2.4:</b> Specifies a character that will
be replaced to <tt>?</tt> in SQL query. Notice, that it is simple <tt>String.replaceAll()</tt>
operation and no SQL parsing is involved (quoted strings will also change) </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>template.&lt;xxx&gt;</tt> </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Sets additional options on the Spring <tt>JdbcTemplate</tt>
that is used behind the scenes to execute the queries. For instance, <tt>template.maxRows=10</tt>.
For detailed documentation, see the <a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/jdbc/core/JdbcTemplate.html"
class="external-link" rel="nofollow">JdbcTemplate javadoc</a> documentation. </td>
</tr>
</tbody></table>
</div>


<h3><a name="SQLComponent-Treatmentofthemessagebody"></a>Treatment of the
message body</h3>

<p>The SQL component tries to convert the message body to an object of <tt>java.util.Iterator</tt>
type and then uses this iterator to fill the query parameters (where each query parameter
is represented by a <tt>#</tt> symbol (or configured placeholder) in the endpoint
URI). If the message body is not an array or collection, the conversion results in an iterator
that iterates over only one object, which is the body itself.</p>

<p>For example, if the message body is an instance of <tt>java.util.List</tt>,
the first item in the list is substituted into the first occurrence of <tt>#</tt>
in the SQL query, the second item in the list is substituted into the second occurrence of
<tt>#</tt>, and so on.</p>

<h3><a name="SQLComponent-Resultofthequery"></a>Result of the query</h3>

<p>For <tt>select</tt> operations, the result is an instance of <tt>List&lt;Map&lt;String,
Object&gt;&gt;</tt> type, as returned by the <a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/jdbc/core/JdbcTemplate.html#queryForList(java.lang.String,%20java.lang.Object%91%93)"
class="external-link" rel="nofollow">JdbcTemplate.queryForList()</a> method. For
<tt>update</tt> operations, the result is the number of updated rows, returned
as an <tt>Integer</tt>.</p>

<h3><a name="SQLComponent-Headervalues"></a>Header values</h3>

<p>When performing <tt>update</tt> operations, the SQL Component stores
the update count in the following message headers:</p>

<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>SqlProducer.UPDATE_COUNT</tt> </td>
<td class='confluenceTd'> Camel 1.x: The number of rows updated for <tt>update</tt>
operations, returned as an <tt>Integer</tt> object. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelSqlUpdateCount</tt> </td>
<td class='confluenceTd'> Camel 2.0: The number of rows updated for <tt>update</tt>
operations, returned as an <tt>Integer</tt> object. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelSqlRowCount</tt> </td>
<td class='confluenceTd'> Camel 2.0: The number of rows returned for <tt>select</tt>
operations, returned as an <tt>Integer</tt> object. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelSqlQuery</tt> </td>
<td class='confluenceTd'> <b>Camel 2.8:</b> Query to execute. This query
takes precedence over the query specified in the endpoint URI. Note that query parameters
in the header <em>are</em> represented by a <tt>?</tt> instead of
a <tt>#</tt> symbol </td>
</tr>
</tbody></table>
</div>


<h3><a name="SQLComponent-ConfigurationinCamel1.5.0orlower"></a>Configuration
in Camel 1.5.0 or lower</h3>

<p>The SQL component must be configured before it can be used. In Spring, you can configure
it as follows:</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">"sql"</span>
class=<span class="code-quote">"org.apache.camel.component.sql.SqlComponent"</span>&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"dataSource"</span>
ref=<span class="code-quote">"myDS"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>

<span class="code-tag">&lt;bean id=<span class="code-quote">"myDS"</span>
class=<span class="code-quote">"org.springframework.jdbc.datasource.DriverManagerDataSource"</span>&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"driverClassName"</span>
value=<span class="code-quote">"com.mysql.jdbc.Driver"</span> /&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"url"</span>
value=<span class="code-quote">"jdbc:mysql://localhost:3306/ds"</span> /&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"username"</span>
value=<span class="code-quote">"username"</span> /&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"password"</span>
value=<span class="code-quote">"password"</span> /&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div>

<h3><a name="SQLComponent-ConfigurationinCamel1.5.1orhigher"></a>Configuration
in Camel 1.5.1 or higher</h3>
<p>You can now set a reference to a <tt>DataSource</tt> in the URI directly:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
sql:select * from table where id=# order by name?dataSourceRef=myDS
</pre>
</div></div>


<h3><a name="SQLComponent-Sample"></a>Sample</h3>
<p>In the sample below we execute a query and retrieve the result as a <tt>List</tt>
of rows, where each row is a <tt>Map&lt;String, Object</tt> and the key is
the column name. </p>

<p>First, we set up a table to use for our sample. As this is based on an unit test,
we do it java code:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-comment">// <span class="code-keyword">this</span>
is the database we create with some initial data <span class="code-keyword">for</span>
our unit test
</span>jdbcTemplate.execute(<span class="code-quote">"create table projects (id
integer primary key,"</span>
                     + <span class="code-quote">"project varchar(10), license varchar(5))"</span>);
jdbcTemplate.execute(<span class="code-quote">"insert into projects values (1, 'Camel',
'ASF')"</span>);
jdbcTemplate.execute(<span class="code-quote">"insert into projects values (2, 'AMQ',
'ASF')"</span>);
jdbcTemplate.execute(<span class="code-quote">"insert into projects values (3, 'Linux',
'XXX')"</span>);
</pre>
</div></div>

<p>Then we configure our route and our <tt>sql</tt> component. Notice that
we use a <tt>direct</tt> endpoint in front of the <tt>sql</tt> endpoint.
This allows us to send an exchange to the <tt>direct</tt> endpoint with the URI,
<tt>direct:simple</tt>, which is much easier for the client to use than the long
<tt>sql:</tt> URI. Note that the <tt>DataSource</tt> is looked up
up in the registry, so we can use standard Spring XML to configure our <tt>DataSource</tt>.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"direct:simple"</span>)
    .to(<span class="code-quote">"sql:select * from projects where license = # order
by id?dataSourceRef=jdbc/myDataSource"</span>)
    .to(<span class="code-quote">"mock:result"</span>);
</pre>
</div></div>

<p>And then we fire the message into the <tt>direct</tt> endpoint that will
route it to our <tt>sql</tt> component that queries the database.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">MockEndpoint mock = getMockEndpoint(<span class="code-quote">"mock:result"</span>);
mock.expectedMessageCount(1);

<span class="code-comment">// send the query to direct that will route it to the sql
where we will execute the query
</span><span class="code-comment">// and bind the parameters with the data from
the body. The body only contains one value
</span><span class="code-comment">// in <span class="code-keyword">this</span>
<span class="code-keyword">case</span> (XXX) but <span class="code-keyword">if</span>
we should use multi values then the body will be iterated
</span><span class="code-comment">// so we could supply a List&lt;<span
class="code-object">String</span>&gt; instead containing each binding value.
</span>template.sendBody(<span class="code-quote">"direct:simple"</span>,
<span class="code-quote">"XXX"</span>);

mock.assertIsSatisfied();

<span class="code-comment">// the result is a List
</span>List received = assertIsInstanceOf(List.class, mock.getReceivedExchanges().get(0).getIn().getBody());

<span class="code-comment">// and each row in the list is a Map
</span>Map row = assertIsInstanceOf(Map.class, received.get(0));

<span class="code-comment">// and we should be able the get the project from the map
that should be Linux
</span>assertEquals(<span class="code-quote">"Linux"</span>, row.get(<span
class="code-quote">"PROJECT"</span>));
</pre>
</div></div>

<p>We could configure the <tt>DataSource</tt> in Spring XML as follows:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
 <span class="code-tag">&lt;jee:jndi-lookup id=<span class="code-quote">"myDS"</span>
jndi-name=<span class="code-quote">"jdbc/myDataSource"</span>/&gt;</span>

</pre>
</div></div>

<h3><a name="SQLComponent-UsingtheJDBCbasedidempotentrepository"></a>Using
the JDBC based idempotent repository</h3>
<p><b>Available as of Camel 2.7</b>: In this section we will use the JDBC
based idempotent repository. </p>

<p>First we need to setup a <tt>javax.sql.DataSource</tt> in the spring
XML file:</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">"dataSource"</span>
class=<span class="code-quote">"org.springframework.jdbc.datasource.SingleConnectionDataSource"</span>&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"driverClassName"</span>
value=<span class="code-quote">"org.hsqldb.jdbcDriver"</span>/&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"url"</span>
value=<span class="code-quote">"jdbc:hsqldb:mem:camel_jdbc"</span>/&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"username"</span>
value=<span class="code-quote">"sa"</span>/&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"password"</span>
value=""/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div>

<p>And finally we can create our JDBC idempotent repository in the spring XML file as
well:</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">"messageIdRepository"</span>
class=<span class="code-quote">"org.apache.camel.processor.idempotent.jdbc.JdbcMessageIdRepository"</span>&gt;</span>
	<span class="code-tag">&lt;constructor-arg ref=<span class="code-quote">"dataSource"</span>
/&gt;</span>
	<span class="code-tag">&lt;constructor-arg value=<span class="code-quote">"myProcessorName"</span>
/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>

<span class="code-tag">&lt;camel:camelContext&gt;</span>
	<span class="code-tag">&lt;camel:errorHandler id=<span class="code-quote">"deadLetterChannel"</span>
type=<span class="code-quote">"DeadLetterChannel"</span> deadLetterUri=<span
class="code-quote">"mock:error"</span>&gt;</span>
		<span class="code-tag">&lt;camel:redeliveryPolicy maximumRedeliveries=<span
class="code-quote">"0"</span> maximumRedeliveryDelay=<span class="code-quote">"0"</span>
logStackTrace=<span class="code-quote">"false"</span> /&gt;</span>
	<span class="code-tag">&lt;/camel:errorHandler&gt;</span>
	
	<span class="code-tag">&lt;camel:route id=<span class="code-quote">"JdbcMessageIdRepositoryTest"</span>
errorHandlerRef=<span class="code-quote">"deadLetterChannel"</span>&gt;</span>
		<span class="code-tag">&lt;camel:from uri=<span class="code-quote">"direct:start"</span>
/&gt;</span>
		<span class="code-tag">&lt;camel:idempotentConsumer messageIdRepositoryRef=<span
class="code-quote">"messageIdRepository"</span>&gt;</span>
			<span class="code-tag">&lt;camel:header&gt;</span>messageId<span
class="code-tag">&lt;/camel:header&gt;</span>
			<span class="code-tag">&lt;camel:to uri=<span class="code-quote">"mock:result"</span>
/&gt;</span>
		<span class="code-tag">&lt;/camel:idempotentConsumer&gt;</span>
	<span class="code-tag">&lt;/camel:route&gt;</span>
<span class="code-tag">&lt;/camel:camelContext&gt;</span>
</pre>
</div></div>

<h3><a name="SQLComponent-UsingtheJDBCbasedaggregationrepository"></a>Using
the JDBC based aggregation repository</h3>
<p><b>Available as of Camel 2.6</b></p>
<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>Using JdbcAggregationRepository
in Camel 2.6</b><br />In Camel 2.6, the JdbcAggregationRepository is provided
in the <tt>camel-jdbc-aggregator</tt> component. From Camel 2.7 onwards, the <tt>JdbcAggregationRepository</tt>
is provided in the <tt>camel-sql</tt> component.</td></tr></table></div>
<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>.<br/>
The <tt>JdbcAggregationRepository</tt> allows together with Camel to provide persistent
support for the <a href="/confluence/display/CAMEL/Aggregator2" title="Aggregator2">Aggregator</a>.</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>



<h4><a name="SQLComponent-Whatispreservedwhenpersisting"></a>What is preserved
when persisting</h4>
<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>

<h4><a name="SQLComponent-Recovery"></a>Recovery</h4>
<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-sql, for example <a href="https://svn.apache.org/repos/asf/camel/trunk/components/camel-sql/src/test/java/org/apache/camel/processor/aggregate/jdbc/JdbcAggregateRecoverDeadLetterChannelTest.java"
class="external-link" rel="nofollow">this test</a>.</p>

<h4><a name="SQLComponent-Database"></a>Database</h4>

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

<h4><a name="SQLComponent-Codec%28Serialization%29"></a>Codec (Serialization)</h4>
<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>

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

<h4><a name="SQLComponent-Service%28Start%2FStop%29"></a>Service (Start/Stop)</h4>
<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>

<h4><a name="SQLComponent-Aggregatorconfiguration"></a>Aggregator configuration</h4>
<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.processor.aggregate.jdbc.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="SQLComponent-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/JDBC" title="JDBC">JDBC</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/SQL+Component">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=82932&revisedVersion=23&originalVersion=22">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/SQL+Component?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message