I'm running Dropwizard together with Elasticsearch and MongoDB. Both 
Elasticsearch and MongoDB are Managed.

This means I configure them in the constructor, while they are started on 
start() and stopped on stop().

public MongoManaged(AbilityConfiguration configuration) {
    replicaSet = new ArrayList<>();

    credentials = MongoCredential.createCredential(configuration.mongo.user, 
configuration.mongo.db, configuration.mongo.password.toCharArray());

    configuration.mongo.servers.forEach((AbilityConfiguration.Server server) -> 
{
        replicaSet.add(new ServerAddress(server.getHost(), server.getPort()));
    });

    databaseName = configuration.mongo.db;
}

public void start() {
    System.out.println("Starting mongo...");

    MongoClientOptions mongoClientOptions = 
MongoClientOptions.builder().codecRegistry(generatePOJOCodecRegistry()).connectionsPerHost(100).build();

    mongoClient = new MongoClient(replicaSet, credentials, mongoClientOptions);

    database = mongoClient.getDatabase(databaseName);
}

public MongoDatabase getDatabase() {
    return database;
}

public void stop() {
    System.out.println("Stopping mongo...");
    mongoClient.close();
}


In Dropwizards run() routine I add the managed systems to the environment 
as well as the health checks, followed by the REST routines.

MongoManaged mongo = new MongoManaged(configuration);
environment.lifecycle().manage(mongo);
environment.lifecycle().addLifeCycleListener(lifeCycleListener);
environment.healthChecks().register("MongoHealthCheck", new 
MongoHealthCheck(mongo.getDatabase()));

environment.jersey().register(new UserResource(mongo.getDatabase()));

...


Now this fails, as when registering e.g. "UserResource" to the environment, 
mongo was not started yet and mongo.getDatabase() returns null at this 
point.

I try to work around this issue by registering a LifeCycle listener and 
registering the "UserResource" when MongoDB started but then I got an error:

"java.lang.IllegalStateException: The resource configuration is not 
modifiable in this context."

environment.lifecycle().addLifeCycleListener(new LifeCycle.Listener() {
    @Override
    public void lifeCycleStarting(LifeCycle lifeCycle) {

    }

    @Override
    public void lifeCycleStarted(LifeCycle lifeCycle) {
        environment.jersey().register(new UserResource(mongo.getDatabase()));
    }

    @Override
    public void lifeCycleFailure(LifeCycle lifeCycle, Throwable throwable) {

    }

    @Override
    public void lifeCycleStopping(LifeCycle lifeCycle) {

    }

    @Override
    public void lifeCycleStopped(LifeCycle lifeCycle) {

    }
});


So how can I solve this issue? Searching through google just gave mew 
examples, where they move all the code from managed start() to the 
constructor
as this is executed before and we don't need for start() to finish. But 
this doesn't seem to me as a clean solution.

Any suggestions?

-- 
You received this message because you are subscribed to the Google Groups 
"dropwizard-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to