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

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

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

HttpClient.close介绍

[英]Close the client. Closing will close down any pooled connections. Clients should always be closed after use.
[中]关闭客户端。关闭将关闭所有池连接。客户端在使用后应始终关闭。

代码示例

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

protected void tearDown() throws Exception {
 if (client != null) {
  try {
   client.close();
  } catch (IllegalStateException ignore) {
   // Client was already closed by the test
  }
 }
 if (server != null) {
  CountDownLatch latch = new CountDownLatch(1);
  server.close((asyncResult) -> {
   assertTrue(asyncResult.succeeded());
   latch.countDown();
  });
  awaitLatch(latch);
 }
 super.tearDown();
}

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

@Test
public void testClientMakeRequestHttp2WithSSLWithoutAlpn() throws Exception {
 client.close();
 client = vertx.createHttpClient(createBaseClientOptions().setUseAlpn(false));
 try {
  client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI);
  fail();
 } catch (IllegalArgumentException ignore) {
  // Expected
 }
}

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

@Test
public void testClientWebsocketIdleTimeout() {
 client.close();
 client = vertx.createHttpClient(new HttpClientOptions().setIdleTimeout(1));
 server.websocketHandler(ws -> {}).listen(ar -> {
  client.websocket(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", ws -> {
   ws.closeHandler(v -> testComplete());
  });
 });
 await();
}

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

@Test
public void testClientOptionsCopiedBeforeUse() {
 client.close();
 server.requestHandler(req -> {
  req.response().end();
 });
 server.listen(ar -> {
  assertTrue(ar.succeeded());
  HttpClientOptions options = new HttpClientOptions();
  client = vertx.createHttpClient(options);
  // Now change something - but server should ignore this
  options.setSsl(true);
  client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/uri", onSuccess(res -> {
   assertEquals(200, res.statusCode());
   testComplete();
  })).end();
 });
 await();
}

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

@Test
public void testClientContextWithKeepAlive() throws Exception {
 client.close();
 client = vertx.createHttpClient(new HttpClientOptions().setKeepAlive(true).setPipelining(false).setMaxPoolSize(1));
 testClientContext();
}

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

@Test
public void testClientContextWithPipelining() throws Exception {
 client.close();
 client = vertx.createHttpClient(new HttpClientOptions().setKeepAlive(true).setPipelining(true).setMaxPoolSize(1));
 testClientContext();
}

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

@Test
public void testHttpClientName() throws Exception {
 HttpClient client1 = vertx.createHttpClient();
 try {
  FakeHttpClientMetrics metrics1 = FakeMetricsBase.getMetrics(client1);
  assertEquals("", metrics1.getName());
  String name = TestUtils.randomAlphaString(10);
  HttpClient client2 = vertx.createHttpClient(new HttpClientOptions().setMetricsName(name));
  try {
   FakeHttpClientMetrics metrics2 = FakeMetricsBase.getMetrics(client2);
   assertEquals(name, metrics2.getName());
  } finally {
   client2.close();
  }
 } finally {
  client1.close();
 }
}

代码示例来源: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 testClientLogging() throws Exception {
 client.close();
 client = vertx.createHttpClient(createBaseClientOptions().setLogActivity(true));
 TestLoggerFactory factory = testLogging();
 if (this instanceof Http1xTest) {
  assertTrue(factory.hasName("io.netty.handler.logging.LoggingHandler"));
 } else {
  assertTrue(factory.hasName("io.netty.handler.codec.http2.Http2FrameLogger"));
 }
}

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

