[英]Dropwizard: How to stop service programmatically
To start the service, I know one uses new MyService().run(args)
. 为了启动服务,我知道一个人使用new MyService().run(args)
。 How to stop it? 怎么阻止它?
I need to start and stop programmatically for setUp()
and tearDown()
in my tests. 我需要在我的测试中以编程方式启动和停止setUp()
和tearDown()
。
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 ). 但是从dropwizard 0.6.2开始,dropwizard-testing模块包含一个完全符合此用例的junit规则 ( 参见此处 )。
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(){
....
Keep the environment
variable around and add the following method to your application: 保持environment
变量并将以下方法添加到您的应用程序:
public void stop() throws Exception {
environment.getApplicationContext().getServer().stop();
}
Now you can call myService.stop()
to stop the server. 现在,您可以调用myService.stop()
来停止服务器。
Thanks @LiorH for this great suggestion. 谢谢@LiorH这个伟大的建议。
Here is a complete test class using the DropwizardServiceRule in dropwizard-0.6.2 . 这是使用dropwizard-0.6.2中的DropwizardServiceRule的完整测试类。
First create a service configuration for testing: testing-server.yml
and place it in the test's class path (ex. src\\test\\resources
). 首先创建一个用于测试的服务配置: testing-server.yml
并将其放在测试的类路径中(例如src\\test\\resources
)。 This way you can set different ports for the test service to use: 这样,您可以为要使用的测试服务设置不同的端口:
http:
port: 7000
adminPort: 7001
A simple test class that checks if there is a resource at the location "/request" looks like this: 检查“/ request”位置是否有资源的简单测试类如下所示:
class TheServiceTest {
@ClassRule
public static DropwizardServiceRule RULE = new DropwizardServiceRule<MyConfiguration>(TheService.class,
Resources.getResource("testing-server.yml").getPath());
@Test
public void
dropwizard_gets_configured_correctly() throws Exception {
Client client = new Client();
ClientResponse response = client.resource(
String.format("http://localhost:%d/request", RULE.getLocalPort()))
.get(ClientResponse.class);
assertThat(response.getStatus(), is(200));
}
}
I have also added the import in case you do not know what implementation to choose. 我还添加了导入,以防您不知道要选择哪种实现。
import com.google.common.io.Resources;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.yammer.dropwizard.testing.junit.DropwizardServiceRule;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.TestRule;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
At the end of the tests, the server will shutdown gracefully, so you do not need to worry about it. 在测试结束时,服务器将正常关闭,因此您无需担心它。
你可以尝试使用org.eclipse.jetty.server.Server的stop()方法,该方法由Dropwizard内部使用。
Or you use this java feature in your main/constructor ...: 或者你在main / constructor中使用这个java特性......:
// In case jvm 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));
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.