Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
<jee:jndi-lookup id="myDS" jndi-name="jdbc/myDataSource"/> 

]]>


Using named parameters

Available as of Camel 2.11

In the given route below, we want to get all the projects from the projects table. Notice the SQL query has 2 named parameters, :#lic and :#min.
Camel will then lookup for these parameters from the message body or message headers. Notice in the example above we set two headers with constant value
for the named parameters:java

Code Block
languagejava
from("direct:projects")
   .setHeader("lic", constant("ASF"))
   .setHeader("min", constant(123))
   .to("sql:select * from projects where license = :#lic and id > :#min order by id")

:#min order by id") ]]>


Though if the message body is a java.util.Map then the named parameters will be taken from the body.

Code Block
languagejava
from("direct:projects") 
   .to("sql:select * from projects where license = :#lic and id > :#min order by id")

...

id")

...


Using expression parameters

...

Code Block
languagejava
from("direct:projects")
   .setBody(constant("ASF"))
   .setProperty("min", constant(123)) 
   .to("sql:select * from projects where license = :#${body} and id > :#${property.min} order by id")

...

id")

...


Using IN queries with dynamic values

...

Second we need to setup a javax.sql.DataSource in the spring XML file:

 ]]>

Code Block
languagexml
<jdbc:embedded-database id="dataSource" type="DERBY" /> 


And finally we can create our JDBC idempotent repository in the spring XML file as well: ]]>

Code Block
languagexml
 <bean id="messageIdRepository" class="org.apache.camel.processor.idempotent.jdbc.JdbcMessageIdRepository">  
   <constructor-arg ref="dataSource" /> 
   <constructor-arg value="myProcessorName" />
 </bean> 

...

A customized org.apache.camel.processor.idempotent.jdbc.JdbcMessageIdRepository could look like:]]>

Code Block
languagexml
<bean id="messageIdRepository" class="org.apache.camel.processor.idempotent.jdbc.JdbcMessageIdRepository">
   <constructor-arg ref="dataSource" />
   <constructor-arg value="myProcessorName" />
   <property name="tableExistsString" value="SELECT 1 FROM CUSTOMIZED_MESSAGE_REPOSITORY WHERE 1 = 0" />
   <property name="createString" value="CREATE TABLE CUSTOMIZED_MESSAGE_REPOSITORY (processorName VARCHAR(255), messageId VARCHAR(100), createdAt TIMESTAMP)" />    
   <property name="queryString" value="SELECT COUNT(*) FROM CUSTOMIZED_MESSAGE_REPOSITORY WHERE processorName = ? AND messageId = ?" />
   <property name="insertString" value="INSERT INTO CUSTOMIZED_MESSAGE_REPOSITORY (processorName, messageId, createdAt) VALUES (?, ?, ?)" />
   <property name="deleteString" value="DELETE FROM CUSTOMIZED_MESSAGE_REPOSITORY WHERE processorName = ? AND messageId = ?" />
</bean>

...