Dropwizard:如何以编程方式停止服务
问题描述:
要启动该服务,我知道使用 new MyService()。run(args)
。如何阻止它?
To start the service, I know one uses new MyService().run(args)
. How to stop it?
我需要以编程方式启动和停止 setUp()
和 tearDown()
在我的测试中。
I need to start and stop programmatically for setUp()
and tearDown()
in my tests.
答
你可以在新线程中启动服务,一次测试结束服务将自动关闭。
You can start the service in new thread, once the test ends the service will shutdown automatically.
然而从dropwizard 0.6.2开始,dropwizard测试模块包含一个 junit规则(请看这里)。
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(){
....