Skip to content

Instantly share code, notes, and snippets.

@cescoffier
Created May 8, 2017 16:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cescoffier/f6f2131d8f1bbdc2320b3c0a2268735d to your computer and use it in GitHub Desktop.
Save cescoffier/f6f2131d8f1bbdc2320b3c0a2268735d to your computer and use it in GitHub Desktop.
Example of verticle reconfiguration
package io.vertx.sample;
import io.vertx.config.ConfigRetrieverOptions;
import io.vertx.config.ConfigStoreOptions;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.json.JsonObject;
import io.vertx.rxjava.config.ConfigRetriever;
import io.vertx.rxjava.core.AbstractVerticle;
public class MyFirstVerticle extends AbstractVerticle {
@Override
public void start() {
ConfigStoreOptions file = new ConfigStoreOptions()
.setType("file")
.setFormat("yaml")
.setConfig(new JsonObject().put("path", "my-config.yml"));
ConfigRetrieverOptions options = new ConfigRetrieverOptions()
.addStore(file);
ConfigRetriever retriever = ConfigRetriever.create(vertx, options);
// Initial retrieval
retriever.rxGetConfig()
.subscribe(conf ->
vertx.deployVerticle(SecondVerticle.class.getName(),
new DeploymentOptions().setConfig(conf))
);
// Listen for change and propagate them to the event bus.
retriever.configStream().toObservable().subscribe(newConf -> {
vertx.eventBus().publish("reconf", newConf);
});
}
}
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>io.vertx.sample</groupId>
<artifactId>my-first-vertx-app</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<vertx.version>3.4.1</vertx.version>
<vertx.verticle>io.vertx.sample.MyFirstVerticle</vertx.verticle>
<fabric8-vertx-maven-plugin.version>1.0.7</fabric8-vertx-maven-plugin.version>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-dependencies</artifactId>
<version>${vertx.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-config</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-config-yaml</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-rx-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>vertx-maven-plugin</artifactId>
<version>${fabric8-vertx-maven-plugin.version}</version>
<executions>
<execution>
<id>vmp</id>
<goals>
<goal>initialize</goal>
<goal>package</goal>
</goals>
</execution>
</executions>
<configuration>
<redeploy>true</redeploy>
</configuration>
</plugin>
</plugins>
</build>
</project>
package io.vertx.sample;
import io.vertx.core.json.JsonObject;
import io.vertx.rxjava.core.AbstractVerticle;
/**
* @author <a href="http://escoffier.me">Clement Escoffier</a>
*/
public class SecondVerticle extends AbstractVerticle {
private JsonObject conf = new JsonObject();
@Override
public void start() throws Exception {
conf = config();
// Listen for change
vertx.eventBus().<JsonObject>consumer("reconf").toObservable()
.subscribe(message -> {
conf = message.body();
System.out.println("configuration updated in " + this.getClass().getName() + " / " + conf);
});
vertx.createHttpServer()
.requestHandler(req -> req.response().end(conf.encodePrettily()))
.listen(8080);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment