How to disable dropwizard app?

I am trying to come up with a microservice using dropwizard. The documentation tells how to start the application, but says nothing about terminating it. For example apache tomcat has startup and shutdown scripts.

Does anyone know how to terminate the dropwizard app other than clicking Ctrl+C

in kill

?

+2


source to share


5 answers


Dropwizard Jetty has shutdowns. So, it kill -SIGINT <pid>

works pretty well.



+10


source


Inspired by praveenag's answer, I dug into Jetty's code.

If you run DropWizard by providing:

-DSTOP.PORT=xxxx -DSTOP.KEY=my_secret_key

      

as Java parameters,



It tells Jetty to listen on this port for a stop request.

You can then write to this socket to tell Jetty to shut down. I did it from R like this:

socket = make.socket("localhost", 8082)
write.socket(socket, "my_secret_key\r\n")
write.socket(socket, "stop\r\n")
close.socket(socket)

      

I think you can do the same from any other language.

+6


source


I am guessing this is not a question for your development environment, but for your deployments. The answer depends on your deployment strategy. We used to handle deployments where the drop wizard comes as a Java process that can be started and the pid is written and forcibly kills the process. Or wind up the java process in an upstart / init script to gracefully start and shutdown the system.

On the other hand, when you launch the dropwizard application, it eventually launches the pier. http://eureka.ykyuen.info/2010/07/26/jetty-stop-a-jetty-server-by-command . Perhaps this could shed some light on how you can pass the stop port as arguments when running the dropwizard application.

+2


source


The other answers here are good, but if you want to go a little further down the stack and easily add custom / security / arbitrarily complex shutdown logic then add a shutdown trap via the admin admin dropwizard. Challenge is a good example.

Create a shutdown task with any logic you like

import io.dropwizard.servlets.tasks.Task;

public class ShutdownTask extends Task {

  public ShutdownTask() {
    super("shutdown"); // the task name, used in the endpoint to execute it 
  }

  public void execute(
    ImmutableMultimap<String, String> immutableMultimap,
    PrintWriter printWriter
  ) throws Exception {
    // kill the process asynchronously with some nominal delay
    // to allow the task http response to be sent
    new Timer().schedule(new TimerTask() {
      public void run() {
        // any custom logging / logic here prior to shutdown
        System.exit(0);
      }
    }, 5000);
  }
}

      

Register the task in Application.run()

environment.admin().addTask(new ShutdownTask());

      

And then do this via POST to the following endpoint on the admin port

http://localhost:<ADMIN PORT>/tasks/shutdown

      

+1


source


In Java:

// In case vm shutdown
Runtime.getRuntime().addShutdownHook(new Thread() {
    @Override
    public void run()
    {
        // what should be closed if forced shudown
        // ....

        LOG.info(String.format("--- End of ShutDownHook (%s) ---", "APPLICATION_NAME"));
    }
});

      

Create your own strategy for how to close the application.

-4


source







All Articles