问题
I'm new to Apache Camel and I've stared to write an application that grabs data from one database and inserts it to another database. I'm using the sql component and am trying to figure out how to grab the CamelSqlUpdateCount, CamelSqlRowCount and CamelSqlQuery from the message header using spring dsl.
I know that I can get attributes in the data using this....
<log message="Processing product ${body[product_id]}"/>
But when I try to grab data from my header after my insert like this...
<from uri="sourceSql:{{sql.selectProduct}}"/>
<log message="SELECT,${header.CamelSqlQuery},${header.CamelSqlRowCount}" loggingLevel="INFO" logName="db_log"/>
<to uri="targetSql:{{sql.insertProductOrig}}"/>
<log message="INSERT,CONV_ORIG,${header.CamelSqlQuery},${header.CamelSqlUpdateCount}" loggingLevel="INFO" logName="db_log"/>
it comes back empty. After getting advice on turning on a trace in my route to see what is coming back in the message headers I can see that those header attributes are not there. I am connecting to sql server to select and mysql to insert. does anyone know what might be wrong?
I've attached some sample output from the trace...
2013-09-04,21:11:55.615,MacBook-local >>> (processProduct-route) from(sourceSql://SELECT%20TOP%205%20product_id%20FROM%20product_) --> log[SELECT,${header.CamelSqlQuery},${header.CamelSqlRowCount}] <<< Pattern:InOnly, Headers:{breadcrumbId=MacBook-local}, BodyType:org.springframework.util.LinkedCaseInsensitiveMap, Body:{product_id=273}
2013-09-04,21:11:55.625,MacBook-local >>> (processProduct-route) log[SELECT,${header.CamelSqlQuery},${header.CamelSqlRowCount}] --> multicast <<< Pattern:InOnly, Headers:{breadcrumbId=MacBook-local}, BodyType:org.springframework.util.LinkedCaseInsensitiveMap, Body:{product_id=273}
2013-09-04,21:11:55.644,MacBook-local >>> (processProduct-route) from(sourceSql://SELECT%20TOP%205%20product_id%20FROM%20product_) --> targetSql://insert%20into%20conv_stg_product_%20(product_id)%20values%20(:%23product_id) <<< Pattern:InOnly, Headers:{breadcrumbId=MacBook-local}, BodyType:org.springframework.util.LinkedCaseInsensitiveMap, Body:{product_id=273}
2013-09-04,21:11:55.646,MacBook-local >>> (processProduct-route) from(sourceSql://SELECT%20TOP%205%20product_id%20FROM%20product_) --> log[INSERT,CONV_STG,${header.CamelSqlQuery},${header.CamelSqlUpdateCount}] <<< Pattern:InOnly, Headers:{breadcrumbId=MacBook}, BodyType:org.springframework.util.LinkedCaseInsensitiveMap, Body:{product_id=273}
2013-09-04,21:11:55.642,MacBook >>> (processProduct-route) from(sourceSql://SELECT%20TOP%205%20product_id%20FROM%20product_) --> log[INSERT,CONV_ORIG,${header.CamelSqlQuery},${header.CamelSqlUpdateCount}] <<< Pattern:InOnly, Headers:{breadcrumbId=MacBook}, BodyType:org.springframework.util.LinkedCaseInsensitiveMap, Body:{product_id=273}
Here are what the sql statements look like...
sql.selectProduct=SELECT TOP 5 product_id FROM product_
sql.insertProductOrig=insert into conv_orig_product_ (product_id) values (:#product_id)
Here is an extract from my POM to show what jdbc drivers I am using...
<!-- SQL Server database driver -->
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.3.0</version>
</dependency>
<!-- MySQL database driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
thanks
回答1:
This looks like an end user issue.
2013-09-04,21:11:55.625,MacBook-local >>> (processProduct-route) log[SELECT,${header.CamelSqlQuery},${header.CamelSqlRowCount}] --> multicast <<< Pattern:InOnly, Headers:{breadcrumbId=MacBook-local}, BodyType:org.springframework.util.LinkedCaseInsensitiveMap, Body:{product_id=273}
The details from the trace logging indicates a multicast EIP is in use (see above). And when you do that then the multicast copied the incoming message (original message), and each receiver gets a copy of that "original" message. So that would explain why these headers is not there, as the original copy does not have the SQL update headers.
So remove the multicast and just use a straight pipeline.
You can read about multicast and pipeline here
- http://camel.apache.org/multicast.html
- http://camel.apache.org/pipes-and-filters.html
来源:https://stackoverflow.com/questions/18572129/apache-camel-how-to-get-header-attributes-from-sql-component