I am new to dropwizard, and am using GuiceBundle and MongoBundle in my application. The MongoClient is wrapped in a dropwizard Managed object and is tied to the lifecycle of the application.
public class SalApplication extends Application<SomeConf> {
...
private GuiceBundle<SomeConf> guiceBundle;
private MongoBundle<SomeConf> mongoBundle;
...
@Override
public void initialize(Bootstrap<SomeConf> bootstrap) {
// build bundles and add to bootstrap
...
}
@Override
public void run(SomeConf someConf, Environment env) throws Exception{
...
MongoClient client = mongoBundle.getClient();
MongoClientManager mongoDB = new MongoClientManager(client);
env.lifecycle().manage(mongoDB); //MongoClientManager implements Managed
}
My hiccup is, how do I get hold of the MongoClient object. The object is supposed to be injected into my DAOs. But how can I access the MongoClient object from inside guice Module.
If I construct another MongoClient object inside guice module, then what is the point of the Managed Object. I'm really confused.
I found a simpler way to achieve what I needed.
I was previously using com.meltmedia.dropwizard.dropwizard-mongo, whose MongoBundle constructs the MongoClient, which had to be passed to my ManagedObject.
I stopped using it. Instead I'm constructing the MongoClient object myself using mongo-java-driver inside guice module, and is injected into the constructor of my managed object.