Skip to content

Instantly share code, notes, and snippets.

@noctarius
Created February 7, 2017 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noctarius/b0c5a3e5269e4190c5599c9934acd330 to your computer and use it in GitHub Desktop.
Save noctarius/b0c5a3e5269e4190c5599c9934acd330 to your computer and use it in GitHub Desktop.
java:
import io.vertx.core.*;
import io.vertx.core.http.*;
import io.vertx.core.shareddata.AsyncMap;
import io.vertx.core.spi.cluster.ClusterManager;
import io.vertx.spi.cluster.hazelcast.HazelcastClusterManager;
import java.util.Optional;
import java.util.function.Consumer;
public class VertxExample {
public static void main(String[] args) {
ClusterManager clusterManager = new HazelcastClusterManager();
VertxOptions options = new VertxOptions().setClusterManager(clusterManager);
Vertx.clusteredVertx(options, res -> {
if (res.succeeded()) {
Vertx vertx = res.result();
vertx.deployVerticle(new Server());
}
});
}
public static class Server extends AbstractVerticle {
@Override
public void start() throws Exception {
HttpServer httpServer = vertx.createHttpServer();
httpServer.requestHandler(req -> {
String key = req.getParam("key");
req.response().setChunked(true);
switch (req.method()) {
case GET:
map("my-map", map -> map.get(key, response(req)));
break;
case POST:
case PUT:
map("my-map", map -> map.put(key, req.getParam("value"), response(req)));
}
});
httpServer.listen(8080);
}
private <T> Handler<AsyncResult<T>> response(HttpServerRequest req) {
return res -> {
HttpServerResponse response = req.response();
if (res.succeeded()) response = response.setStatusCode(200).write(
Optional.ofNullable(res.result()).map(v -> v.toString()).orElse("null"));
else response = response.setStatusCode(500).write(res.cause().toString());
response.end();
};
}
private void map(String mapName, Consumer<AsyncMap<String, String>> consumer) {
vertx.sharedData().<String, String> getClusterWideMap(mapName, res -> {
if (res.succeeded()) consumer.accept(res.result());
});
}
}
}
kotlin:
import io.vertx.core.*
import io.vertx.core.http.*
import io.vertx.core.shareddata.AsyncMap
import io.vertx.spi.cluster.hazelcast.HazelcastClusterManager
fun main(args: Array<String>) {
val clusterManager = HazelcastClusterManager()
val vertxOptions = VertxOptions().apply { this.clusterManager = clusterManager }
Vertx.clusteredVertx(vertxOptions) { res ->
res.result().deployVerticle(Server())
}
}
class Server : AbstractVerticle() {
override fun start() {
vertx.createHttpServer().requestHandler { req ->
val key = req.getParam("key")
req.response().isChunked = true
when(req.method()) {
HttpMethod.GET ->
map("my-map") { it.get(key, response(req)) }
HttpMethod.POST, HttpMethod.PUT ->
map("my-map") { it.put(key, req.getParam("value"), response(req)) }
}
}.listen(8080)
}
fun <T> response(req: HttpServerRequest): Handler<AsyncResult<T>> = Handler {
val (statusCode, body) = if (it.succeeded()) 200 to it.result() else 500 to it.cause()
req.response().setStatusCode(statusCode).write(body.toString()).end()
}
fun map(mapName: String, consumer: (AsyncMap<String, String>) -> Unit) =
vertx.sharedData().getClusterWideMap<String, String>(mapName) { if (it.succeeded()) consumer(it.result()) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment