Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropwizard: How to stop service programmatically

To start the service, I know one uses new MyService().run(args). How to stop it?

I need to start and stop programmatically for setUp() and tearDown() in my tests.

like image 564
Neo Avatar asked Apr 13 '13 14:04

Neo


2 Answers

You can start the service in new thread, once the test ends the service will shutdown automatically.

However starting in dropwizard 0.6.2 the dropwizard-testing module contains a junit rule exactly for this use case (see here).

Usage of this rule will look something like this:

Class MyTest {

    @ClassRule
    public static TestRule testRule = new DropwizardServiceRule<MyConfiguration>(MyService.class,
                    Resources.getResource("service.yml").getPath()));

    @Test
    public void someTest(){
    ....
like image 82
LiorH Avatar answered Sep 17 '22 04:09

LiorH


Keep the environment variable around and add the following method to your application:

public void stop() throws Exception {
  environment.getApplicationContext().getServer().stop();
}

Now you can call myService.stop() to stop the server.

like image 38
Alexandre Ardhuin Avatar answered Sep 18 '22 04:09

Alexandre Ardhuin