{"id":241,"date":"2013-02-19T17:11:50","date_gmt":"2013-02-19T17:11:50","guid":{"rendered":"http:\/\/www.andreagirardi.it\/blog\/?p=241"},"modified":"2014-07-01T16:13:38","modified_gmt":"2014-07-01T16:13:38","slug":"route-a-message-to-mongodb","status":"publish","type":"post","link":"https:\/\/www.andreagirardi.it\/blog\/route-a-message-to-mongodb\/","title":{"rendered":"Route a message to MongoDB"},"content":{"rendered":"<p>The requirement is very simple. Route an XML message from rabbitMQ to MongoDB. MongoDB  BSON as the data storage and network transfer format for &#8220;documents&#8221;. BSON is a binary-encoded serialization of JSON-like documents. So, the source message is in a XML format, after getting it from rabbitMQ is necessary to translate into a JSON format compatible with MongoDB.<\/p>\n<p>To translate a message from XML to JSON is possible to use Marshalling function available in Camel extension XmlJson. The pom.xml file needs this new entry:<\/p>\n<pre lang=\"xml\">\r\n<dependency>\r\n\t<groupId>org.apache.camel<\/groupId>\r\n\t<artifactId>camel-xmljson<\/artifactId>\r\n\t<version>${org.camel.version}<\/version>\r\n<\/dependency>\r\n<\/pre>\n<p>Of course, to send communicate with MongoDB another entry has to be added to pom.xml:<\/p>\n<pre lang=\"xml\">\r\n<dependency>\r\n\t<groupId>org.apache.camel<\/groupId>\r\n\t<artifactId>camel-mongodb<\/artifactId>\r\n\t<version>${org.camel.version}<\/version>\r\n<\/dependency>\r\n<\/pre>\n<p>Now, is possible to change the route on camel ApplicationContext file:<\/p>\n<pre lang=\"xml\">  \r\n<!-- Camel route -->    \r\n<camelContext xmlns=\"http:\/\/camel.apache.org\/schema\/spring\">\r\n\t<dataFormats>\r\n\t\t<xmljson id=\"xmljson\"\/>\r\n\t<\/dataFormats>\r\n\t<route> \r\n\t\t<from uri=\"spring-amqp:TPDirect:TPQueue:TPRouting?type=direct&amp;autodelete=true&amp;durable=true\" \/>\r\n\t\t<marshal ref=\"xmljson\"\/>\r\n\t\t<log message=\"From XML to Json: DONE!\" \/>\r\n\t\t<convertBodyTo type=\"java.lang.String\"\/>\r\n\t\t<to uri=\"mongodb:myDb?database=flights&amp;collection=tickets&amp;operation=save\" \/>\r\n\t<\/route>                                 \r\n<\/camelContext>\r\n\r\n\r\n<!-- Mongo DB -->\r\n<bean id=\"myDb\" class=\"com.mongodb.Mongo\">\r\n\t<constructor-arg index=\"0\" value=\"localhost\"\/>\r\n<\/bean>\r\n<\/pre>\n<p>Bean myDB contains the information to reach MongoDB. It&#8217;s also possible to define it using the full url:<\/p>\n<pre lang=\"xml\">\r\n<bean id=\"myDb\" class=\"com.mongodb.Mongo\"> \r\n    <constructor-arg index=\"0\"> \r\n        <bean class=\"com.mongodb.MongoURI\"> \r\n            <constructor-arg index=\"0\" value=\"mongodb:\/\/username:password@host:port\/db\" \/> \r\n        <\/bean> \r\n    <\/constructor-arg> \r\n<\/bean> \r\n<\/pre>\n<p>To avoid this exception: <\/p>\n<pre lang=\"bash\">\r\nCaused by: No type converter available to convert from type: byte[] to the required type: org.apache.camel.component.mongodb.converters.MongoDbBasicConverters with value\r\n<\/pre>\n<p>The JSON translated message has to be converted into String. <\/p>\n<p>That&#8217;s all!<\/p>\n<p>[AdSense-A]<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-linkedin\"><a rel=\"nofollow\" data-shared=\"sharing-linkedin-241\" class=\"share-linkedin sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/route-a-message-to-mongodb\/?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\"><span>LinkedIn<\/span><\/a><\/li><li class=\"share-reddit\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-reddit sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/route-a-message-to-mongodb\/?share=reddit\" target=\"_blank\" title=\"Click to share on Reddit\"><span>Reddit<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow\" data-shared=\"sharing-twitter-241\" class=\"share-twitter sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/route-a-message-to-mongodb\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\"><span>Twitter<\/span><\/a><\/li><li class=\"share-facebook\"><a rel=\"nofollow\" data-shared=\"sharing-facebook-241\" class=\"share-facebook sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/route-a-message-to-mongodb\/?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\"><span>Facebook<\/span><\/a><\/li><li class=\"share-skype\"><a rel=\"nofollow\" data-shared=\"sharing-skype-241\" class=\"share-skype sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/route-a-message-to-mongodb\/?share=skype\" target=\"_blank\" title=\"Share on Skype\"><span>Skype<\/span><\/a><\/li><li class=\"share-pocket\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-pocket sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/route-a-message-to-mongodb\/?share=pocket\" target=\"_blank\" title=\"Click to share on Pocket\"><span>Pocket<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>The requirement is very simple. Route an XML message from rabbitMQ to MongoDB. MongoDB BSON as the data storage and network transfer format for &#8220;documents&#8221;. BSON is a binary-encoded serialization of JSON-like documents. So, the source message is in a XML format, after getting it from rabbitMQ is necessary to translate into a JSON format [&hellip;]<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-linkedin\"><a rel=\"nofollow\" data-shared=\"sharing-linkedin-241\" class=\"share-linkedin sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/route-a-message-to-mongodb\/?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\"><span>LinkedIn<\/span><\/a><\/li><li class=\"share-reddit\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-reddit sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/route-a-message-to-mongodb\/?share=reddit\" target=\"_blank\" title=\"Click to share on Reddit\"><span>Reddit<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow\" data-shared=\"sharing-twitter-241\" class=\"share-twitter sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/route-a-message-to-mongodb\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\"><span>Twitter<\/span><\/a><\/li><li class=\"share-facebook\"><a rel=\"nofollow\" data-shared=\"sharing-facebook-241\" class=\"share-facebook sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/route-a-message-to-mongodb\/?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\"><span>Facebook<\/span><\/a><\/li><li class=\"share-skype\"><a rel=\"nofollow\" data-shared=\"sharing-skype-241\" class=\"share-skype sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/route-a-message-to-mongodb\/?share=skype\" target=\"_blank\" title=\"Share on Skype\"><span>Skype<\/span><\/a><\/li><li class=\"share-pocket\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-pocket sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/route-a-message-to-mongodb\/?share=pocket\" target=\"_blank\" title=\"Click to share on Pocket\"><span>Pocket<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"footnotes":""},"categories":[14],"tags":[30,31,32,13],"_links":{"self":[{"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/posts\/241"}],"collection":[{"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/comments?post=241"}],"version-history":[{"count":8,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/posts\/241\/revisions"}],"predecessor-version":[{"id":539,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/posts\/241\/revisions\/539"}],"wp:attachment":[{"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/media?parent=241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/categories?post=241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/tags?post=241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}