It sounds like an issue I would solve outside of Dropwizard.  There are tools 
that would let you manage the order of your services startups so that Mongo is 
available by the time the Dropwizard service starts.

Michael Corum
VP, Technical Architecture Solutions

RGA Reinsurance Company
16600 Swingley Ridge Road
Chesterfield, Missouri 6301701706
T 636.736.7066
www.rgare.com


From: "[email protected]" <[email protected]> on 
behalf of Thomas Hübner <[email protected]>
Reply-To: "[email protected]" <[email protected]>
Date: Friday, December 15, 2017 at 9:53 AM
To: "[email protected]" <[email protected]>
Subject: Dropwizard init / startup confusion

External e-mail. Use caution! / Courriel externe. Faites attention!
________________________________
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]<mailto:[email protected]>.
For more options, visit 
https://groups.google.com/d/optout<https://urldefense.proofpoint.com/v2/url?u=https-3A__groups.google.com_d_optout&d=DwMFaQ&c=5uPv0lijNz76uSeaN5P0Zw&r=rh3Qrw7azSI9xkZZ-a8EEw&m=ubw1gUcrMZNFwj-6L_nDrcxwPc4PZzFXsF7yGXrBj0Q&s=Zbm49XgnH1a4Y4DmpnamXphGiJm0CXCGmHeuaRJ5wns&e=>.

-- 
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