diff --git a/heroku-example/pom.xml b/heroku-example/pom.xml index 35606f030..d5b1e26ee 100644 --- a/heroku-example/pom.xml +++ b/heroku-example/pom.xml @@ -19,6 +19,11 @@ vertx-core ${project.version} + + io.vertx + vertx-config + ${project.version} + @@ -74,7 +79,7 @@ com.heroku.sdk heroku-maven-plugin - 0.5.6 + 2.0.7 java $JAVA_OPTS -Dhttp.port=$PORT -jar target/heroku-example-${project.version}-fat.jar diff --git a/heroku-example/src/main/java/io/vertx/example/HelloWorldVerticle.java b/heroku-example/src/main/java/io/vertx/example/HelloWorldVerticle.java index e01303a41..be50679d6 100644 --- a/heroku-example/src/main/java/io/vertx/example/HelloWorldVerticle.java +++ b/heroku-example/src/main/java/io/vertx/example/HelloWorldVerticle.java @@ -1,15 +1,63 @@ package io.vertx.example; +import io.vertx.config.ConfigRetriever; +import io.vertx.config.ConfigRetrieverOptions; +import io.vertx.config.ConfigStoreOptions; import io.vertx.core.AbstractVerticle; -import io.vertx.core.Vertx; +import io.vertx.core.Future; +import io.vertx.core.json.JsonObject; public class HelloWorldVerticle extends AbstractVerticle { + private ConfigRetriever configRetriever; + private JsonObject config; + @Override - public void start() throws Exception { - vertx.createHttpServer().requestHandler(req -> req.response().end("Hello World!")) - .listen( - Integer.getInteger("http.port"), System.getProperty("http.address", "0.0.0.0")); + public void start(Future startFuture) throws Exception { + retrieveConfig().setHandler(ar -> { + if (ar.succeeded()) { + vertx.createHttpServer() + .requestHandler(req -> req.response().end("Hello World!")) + .listen(config.getInteger("http.port"), config.getString("http.address", "0.0.0.0")); + startFuture.complete(); + } else { + startFuture.fail(ar.cause()); + } + }); } -} + @Override + public void stop() throws Exception { + configRetriever.close(); + super.stop(); + } + + private Future retrieveConfig() { + final Future future = Future.future(); + + // Option for reading the Config Vars which are defined in the settings of the application on Heroku + final ConfigStoreOptions configStoreEnvOptions = new ConfigStoreOptions() + .setType("env") + .setOptional(true); + + final ConfigStoreOptions configStoreSysOptions = new ConfigStoreOptions() + .setType("sys") + .setOptional(true); + + final ConfigRetrieverOptions configRetrieverOptions = new ConfigRetrieverOptions() + .addStore(configStoreEnvOptions) + .addStore(configStoreSysOptions); + + configRetriever = ConfigRetriever.create(vertx, configRetrieverOptions); + configRetriever.getConfig(ar -> { + if (ar.succeeded()) { + config = ar.result(); + future.complete(); + } else { + future.fail(ar.cause()); + } + }); + + return future; + } +}