io.vertx.core.http.HttpConnection类的使用及代码示例

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

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

HttpConnection介绍

[英]Represents an HTTP connection.

HTTP/1.x connection provides an limited implementation, the following methods are implemented:

  • #close
  • #closeHandler
  • #exceptionHandler
    [中]表示HTTP连接。
    HTTP/1。x连接提供了有限的实现,实现了以下方法:
    *#关闭
    *#closeHandler
    *#例外处理程序

代码示例

代码示例来源: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 testClientConnectionExceptionHandler() throws Exception {
 server.requestHandler(req -> {
  NetSocket so = req.netSocket();
  so.write(Buffer.buffer(TestUtils.randomAlphaString(40) + "\r\n"));
 });
 CountDownLatch listenLatch = new CountDownLatch(1);
 server.listen(onSuccess(s -> listenLatch.countDown()));
 awaitLatch(listenLatch);
 HttpClientRequest req = client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
 });
 req.connectionHandler(conn -> {
  conn.exceptionHandler(err -> {
   testComplete();
  });
 });
 req.sendHead();
 await();
}

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

/**
 * Like {@link #goAway(long, int)} with a last stream id {@code -1} which means to disallow any new stream creation.
 */
@Fluent
default HttpConnection goAway(long errorCode) {
 return goAway(errorCode, -1);
}

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

