Niels
Niels

Reputation: 3

Apache camel set mongodb collection dynamically

I'm trying to create a route where the endpoint depents on the incoming message. The aim is to write into a mongodb in different databases and collection.

I'm looking for an easy way to get the information from the message header and write it in to the <to uri=""/>

<route> <from uri="jms:topic:BUS_IN" /> <to uri="mongodb:myDb?database=${header.someValue}&amp;collection=storyTeaser&amp;operation=save" /> </route>

Thanks a lot

Upvotes: 0

Views: 784

Answers (2)

Urn
Urn

Reputation: 43

Using Apache Camels toD function https://camel.apache.org/message-endpoint.html will allow you to dynamically set the URI as messages are passed through. The URI allows for simple language https://camel.apache.org/simple.html where we can for instance use the filename to generate a collection.

Heres an example Route:

from(input).routeId("SampleRoute")
.toD("mongodb3://mongoBean?database=myDB&collection=${file:onlyname.noext}&" +
    "operation=insert&createCollection=true")

Upvotes: 1

Christian
Christian

Reputation: 26

You could add a second route that sets the header variables:

<route>
        <from uri="jms:topic:BUS_IN" />
        <camel:setHeader headerName="CamelMongoDbDatabase">
            <camel:simple>testmydb</camel:simple>
        </camel:setHeader>
        <camel:setHeader headerName="CamelMongoDbCollection">
            <camel:simple>mycollection</camel:simple>
        </camel:setHeader>
        <to uri="jms:queue:mongodb.out"/>
    </route>

And then add the parameter "dynamicity" in the uri of your first route:

<route>
        <from uri="jms:queue:mongodb.out" />
        <to uri="mongodb:myDb?database=new_test&amp;collection=old&amp;dynamicity=true&amp;operation=save"/>
    </route>

Upvotes: 1

Related Questions