io.vertx.core.http.HttpServer#requestHandler ( )源码实例Demo

下面列出了io.vertx.core.http.HttpServer#requestHandler ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: quarantyne   文件: AdminVerticle.java
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());
    }
  });
}
 
源代码2 项目: generator-jvm   文件: App.java
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));
  }
 
@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());
  });
}
 
源代码4 项目: examples   文件: WebsiteMain.java
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);
}
 
源代码5 项目: mewbase   文件: Main.java
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();
}
 
源代码6 项目: jdk-source-analysis   文件: VertTest.java
@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();
    }
 
源代码7 项目: vertx-unit   文件: Examples.java
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
}
 
源代码8 项目: chuidiang-ejemplos   文件: WebServerVerticle.java
@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();
}
 
源代码9 项目: examples   文件: SimpleWebsiteMain.java
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);
}
 
源代码10 项目: vertx-shell   文件: HttpTermServerSubRouterTest.java
@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);
}
 
源代码11 项目: vertx-unit   文件: Examples.java
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
}
 
源代码12 项目: incubator-tuweni   文件: SecurityTestUtils.java
static void configureAndStartTestServer(HttpServer httpServer) {
  httpServer.requestHandler(request -> {
    request.response().setStatusCode(200).end("OK");
  });
  startServer(httpServer);
}
 
源代码13 项目: cava   文件: SecurityTestUtils.java
static void configureAndStartTestServer(HttpServer httpServer) {
  httpServer.requestHandler(request -> {
    request.response().setStatusCode(200).end("OK");
  });
  startServer(httpServer);
}