Java Code Examples for io.vertx.core.http.HttpServer#requestHandler()

The following examples show how to use io.vertx.core.http.HttpServer#requestHandler() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VertTest.java    From jdk-source-analysis with Apache License 2.0 8 votes vote down vote up
@Test
    public void test() {
        Vertx vertx = Vertx.vertx();
//        request.response().putHeader("Content-Type", "text/plain").write("some text").end();
        vertx.setPeriodic(1000, id -> {
            System.out.println(id);
        });
        vertx.executeBlocking(promise -> {

        }, asyncResult -> {

        });

        HttpServer server = vertx.createHttpServer();
        Handler<HttpServerRequest> requestHandler = server.requestHandler();
    }
 
Example 2
Source File: AdminVerticle.java    From quarantyne with Apache License 2.0 6 votes vote down vote up
public void start() {
  this.metricsService = MetricsService.create(vertx);

  HttpServer httpServer = vertx.createHttpServer();
  httpServer.requestHandler(req -> {
    if (req.path().equals(HEALTH_PATH)) {
      req.response().end("ok");
    } else if (req.path().equals(METRICS_PATH)) {
      publishMetricsSnapshot(req.response());
    } else {
      req.response().setStatusCode(404).end("HTTP 404");
    }
  });
  // we don't start this verticle unless this was defined
  httpServer.listen(ipPort.getPort(), ipPort.getIp(), h -> {
    if (h.failed()) {
      log.info("failed to start admin service", h.cause());
    }
  });
}
 
Example 3
Source File: App.java    From generator-jvm with MIT License 6 votes vote down vote up
public static void main(String[] args) {

    final HttpServer server = Vertx.vertx().createHttpServer();

    server.requestHandler(event -> {

      log.info("hendling request");

      final Map<String, String> o = HashMap.of("hello", "world",
                                               "ololo", "trololo")
                                           .toJavaMap();
      final String json = Try.of(() -> mapper.writeValueAsString(o))
                             .getOrElseGet(throwable -> "{}");
      event.response()
           .putHeader("Content-Type", "application/json")
           .end(json);
    });

    server.listen(port, event -> log.info("listening {} port.", port));
  }
 
Example 4
Source File: TestVertxMetersInitializer.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("deprecation")
// TODO: vert.x 3.8.3 does not update startListen to promise, so we keep use deprecated API now. update in newer version.
public void start(Future<Void> startFuture) {
  Router mainRouter = Router.router(vertx);
  mainRouter.route("/").handler(context -> {
    context.response().end(context.getBody());
  });

  HttpServer server = vertx.createHttpServer();
  server.requestHandler(mainRouter);
  server.listen(0, "0.0.0.0", ar -> {
    if (ar.succeeded()) {
      port = ar.result().actualPort();
      startFuture.complete();
      return;
    }

    startFuture.fail(ar.cause());
  });
}
 
Example 5
Source File: WebsiteMain.java    From examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    System.out.println("done waiting");

    Vertx vertx = Vertx.vertx();

    // Deploy
    DeploymentOptions options = new DeploymentOptions().setWorker(true);
    ZookeeperVerticle zkv = new ZookeeperVerticle();
    vertx.deployVerticle(zkv, options);

    HttpServer server = vertx.createHttpServer();
    server.requestHandler(request -> {
        HttpServerResponse response = request.response();
        response.putHeader("content-type", "application/json");
        JsonObject responseJson;
        synchronized (WebsiteMain.jsonObject) {
            responseJson = WebsiteMain.jsonObject.copy();
        }
        response.end(responseJson.encodePrettily());
    });
    server.listen(8080);
}
 
