Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dropwizard: read configuration from a non-file source

What's the right way to read configuration in dropwizard from something like a database, or a REST call? I have a use case where I cannot have a yml file with some values, and should retrieve settings/config at startup time from a preconfigured URL with REST calls.

Is it right to just invoke these REST calls in the get methods of the ApplicationConfiguration class?

like image 700
ragebiswas Avatar asked Feb 09 '16 13:02

ragebiswas


People also ask

Does Dropwizard use Jetty?

Because you can't be a web application without HTTP, Dropwizard uses the Jetty HTTP library to embed an incredibly tuned HTTP server directly into your project.

What language does the Dropwizard framework use to create the configuration file?

Dropwizard stores configurations in YAML files. You will need to have the file configuration. yml in your application root folder. This file will then be deserialized to an instance of your application's configuration class and validated.

Does Dropwizard use Log4j?

Dropwizard uses Logback for its logging backend. It provides an slf4j implementation, and even routes all java. util. logging , Log4j, and Apache Commons Logging usage through Logback.


2 Answers

Similar to my answer here, you implement the ConfigurationSourceProvider interface the way you wish to implement and configure your dropwizard application to use it on your Application class by:

@Override
public void initialize(Bootstrap<MyConfiguration> bootstrap){
  bootstrap.setConfigurationSourceProvider(new MyDatabaseConfigurationSourceProvider());
}

By default, the InputStream you return is read as YAML and mapped to the Configuration object. The default implementation

You can override this via

bootstrap.setConfigurationFactoryFactory(new MyDatabaseConfigurationFactoryFactory<>());

Then you have your FactoryFactory :) that returns a Factory which reads the InputStream and returns your Configuration.

public T build(ConfigurationSourceProvider provider, String path {
  Decode.onWhateverFormatYouWish(provider.open(path));
}
like image 52
Natan Avatar answered Sep 19 '22 00:09

Natan


elaborating a bit further on Nathan's reply, you might want to consider using the UrlConfigurationSourceProvider , which is also provided with dropwizard, and allows to retrieve the configuration from an URL.

Something like:

@Override
public void initialize(Bootstrap<MyRestApplicationConfiguration> bootstrap) {
    bootstrap.setConfigurationSourceProvider(new UrlConfigurationSourceProvider());
}
like image 44
Daniele Avatar answered Sep 22 '22 00:09

Daniele