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

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

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

HttpConnection.closeHandler介绍

[英]Set a close handler. The handler will get notified when the connection is closed.
[中]设置一个关闭处理程序。当连接关闭时,处理程序将收到通知。

代码示例

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

resp.endHandler(v1 -> {
 HttpConnection conn = req.connection();
 conn.closeHandler(v2 -> {
  closed.countDown();
 });

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

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

assertEquals(1, respEndHandlerCount.incrementAndGet());
});
req.connection().closeHandler(v -> {
 assertEquals(1, reqExceptionHandlerCount.get());
 assertEquals(1, respExceptionHandlerCount.get());

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

代码示例来源: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 testDiscardConnectionWhenChannelBecomesInactive() throws Exception {
 AtomicInteger count = new AtomicInteger();
 server.requestHandler(req -> {
  if (count.getAndIncrement() == 0) {
   Http2ServerConnection a = (Http2ServerConnection) req.connection();
   SocketChannel channel = (SocketChannel) a.channel();
   channel.shutdown();
  } else {
   req.response().end();
  }
 });
 startServer();
 AtomicBoolean closed = new AtomicBoolean();
 client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onFailure(err -> {}))
  .connectionHandler(conn -> conn.closeHandler(v -> closed.set(true)))
  .end();
 AsyncTestBase.assertWaitUntil(closed::get);
 client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> {
  testComplete();
 }).exceptionHandler(err -> {
  fail();
 }).end();
 await();
}

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

@Test
public void testKeepAliveTimeout() throws Exception {
 server.requestHandler(req -> {
  req.response().end();
 });
 startServer();
 client.close();
 client = vertx.createHttpClient(createBaseClientOptions().setHttp2KeepAliveTimeout(3).setPoolCleanerPeriod(1));
 client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> {
  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();
}

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

AtomicLong time = new AtomicLong();
server.requestHandler(req -> {
 req.connection().closeHandler(v -> {
  complete();
 });
});
req.connectionHandler(conn -> {
 conn.closeHandler(v -> {
  assertTrue(System.currentTimeMillis() - time.get() > 1000);
  complete();

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

@Test
public void testDisableIdleTimeoutInPool() throws Exception {
 server.requestHandler(req -> {
  req.response().end();
 });
 startServer();
 client.close();
 client = vertx.createHttpClient(createBaseClientOptions()
  .setIdleTimeout(1)
  .setMaxPoolSize(1)
  .setKeepAliveTimeout(10)
 );
 client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> {
  resp.endHandler(v1 -> {
   AtomicBoolean closed = new AtomicBoolean();
   resp.request().connection().closeHandler(v2 -> {
    closed.set(true);
   });
   vertx.setTimer(2000, id -> {
    assertFalse(closed.get());
    testComplete();
   });
  });
 }));
 await();
}

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

assertEquals(2, serverStatus.getAndIncrement());
});
conn.closeHandler(v -> {
 assertEquals(4, serverStatus.getAndIncrement());
});

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

@Repeat(times = 10)
@Test
public void testCloseServerConnectionWithPendingMessages() throws Exception {
 int n = 5;
 server.requestHandler(req -> {
  vertx.setTimer(100, id -> {
   req.response().close();
  });
 });
 startServer();
 client.close();
 client = vertx.createHttpClient(new HttpClientOptions().setMaxPoolSize(n).setPipelining(true));
 AtomicBoolean completed = new AtomicBoolean();
 for (int i = 0; i < n * 2; i++) {
  client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onFailure(resp -> {
  }))
   .connectionHandler(conn -> {
    conn.closeHandler(v -> {
     if (completed.compareAndSet(false, true)) {
      testComplete();
     }
    });
   }).end();
 }
 await();
}

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

@Test
public void testRequestEntityTooLarge() {
 String path = "/some/path";
 server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).websocketHandler(ws -> fail());
 server.listen(onSuccess(ar -> {
  client.get(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTPS_HOST, path, onSuccess(resp -> {
   assertEquals(413, resp.statusCode());
   resp.request().connection().closeHandler(v -> {
    testComplete();
   });
  })).putHeader("Upgrade", "Websocket")
   .putHeader("Connection", "Upgrade")
   .end(TestUtils.randomBuffer(8192 + 1));
 }));
 await();
}

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

complete();
});
conn.closeHandler(err -> {
 assertFalse(Thread.holdsLock(conn));
 complete();
 complete();
});
conn.closeHandler(v -> {
 assertFalse(Thread.holdsLock(conn));
 complete();

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

conn.closeHandler(v -> {
 complete();
});

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

private void testServerMaxHeaderSize(int maxHeaderSize) {
 String longHeader = TestUtils.randomAlphaString(9000);
 vertx.createHttpServer(new HttpServerOptions().setMaxHeaderSize(maxHeaderSize)
   .setHost("localhost").setPort(8080)).requestHandler(req -> {
  assertEquals(req.getHeader("t"), longHeader);
  req.response().end();
 }).listen(onSuccess(res -> {
  HttpClientRequest req = vertx.createHttpClient(new HttpClientOptions())
    .request(HttpMethod.GET, 8080, "localhost", "/",
     onSuccess(resp -> {
      if (maxHeaderSize > HttpServerOptions.DEFAULT_MAX_HEADER_SIZE) {
       assertEquals(200, resp.statusCode());
       testComplete();
      } else {
       assertEquals(400, resp.statusCode());
       resp.request().connection().closeHandler(v -> {
        testComplete();
       });
      }
     }));
  // Add longHeader
  req.putHeader("t", longHeader);
  req.end();
 }));
 await();
}

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

private void testServerMaxInitialLineLength(int maxInitialLength) {
 String longParam = TestUtils.randomAlphaString(5000);
 server.close();
 server = vertx.createHttpServer(new HttpServerOptions().setMaxInitialLineLength(maxInitialLength)
   .setHost("localhost").setPort(8080)).requestHandler(req -> {
  assertEquals(req.getParam("t"), longParam);
  req.response().end();
 }).listen(onSuccess(res -> {
  HttpClientRequest req = vertx.createHttpClient(new HttpClientOptions())
    .request(HttpMethod.GET, 8080, "localhost", "/?t=" + longParam,
     onSuccess(resp -> {
      if (maxInitialLength > HttpServerOptions.DEFAULT_MAX_INITIAL_LINE_LENGTH) {
       assertEquals(200, resp.statusCode());
       testComplete();
      } else {
       assertEquals(414, resp.statusCode());
       resp.request().connection().closeHandler(v -> {
        testComplete();
       });
      }
     }));
  req.end();
 }));
 await();
}

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

server = vertx.createHttpServer(serverOptions);
server.requestHandler(req -> {
 req.connection().closeHandler(v -> {
  complete();
 });
 });
 req.connectionHandler(conn -> {
  conn.closeHandler(v2 -> {
   assertSame(ctx, Vertx.currentContext());
   complete();

相关文章