io.vertx.core.http.HttpClient.getNow()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(10.5k)|赞(0)|评价(0)|浏览(156)

本文整理了Java中io.vertx.core.http.HttpClient.getNow()方法的一些代码示例,展示了HttpClient.getNow()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpClient.getNow()方法的具体详情如下:
包路径:io.vertx.core.http.HttpClient
类名称:HttpClient
方法名:getNow

HttpClient.getNow介绍

[英]Sends an HTTP GET request to the server at the specified host and port, specifying a response handler to receive the response
[中]在指定的主机和端口向服务器发送HTTP GET请求,并指定接收响应的响应处理程序

代码示例

代码示例来源:origin: eclipse-vertx/vert.x

private void recursiveCall(HttpClient client, AtomicInteger receivedRequests, int sendRequests){
 client.getNow("/", r -> {
  int numRequests = receivedRequests.incrementAndGet();
  if (numRequests == sendRequests) {
   testComplete();
  } else if (numRequests < sendRequests) {
   recursiveCall(client, receivedRequests, sendRequests);
  }
 });
}

代码示例来源:origin: eclipse-vertx/vert.x

private void testHttpClientResponseThrowsExceptionInHandler(
 String chunk,
 BiConsumer<HttpClientResponse, CountDownLatch> handler) throws Exception {
 server.requestHandler(req -> {
  HttpServerResponse resp = req.response();
  if (chunk != null) {
   resp.end(chunk);
  } else {
   resp.end();
  }
 });
 startServer();
 int num = 50;
 CountDownLatch latch = new CountDownLatch(num);
 for (int i = 0;i < num;i++) {
  client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/someuri", onSuccess(resp -> {
   handler.accept(resp, latch);
  }));
 }
 awaitLatch(latch);
}

代码示例来源:origin: eclipse-vertx/vert.x

