Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating Dropwizard config at runtime

Is it possible to have my app update the config settings at runtime? I can easily expose the settings I want in my UI but is there a way to allow the user to update settings and make them permanent ie save them to the config.yaml file? The only way I can see it to update the file by hand then restart the server which seems a bit limiting.

like image 280
Martin Charlesworth Avatar asked Dec 15 '14 18:12

Martin Charlesworth


People also ask

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.

Does Dropwizard use Jetty?

Overview. Dropwizard is an open-source Java framework used for the fast development of high-performance RESTful web services. It gathers some popular libraries to create the light-weight package. The main libraries that it uses are Jetty, Jersey, Jackson, JUnit, and Guava.

What is difference between Dropwizard and spring boot?

Dropwizard takes the convention over configuration approach a bit more extreme than Spring Boot and is completely based on Jetty, while Spring Boot takes the embeddable version of Tomcat by default but leaves you a way out if preferer Jetty or even RedHat's Undertow.

Which server does Dropwizard use?

Jetty for HTTP 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.


2 Answers

Yes. It is possible to reload the service classes at runtime.

Dropwizard by itself does not have the way to reload the app, but jersey has.

Jersey uses a container object internally to maintain the running application. Dropwizard uses the ServletContainer class of Jersey to run the application.

How to reload the app without restarting it -

  1. Get a handle to the container used internally by jersey

    You can do this by registering a AbstractContainerLifeCycleListener in Dropwizard Environment before starting the app. and implement its onStartup method as below -

In your main method where you start the app -

//getting the container instance
        environment.jersey().register(new AbstractContainerLifecycleListener()  {
        @Override
        public void onStartup(Container container) {
            //initializing container - which will be used to reload the app
            _container = container;
        }

    });  
  1. Add a method to your app to reload the app. It will take in the list of string which are the names of the service classes you want to reload. This method will call the reload method of the container with the new custom DropWizardConfiguration instance.

In your Application class

 public static synchronized void reloadApp(List<String> reloadClasses) {
        DropwizardResourceConfig dropwizardResourceConfig = new DropwizardResourceConfig();

        for (String className : reloadClasses) {
           try {
                Class<?> serviceClass = Class.forName(className);
                dropwizardResourceConfig.registerClasses(serviceClass);
                System.out.printf(" + loaded class %s.\n", className);
            } catch (ClassNotFoundException ex) {
                System.out.printf(" ! class %s not found.\n", className);
            }
        }
        _container.reload(dropwizardResourceConfig);

    }  

For more details see the example documentation of jersey - jersey example for reload

Consider going through the code and documentation of following files in Dropwizard/Jersey for a better understanding -

Container.java

ContainerLifeCycleListener.java

ServletContainer.java

AbstractContainerLifeCycleListener.java

DropWizardResourceConfig.java

ResourceConfig.java

like image 75
Jay Khatwani Avatar answered Sep 28 '22 07:09

Jay Khatwani


No.

Yaml file is parsed at startup and given to the application as Configuration object once and for all. I believe you can change the file after that but it wouldn't affect your application until you restart it.

Possible follow up question: Can one restart the service programmatically?

AFAIK, no. I've researched and read the code somewhat for that but couldn't find a way to do that yet. If there is, I'd love to hear that :).

like image 38
Natan Avatar answered Sep 28 '22 07:09

Natan