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

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

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

HttpConnection.close介绍

[英]Close the connection and all the currently active streams.

An HTTP/2 connection will send a GOAWAY frame before.
[中]关闭连接和所有当前活动的流。
HTTP/2连接将在之前发送GOAWAY帧。

代码示例

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

@Test
public void testServerConnectionClose() throws Exception {
 // Test server connection close + client close handler
 server.requestHandler(req -> {
  req.connection().close();
 });
 CountDownLatch listenLatch = new CountDownLatch(1);
 server.listen(onSuccess(s -> listenLatch.countDown()));
 awaitLatch(listenLatch);
 client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onFailure(err -> {
 }))
  .connectionHandler(conn -> {
  conn.closeHandler(v -> {
   testComplete();
  });
 }).sendHead();
 await();
}

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

@Test
public void testHttpClientMetricsQueueClose() throws Exception {
 server = vertx.createHttpServer();
 List<Runnable> requests = Collections.synchronizedList(new ArrayList<>());
 server.requestHandler(req -> {
  requests.add(() -> {
   vertx.runOnContext(v -> {
    req.connection().close();
   });
  });
 });
 CountDownLatch listenLatch = new CountDownLatch(1);
 server.listen(8080, "localhost", onSuccess(s -> { listenLatch.countDown(); }));
 awaitLatch(listenLatch);
 client = vertx.createHttpClient();
 FakeHttpClientMetrics metrics = FakeHttpClientMetrics.getMetrics(client);
 for (int i = 0;i < 5;i++) {
  client.getNow(8080, "localhost", "/somepath", resp -> {
  });
 }
 assertWaitUntil(() -> requests.size() == 5);
 EndpointMetric endpoint = metrics.endpoint("localhost:8080");
 assertEquals(5, endpoint.connectionCount.get());
 ArrayList<Runnable> copy = new ArrayList<>(requests);
 requests.clear();
 copy.forEach(Runnable::run);
 assertWaitUntil(() -> metrics.endpoints().isEmpty());
 assertEquals(0, endpoint.connectionCount.get());
}

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

closed.countDown();
  });
  conn.close();
 });
}));

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

@Test
public void testConnectionCloseDuringShouldCallHandleExceptionOnlyOnce() throws Exception {
 server.requestHandler(req -> {
  vertx.setTimer(500, id -> {
   req.connection().close();
  });
 });
 AtomicInteger count = new AtomicInteger();
 startServer();
 HttpClientRequest post = client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", onFailure(res -> {}));
 post.setChunked(true);
 post.write(TestUtils.randomBuffer(10000));
 CountDownLatch latch = new CountDownLatch(1);
 post.exceptionHandler(x-> {
  count.incrementAndGet();
  vertx.setTimer(10, id -> {
   latch.countDown();
  });
 });
 // then stall until timeout and the exception handler will be called.
 awaitLatch(latch);
 assertEquals(count.get(), 1);
}

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

@Test
public void testClientConnectionClose() throws Exception {
 // Test client connection close + server close handler
 CountDownLatch latch = new CountDownLatch(1);
 server.requestHandler(req -> {
  AtomicInteger len = new AtomicInteger();
  req.handler(buff -> {
   if (len.addAndGet(buff.length()) == 1024) {
    latch.countDown();
   }
  });
  req.connection().closeHandler(v -> {
   testComplete();
  });
 });
 CountDownLatch listenLatch = new CountDownLatch(1);
 server.listen(onSuccess(s -> listenLatch.countDown()));
 awaitLatch(listenLatch);
 HttpClientRequest req = client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onFailure(err -> {}));
 req.setChunked(true);
 req.write(TestUtils.randomBuffer(1024));
 awaitLatch(latch);
 req.connection().close();
 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

