サービスを開始するには、new MyService().run(args)
を使用することがわかります。どのようにそれを停止するには?Dropwizard:プログラムでサービスを停止する方法
私のテストではsetUp()
とtearDown()
のプログラムで開始し、停止する必要があります。
サービスを開始するには、new MyService().run(args)
を使用することがわかります。どのようにそれを停止するには?Dropwizard:プログラムでサービスを停止する方法
私のテストではsetUp()
とtearDown()
のプログラムで開始し、停止する必要があります。
Dropwizardで内部的に使用されているorg.eclipse.jetty.server.Serverのstop()メソッドを試すことができます。
新しいスレッドでサービスを開始できます。テストが終了するとサービスは自動的にシャットダウンします。
dropwizard 0.6.2から開始するdropwizard-testingモジュールには、このユースケース(see here)用のjunit ruleが含まれています。この偉大な提案のため
Class MyTest {
@ClassRule
public static TestRule testRule = new DropwizardServiceRule<MyConfiguration>(MyService.class,
Resources.getResource("service.yml").getPath()));
@Test
public void someTest(){
....
おかげ@LiorH:このルールの
使い方は次のようになります。
dropwizard-0.6.2のDropwizardServiceRuleを使用した完全なテストクラスです。
まず、テスト用のサービス構成を作成します(testing-server.yml
)。これをテストのクラスパス(例:src\test\resources
)に配置します。あなたが使用するテストサービスのための別のポートを設定することができ この方法:
http:
port: 7000
adminPort: 7001
場所「/リクエスト」のリソースがあるかどうかを確認する簡単なテストクラスは次のようになります。
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));
}
}
どのインプリメンテーションを選択するかわからない場合に備えてインポートを追加しました。
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;
テストの最後にサーバーが正常にシャットダウンされるので、心配する必要はありません。
それとも、あなたのメイン/コンストラクタで、この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));
}
});
は約environment
変数を維持し、アプリケーションに次のメソッドを追加します。
public void stop() throws Exception {
environment.getApplicationContext().getServer().stop();
}
今することができますサーバを停止するにはmyService.stop()
に電話してください。
DWアプリケーションがWindowsサービス(Apache Commons Daemonなどを使用)として動作している場合、DWアプリケーションを停止するには非常に便利です。 –
サンプルコードを教えていただけますか?私はそれについてどうやって行くのか分かりません。 – Neo