@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 { 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(Future<Void> startFuture) { httpServer.requestHandler(request -> reactor.route().accept(request)); httpServer.listen(res -> { if (res.succeeded()) { logger.info("HTTP Server is now listening for requests on port {}", httpServerConfiguration.getPort()); startFuture.complete(); } else { logger.error("Unable to start HTTP Server", res.cause()); startFuture.fail(res.cause()); } }); }
/** * Create an HTTP server for the REST service. * * @param router router instance * @param host server host * @param port server port * @return asynchronous result */ protected Completable createHttpServer(Router router, String host, int port) { return vertx.createHttpServer() .requestHandler(router::accept) .rxListen(port, host) .toCompletable(); }
private Completable createHttpServer(JsonObject config, Router router) { return vertx .createHttpServer() .requestHandler(router::accept) .rxListen(config.getInteger("HTTP_PORT", 8080)) .toCompletable(); }
@Override public void start() throws Exception { vertx.createHttpServer() .requestHandler(req -> { String name = req.getParam("name"); if (name == null) { name = "world"; } // Send a message on the event bus using the `send` method. Pass a reply handler receiving the // response. As the expected object is a Json structure, you can use `vertx.eventBus() // .<JsonObject>send(...`). // Unlike in the previous exercise, we use the `rxSend` method to retrieve a `Single` stream. We then // _map_ the result to extract the (encoded as String) Json structure. // In RX, we must `subscribe` to the stream to trigger the processing. Without nothing happens. There // are several `subscribe` method, but here we recommend the `BiConsumer` format `(res, err) -> ...` // If it's a failure (err != null), write a 500 HTTP response with the cause (`err.getMessage()`) as // payload. On success, write the body (`res`) into the HTTP response. // TODO }) .listen(8080); } }
@Override public void start() { ServiceDiscovery.create(vertx, discovery -> { this.discovery = discovery; // Create the service object PortfolioServiceImpl service = new PortfolioServiceImpl(vertx, discovery, config().getDouble("money", 10000.00)); // Register the service proxy on the event bus ProxyHelper.registerService(PortfolioService.class, vertx.getDelegate(), service, ADDRESS); Record record = EventBusService.createRecord("portfolio", ADDRESS, PortfolioService.class.getName()); discovery.publish(record, ar -> { if (ar.succeeded()) { this.record = record; System.out.println("Portfolio service published"); // Used for health check vertx.createHttpServer().requestHandler(req -> req.response().end("OK")).listen(8080); } else { ar.cause().printStackTrace(); } }); }); }
@Override public void start() { /** 1.Call router hub to mount commont **/ final Axis<Router> routerAxiser = Fn.poolThread(Pool.ROUTERS, () -> Ut.instance(RouterAxis.class)); /** 2.Call route hub to mount defined **/ final Axis<Router> axiser = Fn.poolThread(Pool.EVENTS, () -> Ut.instance(EventAxis.class)); /** 3.Get the default HttpServer Options **/ ZeroAtomic.RX_OPTS.forEach((port, option) -> { /** 3.1.Single server processing **/ final HttpServer server = this.vertx.createHttpServer(option); /** 3.2. Build router with current option **/ final Router router = Router.router(this.vertx); routerAxiser.mount(router); axiser.mount(router); /** 3.3. Listen for router on the server **/ final Single<HttpServer> result = server.requestHandler(router::accept).rxListen(); /** 3.4. Log output **/ { result.subscribe((rxServer) -> { this.recordServer(option, router); }); } }); }
@Override public void start() { /** 1.Call router hub to mount commont **/ final Axis<Router> routerAxiser = Fn.poolThread(Pool.ROUTERS, () -> Ut.instance(RouterAxis.class)); /** 2.Call route hub to mount defined **/ final Axis<Router> axiser = Fn.poolThread(Pool.EVENTS, () -> Ut.instance(EventAxis.class)); /** 3.Get the default HttpServer Options **/ ZeroAtomic.RX_OPTS.forEach((port, option) -> { /** 3.1.Single server processing **/ final HttpServer server = this.vertx.createHttpServer(option); /** 3.2. Build router with current option **/ final Router router = Router.router(this.vertx); routerAxiser.mount(router); axiser.mount(router); /** 3.3. Listen for router on the server **/ final Single<HttpServer> result = server.requestHandler(router::accept).rxListen(); /** 3.4. Log output **/ { result.subscribe((rxServer) -> { this.recordServer(option, router); }); } }); }
private Completable startVertx(VertxResteasyDeployment deployment) { return Completable.defer(() -> { Router router = Router.router(vertx); AppGlobals globals = AppGlobals.get(); globals.setRouter(router); VertxPluginRequestHandler resteasyHandler = new VertxPluginRequestHandler(vertx, deployment, plugins); return doOnPlugins(plugin -> plugin.preRoute()) .doOnComplete(() -> { setupRoutes(router); router.route().handler(routingContext -> { ResteasyProviderFactory.pushContext(RoutingContext.class, routingContext); ResteasyProviderFactory.pushContext(io.vertx.rxjava.ext.web.RoutingContext.class, io.vertx.rxjava.ext.web.RoutingContext.newInstance(routingContext.getDelegate())); resteasyHandler.handle(routingContext.request()); }); }).concatWith(doOnPlugins(plugin -> plugin.postRoute())) .concatWith(Completable.defer(() -> { // Start the front end server using the Jax-RS controller int port = globals.getConfig().getInteger("http_port", 9000); String host = globals.getConfig().getString("http_host", NetServerOptions.DEFAULT_HOST); return vertx.createHttpServer() .requestHandler(router::accept) .rxListen(port, host) .doOnSuccess(server -> System.out.println("Server started on port " + server.actualPort())) .doOnError(t -> t.printStackTrace()) .ignoreElement(); })); }); }
private Completable startVertx(VertxResteasyDeployment deployment) { return Completable.defer(() -> { Router router = Router.router(vertx); AppGlobals globals = AppGlobals.get(); globals.setRouter(router); VertxPluginRequestHandler resteasyHandler = new VertxPluginRequestHandler(vertx, deployment, plugins); return doOnPlugins(plugin -> plugin.preRoute()) .doOnComplete(() -> { setupRoutes(router); router.route().handler(routingContext -> { ResteasyProviderFactory.pushContext(RoutingContext.class, routingContext); ResteasyProviderFactory.pushContext(io.vertx.rxjava.ext.web.RoutingContext.class, io.vertx.rxjava.ext.web.RoutingContext.newInstance(routingContext.getDelegate())); resteasyHandler.handle(routingContext.request()); }); }).concatWith(doOnPlugins(plugin -> plugin.postRoute())) .concatWith(Completable.defer(() -> { // Start the front end server using the Jax-RS controller int port = globals.getConfig().getInteger("http_port", 9000); String host = globals.getConfig().getString("http_host", NetServerOptions.DEFAULT_HOST); return vertx.createHttpServer() .requestHandler(router::accept) .rxListen(port, host) .doOnSuccess(server -> System.out.println("Server started on port " + server.actualPort())) .doOnError(t -> t.printStackTrace()) .ignoreElement(); })); }); }
@Override public void start() throws Exception { Router router = Router.router(vertx); router.get().handler(this::convertPortfolioToEuro); router.post().handler(BodyHandler.create()); router.post().handler(this::delegateWithCircuitBreaker); circuit = CircuitBreaker.create("circuit-breaker", vertx, new CircuitBreakerOptions() .setFallbackOnFailure(true) .setMaxFailures(3) .setResetTimeout(5000) .setTimeout(1000) ); discovery = ServiceDiscovery.create(vertx); vertx.createHttpServer() .requestHandler(router::accept) .listen(8080); }
@Override public void start(Future<Void> future) throws Exception { // Create a simple HTTP service (using Vert.x Web Router) and publish it in the service discovery. // As we want to complete the deployment when the service is exposed (asynchronous operation), we use a // `Future` argument to indicate when the deployment is completed. This allows deploying the other verticle // after the deployment completion of this one. // Create an instance of service discovery this.discovery = ServiceDiscovery.create(vertx); // Simple HTTP API using Vert.x Web Router. Router router = Router.router(vertx); router.get("/").handler(rc -> rc.response().end("OK")); router.get("/greetings").handler(rc -> rc.response().end("Hello world")); router.get("/greetings/:name").handler(rc -> rc.response().end("Hello " + rc.pathParam("name"))); vertx.createHttpServer() .requestHandler(router::accept) .rxListen(8080) // When the server is ready, we publish the service .flatMap(this::publish) // Store the record, required to un-publish it .doOnSuccess(rec -> this.record = rec) .toCompletable() .subscribe(toObserver(future)); }
.requestHandler(request -> { try { router.accept(request);
} else { httpServer .requestHandler(req -> routeSafe(req, router));
} else { httpServer .requestHandler(req -> routeSafe(req, router));
.requestHandler(router::accept) .rxListen(8080) .ignoreElement();