use of io.vertx.core.http.HttpServer in project vert.x by eclipse.
the class HostnameResolutionTest method testHttp.
@Test
public void testHttp() throws Exception {
HttpClient client = vertx.createHttpClient();
HttpServer server = vertx.createHttpServer().requestHandler(req -> {
req.response().end("foo");
});
try {
CountDownLatch listenLatch = new CountDownLatch(1);
server.listen(8080, "vertx.io", onSuccess(s -> {
listenLatch.countDown();
}));
awaitLatch(listenLatch);
client.request(HttpMethod.GET, 8080, "vertx.io", "/somepath", onSuccess(req -> {
req.send(onSuccess(resp -> {
Buffer buffer = Buffer.buffer();
resp.handler(buffer::appendBuffer);
resp.endHandler(v -> {
assertEquals(Buffer.buffer("foo"), buffer);
testComplete();
});
}));
}));
await();
} finally {
client.close();
server.close();
}
}
use of io.vertx.core.http.HttpServer in project java-chassis by ServiceComb.
the class MockForRestServerVerticle method mockRestServerVerticle.
public void mockRestServerVerticle() {
final HttpServer server = Mockito.mock(HttpServer.class);
new MockUp<RestServerVerticle>() {
@Mock
private void startListen(HttpServer server, IpPort ipPort, Future<Void> startFuture) {
}
@Mock
private HttpServer createHttpServer(boolean isHttp_2) {
return server;
}
};
}
use of io.vertx.core.http.HttpServer in project java-chassis by ServiceComb.
the class RestServerVerticle method start.
@Override
public void start(Promise<Void> startPromise) throws Exception {
try {
super.start();
// 如果本地未配置地址,则表示不必监听,只需要作为客户端使用即可
if (endpointObject == null) {
LOGGER.warn("rest listen address is not configured, will not start.");
startPromise.complete();
return;
}
Router mainRouter = Router.router(vertx);
mountAccessLogHandler(mainRouter);
mountCorsHandler(mainRouter);
initDispatcher(mainRouter);
mountGlobalRestFailureHandler(mainRouter);
HttpServer httpServer = createHttpServer();
httpServer.requestHandler(mainRouter);
httpServer.connectionHandler(connection -> {
DefaultHttpServerMetrics serverMetrics = (DefaultHttpServerMetrics) ((ConnectionBase) connection).metrics();
DefaultServerEndpointMetric endpointMetric = serverMetrics.getEndpointMetric();
long connectedCount = endpointMetric.getCurrentConnectionCount();
int connectionLimit = DynamicPropertyFactory.getInstance().getIntProperty("servicecomb.rest.server.connection-limit", Integer.MAX_VALUE).get();
if (connectedCount > connectionLimit) {
connection.close();
endpointMetric.onRejectByConnectionLimit();
}
});
List<HttpServerExceptionHandler> httpServerExceptionHandlers = SPIServiceUtils.getAllService(HttpServerExceptionHandler.class);
httpServer.exceptionHandler(e -> {
if (e instanceof ClosedChannelException) {
// This is quite normal in between browser and ege, so do not print out.
LOGGER.debug("Unexpected error in server.{}", ExceptionUtils.getExceptionMessageWithoutTrace(e));
} else {
LOGGER.error("Unexpected error in server.{}", ExceptionUtils.getExceptionMessageWithoutTrace(e));
}
httpServerExceptionHandlers.forEach(httpServerExceptionHandler -> {
httpServerExceptionHandler.handle(e);
});
});
startListen(httpServer, startPromise);
} catch (Throwable e) {
// vert.x got some states that not print error and execute call back in VertexUtils.blockDeploy, we add a log our self.
LOGGER.error("", e);
throw e;
}
}
use of io.vertx.core.http.HttpServer in project chuidiang-ejemplos by chuidiang.
the class WebServerVerticle method start.
@Override
public void start() throws Exception {
HttpServer server = vertx.createHttpServer();
server.requestHandler(request -> {
LOG.info("Web request arrived");
if (request.path().endsWith("index.html")) {
request.response().putHeader("content-type", "text/html");
request.response().sendFile("src/main/webroot/index.html");
} else {
request.response().setChunked(true);
request.response().putHeader("content-type", "text/plain");
request.response().write("No such file!!");
request.response().setStatusCode(404);
request.response().end();
}
});
server.listen();
super.start();
}
use of io.vertx.core.http.HttpServer in project hono by eclipse.
the class AbstractVertxBasedHttpProtocolAdapterTest method getHttpServer.
@SuppressWarnings("unchecked")
private HttpServer getHttpServer(final boolean startupShouldFail) {
final HttpServer server = mock(HttpServer.class);
when(server.actualPort()).thenReturn(0, 8080);
when(server.requestHandler(VertxMockSupport.anyHandler())).thenReturn(server);
when(server.listen(VertxMockSupport.anyHandler())).then(invocation -> {
final Handler<AsyncResult<HttpServer>> handler = (Handler<AsyncResult<HttpServer>>) invocation.getArgument(0);
if (startupShouldFail) {
handler.handle(Future.failedFuture("http server intentionally failed to start"));
} else {
handler.handle(Future.succeededFuture(server));
}
return server;
});
return server;
}
Aggregations