I’ve just started using Apache Camel and I would like to try to synchronize two tables. Both tables have two columns, “id” and “name”. The scenario would query the first table, check for existence of each record in the second table, and insert each record that doesn’t exist. Here is my attempt:
<camel:camelContext id="test">
<camel:route>
<camel:from uri="timer://kickoff?period=5s"/>
<camel:to uri="sql:select id, name from table1?dataSourceRef=dataSource"/>
<camel:split>
<camel:simple>body</camel:simple>
<camel:to uri="sql:select * from table2 where id = #?dataSourceRef=dataSource" />
<camel:choice>
<camel:when>
<camel:simple>${header.CamelSqlRowCount} == 0</camel:simple>
<camel:to uri="sql:insert into table2 (id, name) values (#, #)?dataSourceRef=dataSource" />
</camel:when>
</camel:choice>
<camel:to uri="mock:result" />
</camel:split>
</camel:route>
</camel:camelContext>
The problem here is that by the time I reach insert into table2, the original message (the one containing the data) is lost, as I had to make another query in the meantime (where I check for data in the second table). Is my approach good and how can I retrieve the message that contains the data?
When you call the 2nd SQL to check, then you can use the content enricher EIP to “merge” the result of this call with the original message. In your case, you only need to know if there was a row or not, and store that in a header.
The content enricher eip is documented here: http://camel.apache.org/content-enricher.html, see for example the enrich.
The caveat is that you would need to use a java code and implement an AggregationStrategy that implements the “merge” logic.
You can also do as Ben suggests to store the 1st data in a header (or property on the exchange), which you can access later.
Though looking at this from the EIP perspective, then its the content enricher EIP that best covers this use-case.