@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().toObservable().subscribe(req -> { req.response().putHeader("content-type", "application/json").end(new JsonObject().put("time", System.currentTimeMillis()).toString()); }); server.listen(8080); } }
protected Single<Void> createHttpServer(Router router, String host, int port) { return vertx.createHttpServer() .requestHandler(router::accept) .rxListen(port, host) .map(r -> null); }
@Test public void testHttpClientFlatMapUnmarshallPojo() { HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080)); server.requestStream().handler(req -> { req.response().setChunked(true).end("{\"foo\":\"bar\"}"); }); server.listen(ar -> { HttpClient client = vertx.createHttpClient(new HttpClientOptions()); Single<HttpClientResponse> req = client.rxGetNow(8080, "localhost", "/the_uri"); ArrayList<SimplePojo> objects = new ArrayList<>(); req.flatMapObservable(HttpClientResponse::toObservable). lift(io.vertx.rxjava.core.RxHelper.unmarshaller(SimplePojo.class)). forEach( objects::add, err -> fail(), () -> { server.close(); assertEquals(Arrays.asList(new SimplePojo("bar")), objects); testComplete(); }); }); await(); }
@Override public void start(Future<Void> startFuture) throws Exception { String name = this.getClass().getSimpleName(); int port = eventBusBridgeProperties.getPort(); String routeRegex = eventBusBridgeProperties.getRoutePrefix() .map(val -> String.format("/%s/*", val)) .orElse("/*"); log.info("Starting {} on port {} with route {}...", name, port, routeRegex); Router router = Router.router(vertx); router.route(routeRegex).handler(sockJSHandler); vertx.createHttpServer() .requestHandler(router::accept) .rxListen(port) .subscribe(server -> { log.info("Started {} on port {}", name, server.actualPort()); startFuture.complete(); }, startFuture::fail); }
/** * Like {@link io.vertx.rxjava.core.http.HttpServer#listen} but supplying a handler that will be called when the server is actually listening (or has failed). * @param port the port to listen on * @return * @deprecated use {@link #rxListen} instead */ @Deprecated() public Observable<io.vertx.rxjava.core.http.HttpServer> listenObservable(int port) { io.vertx.rx.java.ObservableFuture<io.vertx.rxjava.core.http.HttpServer> listenHandler = io.vertx.rx.java.RxHelper.observableFuture(); listen(port, listenHandler.toHandler()); return listenHandler; }
@Override public void start(Future<Void> startFuture) { // deploy the web server HttpServerOptions options = new HttpServerOptions() .setCompressionSupported(true); HttpServer server = vertx.createHttpServer(options); server.requestHandler(this::onRequest); server.listenObservable(8080) // listen on port 8080 .subscribe(v -> startFuture.complete(), startFuture::fail); }
@Test public void testWebsocketClientFlatMap() { HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080)); server.websocketStream().handler(ws -> { ws.write(Buffer.buffer("some_content")); ws.close(); }); server.listen(ar -> { HttpClient client = vertx.createHttpClient(new HttpClientOptions()); Buffer content = Buffer.buffer(); client. websocketStream(8080, "localhost", "/the_uri"). toObservable(). flatMap(WebSocket::toObservable). forEach(content::appendBuffer, err -> fail(), () -> { server.close(); assertEquals("some_content", content.toString("UTF-8")); testComplete(); }); }); await(); }
@Test public void testObservableHttpRequest() { HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080).setHost("localhost")); Observable<HttpServerRequest> socketObs = server.requestStream().toObservable(); socketObs.subscribe(new Subscriber<HttpServerRequest>() { @Override Single<HttpServer> onListen = server.rxListen(); onListen.subscribe( s -> {
); HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080).setHost("localhost")); Observable<ServerWebSocket> socketObs = server.websocketStream().toObservable(); socketObs.subscribe(new Subscriber<ServerWebSocket>() { @Override server.listen(onListen.toHandler()); await();
/** * Like {@link io.vertx.rxjava.core.http.HttpServer#close} but supplying a handler that will be called when the server is actually closed (or has failed). * @return * @deprecated use {@link #rxClose} instead */ @Deprecated() public Observable<Void> closeObservable() { io.vertx.rx.java.ObservableFuture<Void> completionHandler = io.vertx.rx.java.RxHelper.observableFuture(); close(completionHandler.toHandler()); return completionHandler; }
public static HttpServer newInstance(io.vertx.core.http.HttpServer arg) { return arg != null ? new HttpServer(arg) : null; } }
@Override public void start() throws Exception { HttpServer server = vertx.createHttpServer().requestHandler(routerProvider.get()::accept); server.rxListen(httpServerPort).subscribe(); } }
@Test public void testResponseBodyAsAsJsonMapped() throws Exception { JsonObject expected = new JsonObject().put("cheese", "Goat Cheese").put("wine", "Condrieu"); HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080)); server.requestStream().handler(req -> req.response().end(expected.encode())); try { server.listen(ar -> { client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions())); Single<HttpResponse<WineAndCheese>> single = client .get(8080, "localhost", "/the_uri") .as(BodyCodec.json(WineAndCheese.class)) .rxSend(); single.subscribe(resp -> { assertEquals(200, resp.statusCode()); assertEquals(new WineAndCheese().setCheese("Goat Cheese").setWine("Condrieu"), resp.body()); testComplete(); }, this::fail); }); await(); } finally { server.close(); } }
/** * Like {@link io.vertx.rxjava.core.http.HttpServer#listen} but supplying a handler that will be called when the server is actually listening (or has failed). * @return * @deprecated use {@link #rxListen} instead */ @Deprecated() public Observable<io.vertx.rxjava.core.http.HttpServer> listenObservable() { io.vertx.rx.java.ObservableFuture<io.vertx.rxjava.core.http.HttpServer> listenHandler = io.vertx.rx.java.RxHelper.observableFuture(); listen(listenHandler.toHandler()); return listenHandler; }
@Test public void testWebsocketClient() { HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080)); server.websocketStream().handler(ws -> { ws.write(Buffer.buffer("some_content")); ws.close(); }); server.listen(ar -> { HttpClient client = vertx.createHttpClient(new HttpClientOptions()); client.websocket(8080, "localhost", "/the_uri", ws -> { Buffer content = Buffer.buffer(); Observable<Buffer> observable = ws.toObservable(); observable.forEach(content::appendBuffer, err -> fail(), () -> { server.close(); assertEquals("some_content", content.toString("UTF-8")); testComplete(); }); }); }); await(); }
/** * Like {@link io.vertx.rxjava.core.http.HttpServer#close} but supplying a handler that will be called when the server is actually closed (or has failed). * @return */ public Single<Void> rxClose() { return Single.create(new io.vertx.rx.java.SingleOnSubscribeAdapter<>(fut -> { close(fut); })); }
public static HttpServer newInstance(io.vertx.core.http.HttpServer arg) { return arg != null ? new HttpServer(arg) : null; } }
@Override public void start() throws Exception { HttpServer s1 = vertx.createHttpServer(new HttpServerOptions().setPort(8080)).requestHandler(req -> { }); HttpServer s2 = vertx.createHttpServer(new HttpServerOptions().setPort(8081)).requestHandler(req -> { }); Single<HttpServer> f1 = s1.rxListen(); Single<HttpServer> f2 = s2.rxListen(); Action1<HttpServer> done = server -> { if (count.incrementAndGet() == 2) { testComplete(); } }; f1.subscribe(done); f2.subscribe(done); } });
@Test public void testHttpClientFlatMap() { HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080)); server.requestStream().handler(req -> { req.response().setChunked(true).end("some_content"); }); server.listen(ar -> { HttpClient client = vertx.createHttpClient(new HttpClientOptions()); Single<HttpClientResponse> req = client.rxGetNow(8080, "localhost", "/the_uri"); Buffer content = Buffer.buffer(); req.flatMapObservable(HttpClientResponse::toObservable).forEach( content::appendBuffer, err -> fail(), () -> { server.close(); assertEquals("some_content", content.toString("UTF-8")); testComplete(); }); }); await(); }