Example 6
Source File: Main.java    From mewbase with MIT License 6 votes vote down vote up
public static void main(String[] args) {
    String resourceBasename = "example.gettingstarted.projectionrest/configuration.conf";
    final Config config = ConfigFactory.load(resourceBasename);

    // create a Vertx web server
    final Vertx vertx = Vertx.vertx();
    final HttpServerOptions options = new HttpServerOptions().setPort(8080);
    final HttpServer httpServer = vertx.createHttpServer(options);
    final BinderStore binderStore = BinderStore.instance(config);
    final Router router = Router.router(vertx);
    router.route().handler(BodyHandler.create());
    httpServer.requestHandler(router::accept);

    /*
    Expose endpoint to retrieve a document from the binder store
     */
    router.route(HttpMethod.GET, "/summary/:product/:date").handler(routingContext -> {
        final String product = routingContext.pathParams().get("product");
        final String date = routingContext.pathParams().get("date");
        final VertxRestServiceActionVisitor actionVisitor = new VertxRestServiceActionVisitor(routingContext);
        actionVisitor.visit(RestServiceAction.retrieveSingleDocument(binderStore, "sales_summary", product + "_" + date));
    });

    httpServer.listen();
}
 
Example 7
Source File: Examples.java    From vertx-unit with Apache License 2.0 6 votes vote down vote up
public static void async_05(TestContext context, Vertx vertx, Handler<HttpServerRequest> requestHandler) {
  Async async = context.async(2);
  HttpServer server = vertx.createHttpServer();
  server.requestHandler(requestHandler);
  server.listen(8080, ar -> {
    context.assertTrue(ar.succeeded());
    async.countDown();
  });

  vertx.setTimer(1000, id -> {
    async.complete();
  });

  // Wait until completion of the timer and the http request
  async.awaitSuccess();

  // Do something else
}
 
Example 8
Source File: WebServerVerticle.java    From chuidiang-ejemplos with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void start() throws Exception {
   HttpServer server = vertx.createHttpServer();
   
   server.requestHandler(request -> {
      LOG.info("Web request arrived");
      
      
      if (request.path().endsWith("index.html")) {
         request.response().putHeader("content-type", "text/html");
         request.response().sendFile("src/main/webroot/index.html");
      } else {
         request.response().setChunked(true);
         request.response().putHeader("content-type", "text/plain");
         request.response().write("No such file!!");
         request.response().setStatusCode(404);
         request.response().end();
      }
   });
   
   server.listen();
   super.start();
}
 
Example 9
Source File: SimpleWebsiteMain.java    From examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    Vertx vertx = Vertx.vertx();
    HttpServer server = vertx.createHttpServer();
    server.requestHandler(request -> {
        HttpServerResponse response = request.response();
        response.putHeader("content-type", "application/json");
        JsonObject responseJson = SimpleWebsiteMain.jsonObject.copy();
        response.end(responseJson.encodePrettily());
    });
    server.listen(8080);
}
 
Example 10
Source File: HttpTermServerSubRouterTest.java    From vertx-shell with Apache License 2.0 5 votes vote down vote up
@Override
protected TermServer createServer(TestContext context, HttpTermOptions options) {
  HttpServer httpServer = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
  Router router = Router.router(vertx);
  Router subRouter = Router.router(vertx);
  router.mountSubRouter("/sub", subRouter);
  httpServer.requestHandler(router);
  Async async = context.async();
  httpServer.listen(8080, context.asyncAssertSuccess(s -> {
    async.complete();
  }));
  async.awaitSuccess(20000);
  return TermServer.createHttpTermServer(vertx, subRouter, options);
}
 
Example 11
Source File: Examples.java    From vertx-unit with Apache License 2.0 5 votes vote down vote up
public static void async_04(TestContext context, Vertx vertx, Handler<HttpServerRequest> requestHandler) {
  Async async = context.async();
  HttpServer server = vertx.createHttpServer();
  server.requestHandler(requestHandler);
  server.listen(8080, ar -> {
    context.assertTrue(ar.succeeded());
    async.complete();
  });

  // Wait until completion
  async.awaitSuccess();

  // Do something else
}
 
Example 12
Source File: SecurityTestUtils.java    From incubator-tuweni with Apache License 2.0 4 votes vote down vote up
static void configureAndStartTestServer(HttpServer httpServer) {
  httpServer.requestHandler(request -> {
    request.response().setStatusCode(200).end("OK");
  });
  startServer(httpServer);
}
 
Example 13
Source File: SecurityTestUtils.java    From cava with Apache License 2.0 4 votes vote down vote up
static void configureAndStartTestServer(HttpServer httpServer) {
  httpServer.requestHandler(request -> {
    request.response().setStatusCode(200).end("OK");
  });
  startServer(httpServer);
}