Dropwizard:如何以编程方式停止服务

Neo*_*Neo 12 java dropwizard

要开始这项服务,我知道有人使用new MyService().run(args).怎么阻止它?

我需要启动和编程停止setUp(),并tearDown()在我的测试.

Lio*_*orH 12

您可以在新线程中启动服务,一旦测试结束,服务将自动关闭.

但是从dropwizard 0.6.2开始,dropwizard-testing模块包含一个完全符合此用例的junit规则(参见此处).

此规则的用法如下所示:

Class MyTest {

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

    @Test
    public void someTest(){
    ....
Run Code Online (Sandbox Code Playgroud)


Ale*_*uin 7

保留environment变量并将以下方法添加到您的应用程序:

public void stop() throws Exception {
  environment.getApplicationContext().getServer().stop();
}
Run Code Online (Sandbox Code Playgroud)

现在您可以调用myService.stop()停止服务器.


San*_*y T 1

您可以尝试使用 org.eclipse.jetty.server.Server 的 stop() 方法,该方法是 Dropwizard 内部使用的。