Discussion:
mongoDB uri
Andrei Stoica
2014-08-22 13:41:07 UTC
Permalink
Hello,

I have a question regarding the mongoDB connection.

In the CamelContext.xml I have the following declaration:

<bean id="myDb" class="com.mongodb.Mongo">
<constructor-arg index="0">
<bean class="com.mongodb.MongoURI">
<constructor-arg index="0"
value="mongodb://user:***@localhost:1111/db"
/>
</bean>
</constructor-arg>
</bean>


When I run using this configuration all works ok. My problem is that I will
not know the mongo uri until the runtime. Is there a way I can change the
mongo DB uri at runtime?

Best regards!
Matt Sicker
2014-08-22 15:50:57 UTC
Permalink
You can use a property. Check this out:
http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
Post by Andrei Stoica
Hello,
I have a question regarding the mongoDB connection.
<bean id="myDb" class="com.mongodb.Mongo">
<constructor-arg index="0">
<bean class="com.mongodb.MongoURI">
<constructor-arg index="0"
/>
</bean>
</constructor-arg>
</bean>
When I run using this configuration all works ok. My problem is that I will
not know the mongo uri until the runtime. Is there a way I can change the
mongo DB uri at runtime?
Best regards!
--
Matt Sicker <boards-***@public.gmane.org>
Andrei Stoica
2014-08-25 08:36:53 UTC
Permalink
Hello,

My problem is that the connection URI might change and I need a way to edit
it at runtime.

When the application will be started, the user will be prompted with a
screen to input the connection details. After he inputs them, I need a way
to route messages to that URI.

Any idea how I can do that?
Raul Kripalani
2014-08-25 09:46:06 UTC
Permalink
Hello Andrei,

This is an interesting use case.

Currently there is no easy way to support your scenario because the MongoDB
component requires a com.mongodb.Mongo bean in the registry, e.g.

mongodb:connectionBean?...

In your context:

<bean name="connectionBean" class="com.mongodb.Mongo">
...
</bean>

However, many ideas come to mind. You can use a Dynamic Router / Recipient
List EIP to dynamically change the endpoint URI, along with Camel's Java
API to dynamically register beans in the registry [3].

So when you receive a message, a Processor or a bean could bind a new Mongo
object in your registry under a random UUID. You set the resulting UUID in
an Exchange property. Then you use that UUID to populate the endpoint URI,
e.g. mongodb:<uuid>.

Once the operation is finished, you can unbind the bean from the registry
(Very important to do so! Otherwise you could easily end up with a memory
leak).

[1] https://camel.apache.org/dynamic-router.html
[2] https://camel.apache.org/recipient-list.html
[3]
http://camel.465427.n5.nabble.com/Programatically-adding-beans-to-a-registry-in-a-RouteBuilder-td5729358.html

Regards,

*Raúl Kripalani*
Apache Camel PMC Member & Committer | Enterprise Architect, Open Source
Integration specialist
http://about.me/raulkripalani | http://www.linkedin.com/in/raulkripalani
http://blog.raulkr.net | twitter: @raulvk

On Mon, Aug 25, 2014 at 9:36 AM, Andrei Stoica <
Post by Andrei Stoica
Hello,
My problem is that the connection URI might change and I need a way to edit
it at runtime.
When the application will be started, the user will be prompted with a
screen to input the connection details. After he inputs them, I need a way
to route messages to that URI.
Any idea how I can do that?
--
http://camel.465427.n5.nabble.com/mongoDB-uri-tp5755565p5755625.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Andrei Stoica
2014-08-25 13:56:26 UTC
Permalink
How can I add a bean to the registry? In the link mentioned by you there is
no conclusion. I've tried the solution from the last comment but it doesn't
work.

Also where should I add the the bean to the registry. I tried adding it in a
process prior to the dynamicRouter method and I get this error:

Failed to resolve endpoint:
mongodb://myDb?collection=none&database=none&dynamicity=true&operation=findOneByQuery
due to: No bean could be found in the registry for: myDb of type:
com.mongodb.Mongo
Andrei Stoica
2014-08-26 11:44:08 UTC
Permalink
I tried adding the bean in the existing camel context like this:

from("jms:...")
.process(new Processor() {

@Override
public void process(Exchange exchange) throws Exception {
// add bean to context using getContext() method
}
)
.dynamicRouter(method(MongoConnection.class)) // in the MongoConnection
class I am using the bean added above

When I run the code I get a warning that the bean is not found in the
Registry.

Am I doing it right? Should I add the bean to the registry somewhere else?
Raul Kripalani
2014-08-26 12:29:14 UTC
Permalink
Can you post the code showing...?

a) how you add the bean.
b) how you construct the endpoint URI from within the "MongoConnection"
class' dynamic router method.

