@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 { HttpServer server = vertx.createHttpServer(); server.requestStream().toFlowable().subscribe(req -> { req.response().putHeader("content-type", "application/json").end("{\"message\":\"Hello World\"}"); }); server.listen(8080); } }
private int successCounter(HttpClientResponse resp) { return resp.statusCode() == 200 ? successCounter.incrementAndGet() : successCounter.get(); }
@Override public void start() throws Exception { Router router = Router.router(vertx); router.route().handler(BodyHandler.create()); router.route().handler(req -> req.response().putHeader("content-type", "text/html") .end("<html><body><h1>Hello from vert.x!</h1></body></html>")); HttpServer server = vertx.createHttpServer(); server.requestStream() .toFlowable() .map(HttpServerRequest::pause) .onBackpressureDrop(req -> req.response().setStatusCode(503).end()) .observeOn(RxHelper.scheduler(vertx.getDelegate())) .subscribe(req -> { req.resume(); router.accept(req); }); server.rxListen(PORT).subscribe(res -> generateRequests()); }
@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 { HttpServer server = vertx.createHttpServer(); server.requestStream().toFlowable().subscribe(req -> { HttpServerResponse resp = req.response(); String contentType = req.getHeader("Content-Type"); if (contentType != null) { resp.putHeader("Content-Type", contentType); } resp.setChunked(true); req.toFlowable().subscribe( resp::write, err -> {}, resp::end ); }); server.listen(8080); } }
@Override public void start() throws Exception { Router router = Router.router(vertx); router.route("/news-feed/*").handler(SockJSHandler.create(vertx).socketHandler(sockJSSocket -> { // Consumer the event bus address as an Flowable Flowable<String> msg = vertx.eventBus().<String>consumer("news-feed") .bodyStream() .toFlowable(); // Send the event to the client Disposable subscription = msg.subscribe(sockJSSocket::write); // Unsubscribe (dispose) when the socket closes sockJSSocket.endHandler(v -> { subscription.dispose(); }); })); // Serve the static resources router.route().handler(StaticHandler.create()); vertx.createHttpServer().requestHandler(router).listen(8080); // Publish a message to the address "news-feed" every second vertx.setPeriodic(1000, t -> vertx.eventBus().publish("news-feed", "news from the server!")); } }
@Override public void start() throws Exception { HttpClient client = vertx.createHttpClient(); HttpClientRequest req = client.request(HttpMethod.GET, 8080, "localhost", "/"); req.toFlowable(). // Status code check and -> Flowable<Buffer> flatMap(resp -> { if (resp.statusCode() != 200) { throw new RuntimeException("Wrong status code " + resp.statusCode()); } return resp.toFlowable(); }). subscribe(data -> System.out.println("Server content " + data.toString("UTF-8"))); // End request req.end(); } }
@Override public void start() throws Exception { HttpClient client = vertx.createHttpClient(); HttpClientRequest req = client.request(HttpMethod.GET, 8080, "localhost", "/"); req.toFlowable(). flatMap(HttpClientResponse::toFlowable) .map(buffer -> buffer.toJsonObject().mapTo(Data.class)) .subscribe( data -> System.out.println("Got response " + data.message)); // End request req.end(); } }
private void generateRequests() throws InterruptedException { List<Callable<HttpClient>> tasks = Collections.nCopies(NUMBER_OF_REQUESTS, () -> vertx.createHttpClient() .getNow(PORT, "localhost", "/", resp -> log(resp, successCounter(resp), failureCounter(resp)) )); Executors.newFixedThreadPool(N_THREADS).invokeAll(tasks); }
@Override public void start() throws Exception { HttpServer server = vertx.createHttpServer(); server.requestStream().toFlowable().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 { HttpClient client = vertx.createHttpClient(); HttpClientRequest req = client.request(HttpMethod.GET, 8080, "localhost", "/"); req.toFlowable(). // Status code check and -> Flowable<Buffer> flatMap(resp -> { if (resp.statusCode() != 200) { throw new RuntimeException("Wrong status code " + resp.statusCode()); } return Flowable.just(Buffer.buffer()).mergeWith(resp.toFlowable()); }). // 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 { HttpClient client = vertx.createHttpClient(); // Create two requests HttpClientRequest req1 = client.request(HttpMethod.GET, 8080, "localhost", "/"); HttpClientRequest req2 = client.request(HttpMethod.GET, 8080, "localhost", "/"); // Turn the requests responses into Flowable<JsonObject> Flowable<JsonObject> obs1 = req1.toFlowable().flatMap(HttpClientResponse::toFlowable). map(buf -> new JsonObject(buf.toString("UTF-8"))); Flowable<JsonObject> obs2 = req2.toFlowable().flatMap(HttpClientResponse::toFlowable). map(buf -> new JsonObject(buf.toString("UTF-8"))); // Combine the responses with the zip into a single response obs1.zipWith(obs2, (b1, b2) -> new JsonObject().put("req1", b1).put("req2", b2)). subscribe(json -> { System.out.println("Got combined result " + json); }, Throwable::printStackTrace); req1.end(); req2.end(); } }
private int failureCounter(HttpClientResponse resp) { return resp.statusCode() == 503 ? failureCounter.incrementAndGet() : failureCounter.get(); }
@Override public void start() throws Exception { HttpServer server = vertx.createHttpServer(); server.requestStream().toFlowable().subscribe(req -> { req.response().putHeader("content-type", "application/json").end("{\"message\":\"Hello World\"}"); }); server.listen(8080); } }
private void log(HttpClientResponse response, int success, int failure) { System.out.println(String .format( "Response status code: [%s] , number of all responses [%s], number of correct responses [%s], number of dropped responses [%s]", response.statusCode(), allCounter.incrementAndGet(), success, failure)); }
@Override public void start() throws Exception { HttpServer server = vertx.createHttpServer(); server.requestStream().toFlowable().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().toFlowable().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().toFlowable().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().toFlowable().subscribe(req -> { req.response().putHeader("content-type", "application/json").end(new JsonObject().put("time", System.currentTimeMillis()).toString()); }); server.listen(8080); } }