private TestLoggerFactory testLogging() throws Exception {
 return TestUtils.testLogging(() -> {
    try {
   server.requestHandler(req -> {
    req.response().end();
   });
   startServer();
   client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
    testComplete();
   });
   await();
    } catch (Exception e) {
     throw new RuntimeException(e);
    }
 });
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testClientConnectInvalidPort() {
 client.getNow(-1, "localhost", "/someuri", onFailure(err -> {
  assertEquals(err.getClass(), IllegalArgumentException.class);
  assertEquals(err.getMessage(), "port p must be in range 0 <= p <= 65535");
  testComplete();
 }));
 await();
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testDumpManyRequestsOnQueue() throws Exception {
 int sendRequests = 10000;
 AtomicInteger receivedRequests = new AtomicInteger();
 vertx.createHttpServer(createBaseServerOptions()).requestHandler(r-> {
  r.response().end();
  if (receivedRequests.incrementAndGet() == sendRequests) {
   testComplete();
  }
 }).listen(onSuccess(s -> {
  HttpClientOptions ops = createBaseClientOptions()
    .setDefaultPort(DEFAULT_HTTP_PORT)
    .setPipelining(true)
    .setKeepAlive(true);
  HttpClient client = vertx.createHttpClient(ops);
  IntStream.range(0, sendRequests).forEach(x -> client.getNow("/", r -> {}));
 }));
 await();
}

代码示例来源:origin: eclipse-vertx/vert.x

private void testHttpClientResponseDecodeError(Handler<Throwable> errorHandler) throws Exception {
 startServer();
 client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onSuccess(resp -> {
  resp.exceptionHandler(errorHandler);
 }));
 await();
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
 public void testNoRequestHandler() throws Exception {
  CountDownLatch latch = new CountDownLatch(1);
  vertx.createHttpServer()
   .websocketHandler(ws -> fail())
   .listen(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, onSuccess(v -> latch.countDown()));
  awaitLatch(latch);
  client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", onSuccess(resp -> {
   resp.endHandler(v -> {
    assertEquals(400, resp.statusCode());
    testComplete();
   });
  }));
  await();
 }
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testClientResponseExceptionHandlerCalledWhenConnectionClosed() throws Exception {
 AtomicReference<HttpConnection> conn = new AtomicReference<>();
 server.requestHandler(req -> {
  conn.set(req.connection());
  req.response().setChunked(true).write("chunk");
 });
 startServer();
 client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onSuccess(resp -> {
  resp.handler(buff -> {
   conn.get().close();
  });
  resp.exceptionHandler(err -> {
   testComplete();
  });
 }));
 await();
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testHttpClientResumeConnectionOnResponseOnLastMessage() throws Exception {
 server.requestHandler(req -> req.response().end("ok"));
 startServer();
 client.close();
 client = vertx.createHttpClient(new HttpClientOptions().setKeepAlive(true).setMaxPoolSize(1));
 client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp1 -> {
  resp1.pause();
  // The connection resume is asynchronous and the end message will be received before connection resume happens
  resp1.resume();
  client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp2 -> {
   testComplete();
  }));
 }));
 await();
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testServerActualPortWhenSet() {
 server
   .requestHandler(request -> {
    request.response().end("hello");
   })
   .listen(ar -> {
    assertEquals(ar.result().actualPort(), DEFAULT_HTTP_PORT);
    vertx.createHttpClient(createBaseClientOptions()).getNow(ar.result().actualPort(), DEFAULT_HTTP_HOST, "/", onSuccess(response -> {
     assertEquals(response.statusCode(), 200);
     response.bodyHandler(body -> {
      assertEquals(body.toString("UTF-8"), "hello");
      testComplete();
     });
    }));
   });
 await();
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testUseResponseAfterComplete() throws Exception {
 server.requestHandler(req -> {
  HttpServerResponse resp = req.response();
  assertFalse(resp.ended());
  resp.end();
  assertTrue(resp.ended());
  checkHttpServerResponse(resp);
  testComplete();
 });
 startServer();
 client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, noOpHandler());
 await();
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testServerActualPortWhenZero() {
 server = vertx.createHttpServer(createBaseServerOptions().setPort(0).setHost(DEFAULT_HTTP_HOST));
 server
   .requestHandler(request -> {
    request.response().end("hello");
   })
   .listen(ar -> {
    assertTrue(ar.result().actualPort() != 0);
    vertx.createHttpClient(createBaseClientOptions()).getNow(ar.result().actualPort(), DEFAULT_HTTP_HOST, "/", onSuccess(response -> {
     assertEquals(response.statusCode(), 200);
     response.bodyHandler(body -> {
      assertEquals(body.toString("UTF-8"), "hello");
      testComplete();
     });
    }));
   });
 await();
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testServerActualPortWhenZeroPassedInListen() {
 server = vertx.createHttpServer(new HttpServerOptions(createBaseServerOptions()).setHost(DEFAULT_HTTP_HOST));
 server
   .requestHandler(request -> {
    request.response().end("hello");
   })
   .listen(0, ar -> {
    assertTrue(ar.result().actualPort() != 0);
    vertx.createHttpClient(createBaseClientOptions()).getNow(ar.result().actualPort(), DEFAULT_HTTP_HOST, "/", onSuccess(response -> {
     assertEquals(response.statusCode(), 200);
     response.bodyHandler(body -> {
      assertEquals(body.toString("UTF-8"), "hello");
      testComplete();
     });
    }));
   });
 await();
}

代码示例来源:origin: eclipse-vertx/vert.x

private void testHttpClientResponsePause(Handler<HttpClientResponse> h) throws Exception {
 server.requestHandler(req -> req.response().end("ok"));
 startServer();
 client.close();
 client = vertx.createHttpClient(new HttpClientOptions().setMaxPoolSize(1).setKeepAlive(true));
 client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp1 -> {
  h.handle(resp1);
  vertx.setTimer(10, timerId -> {
   // The connection should be resumed as it's ended
   client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp2 -> {
    assertSame(resp1.request().connection(), resp2.request().connection());
    resp2.endHandler(v -> testComplete());
   }));
  });
 }));
 await();
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testSetWriteQueueMaxSize() throws Exception {
 server.requestHandler(req -> {
  HttpServerResponse resp = req.response();
  resp.setWriteQueueMaxSize(256 * 1024);
  // Now something bigger
  resp.setWriteQueueMaxSize(512 * 1024);
  // And something smaller again
  resp.setWriteQueueMaxSize(128 * 1024);
  resp.setWriteQueueMaxSize(129 * 1024);
  resp.end();
 }).listen(8080, onSuccess(s -> {
  client.getNow(8080, "localhost", "/", onSuccess(resp -> {
   assertEquals(200, resp.statusCode());
   testComplete();
  }));
 }));
 await();
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testUnknownContentLengthIsSetToZeroWithHTTP_1_0() throws Exception {
 server.requestHandler(req -> {
  req.response().write("Some-String").end();
 });
 startServer();
 client.close();
 client = vertx.createHttpClient(new HttpClientOptions().setProtocolVersion(HttpVersion.HTTP_1_0));
 client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> {
  assertNull(resp.getHeader("Content-Length"));
  testComplete();
 }));
 await();
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
 public void testServerConnectionClosed() throws Exception {
  server.close();
  server = vertx.createHttpServer(createBaseServerOptions().setIdleTimeout(2));
  server.requestHandler(req -> {
   FakeHttpServerMetrics metrics = FakeMetricsBase.getMetrics(server);
   HttpServerMetric metric = metrics.getMetric(req);
   assertNotNull(metric);
   assertFalse(metric.failed.get());
   req.response().closeHandler(v -> {
    assertNull(metrics.getMetric(req));
    assertTrue(metric.failed.get());
    testComplete();
   });
  });
  startServer();
  client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {});
  await();
 }
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testClientLocalAddress() throws Exception {
 String expectedAddress = TestUtils.loopbackAddress();
 client.close();
 client = vertx.createHttpClient(createBaseClientOptions().setLocalAddress(expectedAddress));
 server.requestHandler(req -> {
  assertEquals(expectedAddress, req.remoteAddress().host());
  req.response().end();
 });
 startServer();
 client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onSuccess(resp -> {
  assertEquals(200, resp.statusCode());
  testComplete();
 }));
 await();
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testCloseHandlerWhenConnectionEnds() throws Exception {
 server.requestHandler(req -> {
  req.response().closeHandler(v -> {
   testComplete();
  });
  req.response().setChunked(true).write("some-data");
 });
 startServer();
 client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onSuccess(resp -> {
  resp.handler(v -> {
   resp.request().connection().close();
  });
 }));
 await();
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testCloseHandlerWhenConnectionClose() throws Exception {
 server.requestHandler(req -> {
  HttpServerResponse resp = req.response();
  resp.setChunked(true).write("some-data");
  resp.closeHandler(v -> {
   checkHttpServerResponse(resp);
   testComplete();
  });
 });
 startServer();
 client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onSuccess(resp -> {
  resp.handler(v -> {
   resp.request().connection().close();
  });
 }));
 await();
}

相关文章