generated from vertx-howtos/howto-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
BackendVerticle.java
63 lines (51 loc) · 1.95 KB
/
BackendVerticle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package io.vertx.howtos.cluster;
import io.vertx.core.*;
import io.vertx.ext.cluster.infinispan.ClusterHealthCheck;
import io.vertx.ext.healthchecks.HealthCheckHandler;
import io.vertx.ext.healthchecks.HealthChecks;
import io.vertx.ext.healthchecks.Status;
import io.vertx.ext.web.Router;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class BackendVerticle extends AbstractVerticle {
private static final Logger log = LoggerFactory.getLogger(BackendVerticle.class);
// tag::config[]
private static final int HTTP_PORT = Integer.parseInt(System.getenv().getOrDefault("HTTP_PORT", "0"));
private static final String POD_NAME = System.getenv().getOrDefault("POD_NAME", "unknown");
// end::config[]
// tag::start[]
@Override
public void start() {
registerConsumer();
Router router = setupRouter();
vertx.createHttpServer()
.requestHandler(router)
.listen(HTTP_PORT)
.onSuccess(server -> log.info("Server started and listening on port {}", server.actualPort()));
}
// end::start[]
// tag::consumer[]
private void registerConsumer() {
vertx.eventBus().<String>consumer("greetings", msg -> {
msg.reply(String.format("Hello %s from %s", msg.body(), POD_NAME));
});
}
// end::consumer[]
// tag::router[]
private Router setupRouter() {
Router router = Router.router(vertx);
router.get("/health").handler(rc -> rc.response().end("OK"));
Handler<Promise<Status>> procedure = ClusterHealthCheck.createProcedure(vertx, false);
HealthChecks checks = HealthChecks.create(vertx).register("cluster-health", procedure);
router.get("/readiness").handler(HealthCheckHandler.createWithHealthChecks(checks));
return router;
}
// end::router[]
// tag::main[]
public static void main(String[] args) {
Vertx.clusteredVertx(new VertxOptions())
.compose(vertx -> vertx.deployVerticle(new BackendVerticle()))
.onFailure(Throwable::printStackTrace);
}
// end::main[]
}