Hello Worldから続けてUser Manualを確認していく。
Dropwizardのバージョンは0.6.2。
設定ファイル
使用できる設定ファイルはyamlとjson。
ファイルの拡張子がyml、yamlでない場合はjsonとしてパースをするとのこと。
message-queue.yml
messageQueue:
host: mq.example.com
port: 5673
message-queue.json
{
"messageQueue" : {
"host" : "mq.example.com",
"port" : 5678
}
}
設定のグループ化
Configurationはある程度のまとまりでグループ化することができる。
入れ子になったyamlに対応するConfigurationは以下のとおり。
message-queue.yml
messageQueue:
host: mq.example.com
port: 5673
MessageQueueConfiguration.java
package example.core.configuration;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.hibernate.validator.constraints.NotEmpty;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
public class MessageQueueConfiguration {
@NotEmpty
@JsonProperty
private String host;
@Min(1)
@Max(65535)
@JsonProperty
private int port = 5672;
public String getHost() {
return host;
}
public int getPort() {
return port;
}
}
ExampleServiceConfiguration.java
package example.core.configuration;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.yammer.dropwizard.config.Configuration;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
public class ExampleServiceConfiguration extends Configuration {
@Valid
@NotNull
@JsonProperty
private MessageQueueConfiguration messageQueue = new MessageQueueConfiguration();
public MessageQueueConfiguration getMessageQueue() {
return messageQueue;
}
}
設定の検証
ConfigurationはHibernate Validatorのアノテーションを付与することでサーバ起動時に検証することができる。
入れ子になっている場合も@Valid
で検証可能。
システムプロパティで上書き
サーバ起動時のコマンドに-Ddw.(設定のパス)=(値)
を追加することでyamlファイルの設定値を上書きすることができる。
java -jar example.jar -Ddw.messageQueue.port=5432 server message-queue.yml