@Override public void start() throws Exception { HttpServer server = vertx.createHttpServer(); server.requestStream().toObservable().subscribe(req -> { req.response().putHeader("content-type", "application/json").end("{\"message\":\"Hello World\"}"); }); server.listen(8080); } }
@Override public void start() throws Exception { HttpClient client = vertx.createHttpClient(); HttpClientRequest req = client.request(HttpMethod.GET, 8080, "localhost", "/"); req.toObservable(). flatMap(HttpClientResponse::toObservable). // Unmarshall the response to the Data object via Jackon lift(io.vertx.rxjava.core.RxHelper.unmarshaller(Data.class)). subscribe( data -> { System.out.println("Got response " + data.message); }); // End request req.end(); } }
@Override public void start() throws Exception { HttpClient client = vertx.createHttpClient(); client.put(8080, "localhost", "/", resp -> { System.out.println("Got response " + resp.statusCode()); resp.handler(buf -> System.out.println(buf.toString("UTF-8"))); }).setChunked(true).putHeader("Content-Type", "text/plain").write("hello").end(); } }
@Override public void start() throws Exception { EventBus eb = vertx.eventBus(); eb.consumer(ADDRESS) .toObservable() .subscribe(message -> { System.out.println("Received " + message.body()); message.reply("PONG"); }); // Send a message every second vertx.setPeriodic(1000, v -> { eb.rxSend(ADDRESS, "PING") .subscribe(reply -> { System.out.println("Received reply " + reply.body()); }); }); } }
@Override public void start() throws Exception { HttpServer server = vertx.createHttpServer(); server.requestStream().toObservable().subscribe(req -> { HttpServerResponse resp = req.response(); String contentType = req.getHeader("Content-Type"); if (contentType != null) { resp.putHeader("Content-Type", contentType); } resp.setChunked(true); req.toObservable().subscribe( resp::write, err -> {}, resp::end ); }); server.listen(8080); } }
@Override public void start() throws Exception { HttpClient client = vertx.createHttpClient(); HttpClientRequest req = client.request(HttpMethod.GET, 8080, "localhost", "/"); req.toObservable(). // Status code check and -> Observable<Buffer> flatMap(resp -> { if (resp.statusCode() != 200) { throw new RuntimeException("Wrong status code " + resp.statusCode()); } return resp.toObservable(); }). subscribe(data -> System.out.println("Server content " + data.toString("UTF-8"))); // End request req.end(); } }
@Override public void start() throws Exception { EventBus eb = vertx.eventBus(); eb.consumer("news-feed"). toObservable(). subscribe(message -> System.out.println("Received news: " + message.body())); System.out.println("Ready!"); } }
@Override public void start() throws Exception { Random random1 = new Random(); EventBus eb = vertx.eventBus(); eb.consumer("heatsensor1"). toObservable(). subscribe(message -> { message.reply(9 + random1.nextInt(5)); }); eb.consumer("heatsensor2"). toObservable(). subscribe(message -> { message.reply(10 + random1.nextInt(3)); }); } }
@Override public void start() throws Exception { Router router = Router.router(vertx); router.route().handler(routingContext -> { routingContext.response().putHeader("content-type", "text/html").end("Hello World!"); }); vertx.createHttpServer().requestHandler(router).listen(8080); } }
@Override public void start() throws Exception { EventBus eb = vertx.eventBus(); vertx.setPeriodic(1000, v -> { Single<Message<Integer>> reply1 = eb.<Integer>rxSend("heatsensor1", "ping"); Single<Message<Integer>> reply2 = eb.<Integer>rxSend("heatsensor2", "ping"); Single<int[]> reply = reply1.zipWith(reply2, (msg1, msg2) -> new int[]{msg1.body(), msg2.body()});
@Override public void start() throws Exception { JsonObject config = new JsonObject() .put("connection_string", "mongodb://localhost:27018") .put("db_name", "my_DB"); // Deploy an embedded mongo database so we can test against that vertx.deployVerticle("service:io.vertx.vertx-mongo-embedded-db", db -> { if (db.succeeded()) { // Create the client mongo = MongoClient.createShared(vertx, config); insertAndFind(); } else { System.out.println("Could not start mongo embedded"); db.cause().printStackTrace(); } }); }
@Override public void start() throws Exception { // Scheduler scheduler = io.vertx.rxjava.core.RxHelper.scheduler(vertx); // Create a periodic event stream using Vertx scheduler Observable<Long> o = Observable. timer(0, 1000, TimeUnit.MILLISECONDS, scheduler); o.subscribe(item -> { System.out.println("Got item " + item); }); } }
@Override public void start() throws Exception { Observable<String> o = Observable.just("someID1", "someID2", "someID3", "someID4"); // This scheduler can execute blocking actions Scheduler scheduler = io.vertx.rxjava.core.RxHelper.blockingScheduler(vertx); // All operations done on the observer now can be blocking o = o.observeOn(scheduler); // Load from a blocking api o = o.map(id -> blockingLoad(id) ); o.subscribe(item -> { System.out.println("Got item " + item); }, err -> { err.printStackTrace(); }, () -> { System.out.println("Done"); }); } }
@Override public void start() throws Exception { HttpServer server = vertx.createHttpServer(); server.requestStream().toObservable().subscribe(req -> { req.response().putHeader("content-type", "application/json").end("{\"message\":\"Hello World\"}"); }); server.listen(8080); } }
@Override public void start() throws Exception { HttpClient client = vertx.createHttpClient(); HttpClientRequest req = client.request(HttpMethod.GET, 8080, "localhost", "/"); req.toObservable(). // Status code check and -> Observable<Buffer> flatMap(resp -> { if (resp.statusCode() != 200) { throw new RuntimeException("Wrong status code " + resp.statusCode()); } return Observable.just(Buffer.buffer()).mergeWith(resp.toObservable()); }). // Reduce all buffers in a single buffer reduce(Buffer::appendBuffer). // Turn in to a string map(buffer -> buffer.toString("UTF-8")). // Get a single buffer subscribe(data -> System.out.println("Server content " + data)); // End request req.end(); } }
@Override public void start() throws Exception { HttpServer server = vertx.createHttpServer(); server.requestStream().toObservable().subscribe(req -> { req.response().putHeader("content-type", "text/html").end("<html><body><h1>Hello from vert.x!</h1></body></html>"); }); server.listen(8080); } }
@Override public void start() throws Exception { HttpServer server = vertx.createHttpServer(); server.requestStream().toObservable().subscribe(req -> { req.response().putHeader("content-type", "text/html").end("<html><body><h1>Hello from vert.x!</h1></body></html>"); }); server.listen(8080); } }
@Override public void start() throws Exception { HttpServer server = vertx.createHttpServer(); server.requestStream().toObservable().subscribe(req -> { req.response().putHeader("content-type", "text/html").end("<html><body><h1>Hello from vert.x!</h1></body></html>"); }); server.listen(8080); } }
@Override public void start() throws Exception { HttpServer server = vertx.createHttpServer(); server.requestStream().toObservable().subscribe(req -> { req.response().putHeader("content-type", "application/json").end(new JsonObject().put("time", System.currentTimeMillis()).toString()); }); server.listen(8080); } }
@Override public void start() throws Exception { HttpServer server = vertx.createHttpServer(); server.requestStream().toObservable().subscribe(req -> { req.response().putHeader("content-type", "application/json").end(new JsonObject().put("time", System.currentTimeMillis()).toString()); }); server.listen(8080); } }