Regards,

*Raúl Kripalani*
Apache Camel PMC Member & Committer | Enterprise Architect, Open Source
Integration specialist
http://about.me/raulkripalani | http://www.linkedin.com/in/raulkripalani
http://blog.raulkr.net | twitter: @raulvk


On Tue, Aug 26, 2014 at 12:44 PM, Andrei Stoica <
Post by Andrei Stoica
from("jms:...")
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
// add bean to context using getContext() method
}
)
.dynamicRouter(method(MongoConnection.class)) // in the MongoConnection
class I am using the bean added above
When I run the code I get a warning that the bean is not found in the
Registry.
Am I doing it right? Should I add the bean to the registry somewhere else?
--
http://camel.465427.n5.nabble.com/mongoDB-uri-tp5755565p5755694.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Andrei Stoica
2014-08-26 13:03:53 UTC
Permalink
I managed to get it work this way:

In the camel.xml I declared my bean:

<bean id="myDb" class="com.mongodb.Mongo" />

my route:

from("jms:mongoQueue")
.bean(MongoConnection.class, "setConnectionBean")
.process(new Processor() {

@Override
public void process(Exchange exchange) throws Exception {

exchange.getIn().setHeader(MongoDbConstants.DATABASE, "dbname");
exchange.getIn().setHeader(MongoDbConstants.COLLECTION, "collection");
exchange.getIn().setHeader(MongoDbConstants.OPERATION_HEADER,
"operation");
exchange.getIn().setBody(query);
}
})
.dynamicRouter(method(MongoConnection.class, "getConnection"))

and the MongoConnection.class:

@Autowired
private Mongo mongo;

public String getConnection(String body, @Header(Exchange.SLIP_ENDPOINT)
String previous) throws Exception{

if(previous == null)
return "mongodb://myDb?database=none&collection=none&dynamicity=true";
else
return null;
}

public void setConnectionBean(MongoStep step) throws UnknownHostException{

MongoClientURI uri = new MongoClientURI(getUri());
mongo = new MongoClient(uri);
}

Now I can specify the uri in the runtime.
Raul Kripalani
2014-08-26 13:46:03 UTC
Permalink
Did you just need to change the MongoDB Database and the MongoDB
Collections?

I thought you also wanted to change the hostname and the port at runtime of
the Mongo server...

If that's not the case, you can even simplify your solution a lot! After
all, it seems that your dynamic router is always returning the same URI...

Regards,

*Raúl Kripalani*
Apache Camel PMC Member & Committer | Enterprise Architect, Open Source
Integration specialist
http://about.me/raulkripalani | http://www.linkedin.com/in/raulkripalani
http://blog.raulkr.net | twitter: @raulvk

On Tue, Aug 26, 2014 at 2:03 PM, Andrei Stoica <
Post by Andrei Stoica
<bean id="myDb" class="com.mongodb.Mongo" />
from("jms:mongoQueue")
.bean(MongoConnection.class, "setConnectionBean")
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setHeader(MongoDbConstants.DATABASE, "dbname");
exchange.getIn().setHeader(MongoDbConstants.COLLECTION, "collection");
exchange.getIn().setHeader(MongoDbConstants.OPERATION_HEADER,
"operation");
exchange.getIn().setBody(query);
}
})
.dynamicRouter(method(MongoConnection.class,
"getConnection"))
@Autowired
private Mongo mongo;
public String getConnection(String body,
@Header(Exchange.SLIP_ENDPOINT)
String previous) throws Exception{
if(previous == null)
return
"mongodb://myDb?database=none&collection=none&dynamicity=true";
else
return null;
}
public void setConnectionBean(MongoStep step) throws
UnknownHostException{
MongoClientURI uri = new MongoClientURI(getUri());
mongo = new MongoClient(uri);
}
Now I can specify the uri in the runtime.
--
http://camel.465427.n5.nabble.com/mongoDB-uri-tp5755565p5755704.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Andrei Stoica
2014-08-26 14:02:39 UTC
Permalink
On Tue, Aug 26, 2014 at 2:03 PM, Andrei Stoica <
Post by Andrei Stoica
public void setConnectionBean(MongoStep step) throws
UnknownHostException{
MongoClientURI uri = new MongoClientURI(getUri());
mongo = new MongoClient(uri);
}
--
http://camel.465427.n5.nabble.com/mongoDB-uri-tp5755565p5755704.html
Sent from the Camel - Users mailing list archive at Nabble.com.
the getUri() method will return a custom URI that is passed to the
MongoClient. I construct my URI based on other objects and I didn't consider
it necessary to provide the implementation of the getUri().

Basically when I start my app, an object is created with some values
provided by the UI and then I use that object to create my URI.

Loading...