@Test
public void testPerPeerPooling() throws Exception {
 client.close();
 client = vertx.createHttpClient(new HttpClientOptions()
   .setMaxPoolSize(1)
   .setKeepAlive(true)
   .setPipelining(false));
 testPerXXXPooling((i, handler) -> client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", handler).setHost("host" + i + ":8080"), HttpServerRequest::host);
}

代码示例来源: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

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 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 testPerHostPooling() throws Exception {
 client.close();
 client = vertx.createHttpClient(new HttpClientOptions()
  .setMaxPoolSize(1)
  .setKeepAlive(true)
  .setPipelining(false));
 testPerXXXPooling((i, handler) -> client.get(DEFAULT_HTTP_PORT, "host" + i, "/somepath", handler).setHost("host:8080")
  .putHeader("key", "host" + i), req -> req.getHeader("key"));
}

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

@Test
public void testResetPendingPushPromise() throws Exception {
 server.requestHandler(req -> {
  req.response().push(HttpMethod.GET, "/wibble", ar -> {
   assertFalse(ar.succeeded());
   testComplete();
  });
 });
 startServer();
 client.close();
 client = vertx.createHttpClient(clientOptions.setInitialSettings(new io.vertx.core.http.Http2Settings().setMaxConcurrentStreams(0L)));
 HttpClientRequest req = client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", onFailure(resp -> {}));
 req.pushHandler(pushedReq -> {
  pushedReq.reset(Http2Error.CANCEL.code());
 });
 req.end();
 await();
}

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

@Test
public void testClientDoesNotSupportAlpn() throws Exception {
 waitFor(2);
 server.requestHandler(req -> {
  assertEquals(HttpVersion.HTTP_1_1, req.version());
  req.response().end();
  complete();
 });
 startServer();
 client.close();
 client = vertx.createHttpClient(createBaseClientOptions().setProtocolVersion(HttpVersion.HTTP_1_1).setUseAlpn(false));
 client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> {
  assertEquals(HttpVersion.HTTP_1_1, resp.version());
  complete();
 })).exceptionHandler(this::fail).end();
 await();
}

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

@Test
public void testHttpProxyRequest() throws Exception {
 startProxy(null, ProxyType.HTTP);
 client.close();
 client = vertx.createHttpClient(new HttpClientOptions()
   .setProxyOptions(new ProxyOptions().setType(ProxyType.HTTP).setHost("localhost").setPort(proxy.getPort())));
 testHttpProxyRequest2(handler -> client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", handler));
}

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

@Test
public void testPerPeerPoolingWithProxy() throws Exception {
 client.close();
 client = vertx.createHttpClient(new HttpClientOptions()
   .setMaxPoolSize(1)
   .setKeepAlive(true)
   .setPipelining(false).setProxyOptions(new ProxyOptions()
     .setType(ProxyType.HTTP)
     .setHost(DEFAULT_HTTP_HOST)
     .setPort(DEFAULT_HTTP_PORT)));
 testPerXXXPooling((i, handler) -> client.get(80, "host" + i, "/somepath", handler), HttpServerRequest::host);
}

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

@Test
public void testHttpSocksProxyRequestAuth() throws Exception {
 startProxy("user", ProxyType.SOCKS5);
 client.close();
 client = vertx.createHttpClient(new HttpClientOptions()
   .setProxyOptions(new ProxyOptions().setType(ProxyType.SOCKS5).setHost("localhost").setPort(proxy.getPort())
     .setUsername("user").setPassword("user")));
 server.requestHandler(req -> {
  req.response().end();
 });
 server.listen(onSuccess(s -> {
  client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", onSuccess(resp -> {
   assertEquals(200, resp.statusCode());
   assertNotNull("request did not go through proxy", proxy.getLastUri());
   testComplete();
  })).exceptionHandler(th -> fail(th)).end();
 }));
 await();
}

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

@Test
public void testHttpProxyRequestOverrideClientSsl() throws Exception {
 startProxy(null, ProxyType.HTTP);
 client.close();
 client = vertx.createHttpClient(new HttpClientOptions()
   .setSsl(true).setProxyOptions(new ProxyOptions().setType(ProxyType.HTTP).setHost("localhost").setPort(proxy.getPort())));
 testHttpProxyRequest2(handler -> client.get(new RequestOptions().setSsl(false).setHost("localhost").setPort(8080), handler));
}

相关文章