server.connectionHandler(conn -> {
 if (serverStatus.getAndIncrement() == 0) {
  conn.goAwayHandler(ga -> {
   assertEquals(0, ga.getErrorCode());
   assertEquals(1, serverStatus.getAndIncrement());
  });
  conn.shutdownHandler(v -> {
   assertEquals(2, serverStatus.getAndIncrement());
  });
  conn.closeHandler(v -> {
   assertEquals(4, serverStatus.getAndIncrement());
  });
 Context ctx = Vertx.currentContext();
 if (clientStatus.getAndIncrement() == 0) {
  conn.shutdownHandler(v -> {
   assertOnIOContext(ctx);
   clientStatus.compareAndSet(1, 2);
   complete();
  });
  conn.shutdown();

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

switch (reqCount.getAndIncrement()) {
  case 0:
   req.connection().goAway(0);
   break;
  case 1:
req.connectionHandler(conn -> {
 AtomicInteger gaCount = new AtomicInteger();
 conn.goAwayHandler(ga -> {
  if (gaCount.getAndIncrement() == 0) {
   client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp2 -> {

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

@Test
public void testIncorrectHttpVersion() throws Exception {
 server.requestHandler(req -> {
  NetSocket so = req.netSocket();
  so.write(Buffer.buffer("HTTP/1.2 200 OK\r\nContent-Length:5\r\n\r\nHELLO"));
  so.close();
 });
 startServer();
 AtomicBoolean a = new AtomicBoolean();
 HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onFailure(err -> {
  if (a.compareAndSet(false, true)) {
   assertTrue("message " + err.getMessage() + " should contain HTTP/1.2", err.getMessage().contains("HTTP/1.2"));
  }
 }));
 req.exceptionHandler(err -> {
  fail("Should not be called");
 }).putHeader("connection", "close")
  .connectionHandler(conn -> conn.closeHandler(v -> testComplete()))
  .end();
 await();
}

代码示例来源:origin: apache/servicecomb-java-chassis

clientRequest.connectionHandler(connection -> {
 LOGGER.debug("http connection connected, local:{}, remote:{}.",
   connection.localAddress(),
   connection.remoteAddress());
 connection.closeHandler(v -> {
  LOGGER.debug("http connection closed, local:{}, remote:{}.",
    connection.localAddress(),
    connection.remoteAddress());
 });
 connection.exceptionHandler(e -> {
  LOGGER.info("http connection exception, local:{}, remote:{}.",
    connection.localAddress(),
    connection.remoteAddress(),
    e);
 });

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

@Override
public void handle(RoutingContext rc) {
 HttpServerResponse response = rc.response();
 response
  .setChunked(true)
  .putHeader(HttpHeaders.CONTENT_TYPE, "text/event-stream")
  .putHeader(HttpHeaders.CACHE_CONTROL, "no-cache")
  .putHeader(HttpHeaders.CONNECTION, HttpHeaders.KEEP_ALIVE);
 rc.request().connection()
  .closeHandler(v -> {
   connections.remove(response);
   endQuietly(response);
  })
  .exceptionHandler(t -> {
   connections.remove(response);
   rc.fail(t);
  });
 connections.add(response);
}

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

});
}).connectionHandler(conn -> {
 io.vertx.core.http.Http2Settings initialRemoteSettings = conn.remoteSettings();
 assertEquals(initialSettings.isPushEnabled(), initialRemoteSettings.isPushEnabled());
 assertEquals(initialSettings.getMaxHeaderListSize(), initialRemoteSettings.getMaxHeaderListSize());
 assertEquals(initialSettings.get('\u0007'), initialRemoteSettings.get(7));
 Context ctx = Vertx.currentContext();
 conn.remoteSettingsHandler(settings -> {
  assertOnIOContext(ctx);
  switch (count.getAndIncrement()) {
}).exceptionHandler(this::fail).connectionHandler(conn -> {
 vertx.runOnContext(v -> {
  conn.updateSettings(updatedSettings, ar -> {
   end.complete();
  });

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

indicatedServerName = req.connection().indicatedServerName();
assertEquals(version, req.version());
assertEquals(serverSSL, req.isSSL());
  HttpClientResponse response = ar2.result();
  HttpConnection conn = response.request().connection();
  if (conn.isSsl()) {
   try {
    clientPeerCert = conn.peerCertificateChain()[0];
   } catch (SSLPeerUnverifiedException ignore) {

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

@Test
public void testInitialMaxConcurrentStreamZero() throws Exception {
 server.close();
 server = vertx.createHttpServer(createBaseServerOptions().setInitialSettings(new Http2Settings().setMaxConcurrentStreams(0)));
 server.requestHandler(req -> {
  req.response().end();
 });
 server.connectionHandler(conn -> {
  vertx.setTimer(500, id -> {
   conn.updateSettings(new Http2Settings().setMaxConcurrentStreams(10));
  });
 });
 startServer();
 client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> {
  testComplete();
 }).connectionHandler(conn -> {
  assertEquals(10, conn.remoteSettings().getMaxConcurrentStreams());
 }).setTimeout(10000).exceptionHandler(this::fail).end();
 await();
}

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

server.connectionHandler(conn -> {
 otherContext.runOnContext(v -> {
  conn.updateSettings(expectedSettings);
 });
});
AtomicInteger count = new AtomicInteger();
client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", onFailure(resp -> {})).connectionHandler(conn -> {
 conn.remoteSettingsHandler(settings -> {
  switch (count.getAndIncrement()) {
   case 0:

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

/**
 * Send to the remote endpoint an update of the server settings.
 * <p/>
 * This is not implemented for HTTP/1.x.
 * @param settings the new settings
 * @return a reference to this, so the API can be used fluently
 */
public io.vertx.rxjava.core.http.HttpConnection updateSettings(Http2Settings settings) { 
 delegate.updateSettings(settings);
 return this;
}

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

@Test
public void testServerShutdownConnection() throws Exception {
 waitFor(2);
 server.connectionHandler(HttpConnection::shutdown);
 server.requestHandler(req -> fail());
 startServer();
 AtomicInteger count = new AtomicInteger();
 HttpClientRequest req1 = client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath",
  onFailure(err -> {
   if (count.getAndIncrement() == 0) {
    complete();
   }
  }));
 req1.connectionHandler(conn -> {
  Context ctx = Vertx.currentContext();
  conn.goAwayHandler(ga -> {
   assertOnIOContext(ctx);
   complete();
  });
 });
 req1.end();
 await();
}

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

client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> {
}).connectionHandler(conn -> {
 assertEquals(max == null ? 0xFFFFFFFFL : max, conn.remoteSettings().getMaxConcurrentStreams());
 latch.countDown();
}).exceptionHandler(err -> {

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

/**
 * Set an handler that is called when remote endpoint {@link io.vertx.core.http.Http2Settings} are updated.
 * <p/>
 * This is not implemented for HTTP/1.x.
 * @param handler the handler for remote endpoint settings
 * @return a reference to this, so the API can be used fluently
 */
public io.vertx.rxjava.core.http.HttpConnection remoteSettingsHandler(Handler<Http2Settings> handler) { 
 delegate.remoteSettingsHandler(handler);
 return this;
}

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

/**
 * Returns the SNI server name presented during the SSL handshake by the client.
 * @return the indicated server name
 */
public String indicatedServerName() { 
 String ret = delegate.indicatedServerName();
 return ret;
}

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

private void testKeepAliveTimeout(HttpClientOptions options, int numReqs) throws Exception {
 startServer();
 client.close();
 client = vertx.createHttpClient(options.setPoolCleanerPeriod(1));
 AtomicInteger respCount = new AtomicInteger();
 for (int i = 0;i < numReqs;i++) {
  int current = 1 + i;
  client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> {
   respCount.incrementAndGet();
   if (current == numReqs) {
    long now = System.currentTimeMillis();
    resp.request().connection().closeHandler(v -> {
     long timeout = System.currentTimeMillis() - now;
     int delta = 500;
     int low = 3000 - delta;
     int high = 3000 + delta;
     assertTrue("Expected actual close timeout " + timeout + " to be > " + low, low < timeout);
     assertTrue("Expected actual close timeout " + timeout + " + to be < " + high, timeout < high);
     testComplete();
    });
   }
  }));
 }
 await();
}

相关文章