protected void testCloseHandlerNotCalledWhenConnectionClosedAfterEnd(int expected) throws Exception {
 AtomicInteger closeCount = new AtomicInteger();
 AtomicInteger endCount = new AtomicInteger();
 server.requestHandler(req -> {
  req.response().closeHandler(v -> {
   closeCount.incrementAndGet();
  });
  req.response().endHandler(v -> {
   endCount.incrementAndGet();
  });
  req.connection().closeHandler(v -> {
   assertEquals(expected, closeCount.get());
   assertEquals(1, endCount.get());
   testComplete();
  });
  req.response().end("some-data");
 });
 startServer();
 client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onSuccess(resp -> {
  resp.endHandler(v -> {
   resp.request().connection().close();
  });
 }));
 await();
}

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

resp.endHandler(v -> {
  assertOnIOContext(ctx);
  resp.request().connection().close();
 });
}));

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

@Test
public void testPoolNotExpiring() throws Exception {
 AtomicLong now = new AtomicLong();
 server.requestHandler(req -> {
  req.response().end();
  now.set(System.currentTimeMillis());
  vertx.setTimer(2000, id -> {
   req.connection().close();
  });
 });
 startServer();
 client.close();
 client = vertx.createHttpClient(new HttpClientOptions().setPoolCleanerPeriod(0).setKeepAliveTimeout(100));
 client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> {
  resp.endHandler(v1 -> {
   resp.request().connection().closeHandler(v2 -> {
    long time = System.currentTimeMillis() - now.get();
    assertTrue("Was expecting " + time + " to be > 2000", time >= 2000);
    testComplete();
   });
  });
 }));
 await();
}

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

@Test
public void testClientRequestExceptionHandlerCalledWhenConnectionClosed() throws Exception {
 server.requestHandler(req -> {
  req.handler(buff -> {
   req.connection().close();
  });
 });
 startServer();
 HttpClientRequest req = client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onFailure(err -> {})).setChunked(true);
 req.exceptionHandler(err -> {
  testComplete();
 });
 req.write("chunk");
 await();
}

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

req.connection().close();
 });
});

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

complete();
 resp.request().connection().close();
} else if (err instanceof DecompressionException) {
 complete();

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

@Test
public void testHttpServerRequestShouldExceptionHandlerWhenTheClosedHandlerIsCalled() {
 server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).requestHandler(req -> {
  HttpServerResponse resp = req.response();
  resp.setChunked(true);
  CheckingSender sender = new CheckingSender(vertx.getOrCreateContext(), resp);
  sender.send();
  resp.closeHandler(v -> {
   Throwable failure = sender.close();
   if (failure != null) {
    fail(failure);
   } else {
    testComplete();
   }
  });
 });
 server.listen(onSuccess(s -> {
  client.getNow(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/someuri", onSuccess(resp -> {
   vertx.setTimer(1000, id -> {
    resp.request().connection().close();
   });
  }));
 }));
 await();
}

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

complete();
 });
 conn.close();
}));
req.exceptionHandler(err ->{

代码示例来源: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();
}

代码示例来源:origin: io.vertx/vertx-core

@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", resp -> {
  resp.handler(buff -> {
   conn.get().close();
  });
  resp.exceptionHandler(err -> {
   testComplete();
  });
 });
 await();
}

代码示例来源:origin: io.vertx/vertx-core

@Test
public void testClientRequestExceptionHandlerCalledWhenConnectionClosed() throws Exception {
 server.requestHandler(req -> {
  req.handler(buff -> {
   req.connection().close();
  });
 });
 startServer();
 HttpClientRequest req = client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
  resp.handler(chunk -> {
   resp.request().connection().close();
  });
 }).setChunked(true);
 req.exceptionHandler(err -> {
  testComplete();
 });
 req.write("chunk");
 await();
}

代码示例来源:origin: io.vertx/vertx-core

@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", resp -> {
  resp.handler(v -> {
   resp.request().connection().close();
  });
 });
 await();
}

代码示例来源:origin: io.vertx/vertx-core

@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", resp -> {
  resp.handler(v -> {
   resp.request().connection().close();
  });
 });
 await();
}

相关文章