本文整理汇总了Java中io.vertx.core.http.HttpServer.requestHandler方法的典型用法代码示例。如果您正苦于以下问题:Java HttpServer.requestHandler方法的具体用法?Java HttpServer.requestHandler怎么用?Java HttpServer.requestHandler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.vertx.core.http.HttpServer
的用法示例。
在下文中一共展示了HttpServer.requestHandler方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: start
import io.vertx.core.http.HttpServer; //导入方法依赖的package包/类
@Override
public void start(Future<Void> startFuture) throws Exception {
super.start();
// 如果本地未配置地址,则表示不必监听,只需要作为客户端使用即可
if (endpointObject == null) {
LOGGER.warn("rest listen address is not configured, will not start.");
startFuture.complete();
return;
}
Router mainRouter = Router.router(vertx);
mountAccessLogHandler(mainRouter);
initDispatcher(mainRouter);
HttpServer httpServer = createHttpServer();
httpServer.requestHandler(mainRouter::accept);
startListen(httpServer, startFuture);
}
示例2: testSyncAndWaitFiveSecondsForTimeOut
import io.vertx.core.http.HttpServer; //导入方法依赖的package包/类
@Test
public void testSyncAndWaitFiveSecondsForTimeOut() throws Exception {
// Create a server that will take a long time to reply to a request.
final HttpServer timeOutHttpServer = vertx.createHttpServer();
timeOutHttpServer.requestHandler(
e -> {
try {
Thread.sleep(100000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
});
timeOutHttpServer.listen(TIMEOUT_SERVER_PORT);
// Send a request synchronously and wait 5 seconds for a response.
doSync(e -> vertx.createHttpClient().getNow(TIMEOUT_SERVER_PORT, HOST, URI, e::complete), 5);
}
示例3: async_05
import io.vertx.core.http.HttpServer; //导入方法依赖的package包/类
public static void async_05(TestContext context, Vertx vertx, Handler<HttpServerRequest> requestHandler) {
Async async = context.async(2);
HttpServer server = vertx.createHttpServer();
server.requestHandler(requestHandler);
server.listen(8080, ar -> {
context.assertTrue(ar.succeeded());
async.countDown();
});
vertx.setTimer(1000, id -> {
async.complete();
});
// Wait until completion of the timer and the http request
async.awaitSuccess();
// Do something else
}
示例4: testRedirect
import io.vertx.core.http.HttpServer; //导入方法依赖的package包/类
private void testRedirect(TestContext context, int responseStatus) {
vertx = Vertx.vertx();
HttpServer redirectServer = vertx.createHttpServer();
redirectServer.requestHandler(req -> {
HttpServerResponse resp = req.response();
resp.setStatusCode(responseStatus);
resp.putHeader("Location", "http://localhost:8080/the_verticle.zip");
resp.end();
});
HttpServer server = new RepoBuilder().setVerticle(verticleWithMain).build();
redirectServer.listen(8081, context.asyncAssertSuccess(r -> {
server.listen(
8080,
context.asyncAssertSuccess(s -> {
vertx.deployVerticle("http://localhost:8081/the_verticle.zip", context.asyncAssertSuccess());
})
);
}));
}
示例5: start
import io.vertx.core.http.HttpServer; //导入方法依赖的package包/类
@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();
}
示例6: testTLS
import io.vertx.core.http.HttpServer; //导入方法依赖的package包/类
private void testTLS(WebClientOptions clientOptions, HttpServerOptions serverOptions, Function<WebClient, HttpRequest<Buffer>> requestProvider, Consumer<HttpServerRequest> serverAssertions) throws Exception {
WebClient sslClient = WebClient.create(vertx, clientOptions);
HttpServer sslServer = vertx.createHttpServer(serverOptions);
sslServer.requestHandler(req -> {
assertEquals(serverOptions.isSsl(), req.isSSL());
if (serverAssertions != null) {
serverAssertions.accept(req);
}
req.response().end();
});
try {
startServer(sslServer);
HttpRequest<Buffer> builder = requestProvider.apply(sslClient);
builder.send(onSuccess(resp -> testComplete()));
await();
} finally {
sslClient.close();
sslServer.close();
}
}
示例7: start
import io.vertx.core.http.HttpServer; //导入方法依赖的package包/类
public void start() throws Exception {
JsonObject conf = vertx.getOrCreateContext().config();
HttpServerOptions options = new HttpServerOptions();
options.setCompressionSupported(true);
HttpServer server = vertx.createHttpServer(options);
/*
<property name="inside.host">172.18.7.20</property>
<property name="inside.port">8999</property>
*/
String host = conf.getString("inside.host");
String port = conf.getString("inside.port");
if(S.isBlank(host) || S.isBlank(port))
return;
// ============初始化======================
this.upload_dir = conf.getString("upload.dir");
this.webclient = WebClient.create(vertx);
this.appContain = gsetting.getAppContain(vertx,this.webclient);
this.initRoutes();
server.requestHandler(router::accept);
server.listen(Integer.parseInt(port), host,ar -> {
if (ar.succeeded()) {
log.info("InsideServer listen on " + port);
} else {
log.error("InsideServer Failed to start!", ar.cause());
}
});
}
示例8: start
import io.vertx.core.http.HttpServer; //导入方法依赖的package包/类
@Override
public void start(Future<Void> startFuture) throws Exception {
HttpServer httpServer = vertx.createHttpServer();
Router router = Router.router(vertx);
Route route = router.route();
route.handler(BodyHandler.create());
route = router.route(HttpMethod.POST, "/actions");
route.handler(this::_handle);
httpServer.requestHandler(router::accept);
httpServer.listen(
_PORT,
result -> {
if (result.succeeded()) {
startFuture.complete();
}
else {
startFuture.fail(result.cause());
}
});
}
示例9: main
import io.vertx.core.http.HttpServer; //导入方法依赖的package包/类
public static void main(String[] args) {
System.out.println("Here we go!");
final Vertx vertx = Vertx.vertx();
HttpServer httpServer = vertx.createHttpServer();
httpServer.requestHandler(request -> {
System.out.println(request.absoluteURI());
System.out.println(request.method());
vertx.eventBus().send("latest-news", "Request Received to: " + request.path());
HttpServerResponse response = request.response();
response.end("Message Received");
});
httpServer.listen(8082, httpServerAsyncResult -> {
if (httpServerAsyncResult.succeeded()) {
System.out.println("Server is bound: " + httpServerAsyncResult.result().actualPort());
} else if (httpServerAsyncResult.failed()) {
System.out.println("Unable to set up server");
httpServerAsyncResult.cause().printStackTrace();
}
});
vertx.deployVerticle(new MyVerticle(), event -> {
if(event.succeeded()) {
System.out.println("Verticle deployed");
} else if (event.failed()) {
System.out.println("No Verticle deployed");
event.cause().printStackTrace();
}
});
}
示例10: prepareServer
import io.vertx.core.http.HttpServer; //导入方法依赖的package包/类
HttpServer prepareServer(Vertx vertx) {
HttpServer server = vertx.createHttpServer();
Router router = Router.router(vertx);
router.route().handler(LoggerHandler.create());
router.get("/speech").handler(this::speechHandler);
router.get().handler(StaticHandler.create("static"));
server.requestHandler(router::accept);
return server;
}
示例11: createServer
import io.vertx.core.http.HttpServer; //导入方法依赖的package包/类
@Override
protected TermServer createServer(TestContext context, HttpTermOptions options) {
HttpServer httpServer = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
Router router = Router.router(vertx);
Router subRouter = Router.router(vertx);
router.mountSubRouter("/sub", subRouter);
httpServer.requestHandler(router::accept);
Async async = context.async();
httpServer.listen(8080, context.asyncAssertSuccess(s -> {
async.complete();
}));
async.awaitSuccess(20000);
return TermServer.createHttpTermServer(vertx, subRouter, options);
}
示例12: async_04
import io.vertx.core.http.HttpServer; //导入方法依赖的package包/类
public static void async_04(TestContext context, Vertx vertx, Handler<HttpServerRequest> requestHandler) {
Async async = context.async();
HttpServer server = vertx.createHttpServer();
server.requestHandler(requestHandler);
server.listen(8080, ar -> {
context.assertTrue(ar.succeeded());
async.complete();
});
// Wait until completion
async.awaitSuccess();
// Do something else
}
示例13: start
import io.vertx.core.http.HttpServer; //导入方法依赖的package包/类
@Override
public void start() throws Exception {
super.start();
HttpServer server = getVertx().createHttpServer(new HttpServerOptions().setPort(8080));
server.requestHandler(req -> req.response().end(responseBody(req)));
server.listen();
}
示例14: testPreventRedirectLoop
import io.vertx.core.http.HttpServer; //导入方法依赖的package包/类
@Test
public void testPreventRedirectLoop(TestContext context) {
vertx = Vertx.vertx();
HttpServer redirectServer = vertx.createHttpServer();
redirectServer.requestHandler(req -> {
HttpServerResponse resp = req.response();
resp.setStatusCode(301);
resp.putHeader("Location", "http://localhost:8080/the_verticle.zip");
resp.end();
});
redirectServer.listen(8080, context.asyncAssertSuccess(r -> {
vertx.deployVerticle("http://localhost:8080/the_verticle.zip", context.asyncAssertFailure());
}));
}
示例15: testMetricsCleanupedOnVertxClose
import io.vertx.core.http.HttpServer; //导入方法依赖的package包/类
@Test
public void testMetricsCleanupedOnVertxClose() throws Exception {
CountDownLatch latch1 = new CountDownLatch(1);
HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
server.requestHandler(req -> {});
server.listen(onSuccess(res -> {
latch1.countDown();
}));
awaitLatch(latch1);
HttpClient client = vertx.createHttpClient(new HttpClientOptions());
CountDownLatch latch2 = new CountDownLatch(1);
NetServer nServer = vertx.createNetServer(new NetServerOptions().setPort(1234));
nServer.connectHandler(conn -> {});
nServer.listen(res -> {
latch2.countDown();
});
awaitLatch(latch2);
NetClient nClient = vertx.createNetClient(new NetClientOptions());
DatagramSocket sock = vertx.createDatagramSocket(new DatagramSocketOptions());
EventBus eb = vertx.eventBus();
assertFalse(metricsService.getMetricsSnapshot(vertx).isEmpty());
assertFalse(metricsService.getMetricsSnapshot(server).isEmpty());
assertFalse(metricsService.getMetricsSnapshot(client).isEmpty());
assertFalse(metricsService.getMetricsSnapshot(nServer).isEmpty());
assertFalse(metricsService.getMetricsSnapshot(nClient).isEmpty());
assertFalse(metricsService.getMetricsSnapshot(sock).isEmpty());
assertFalse(metricsService.getMetricsSnapshot(eb).isEmpty());
vertx.close(res -> {
assertTrue(metricsService.getMetricsSnapshot(vertx).isEmpty());
assertTrue(metricsService.getMetricsSnapshot(server).isEmpty());
assertTrue(metricsService.getMetricsSnapshot(client).isEmpty());
assertTrue(metricsService.getMetricsSnapshot(nServer).isEmpty());
assertTrue(metricsService.getMetricsSnapshot(nClient).isEmpty());
assertTrue(metricsService.getMetricsSnapshot(sock).isEmpty());
assertTrue(metricsService.getMetricsSnapshot(eb).isEmpty());
testComplete();
});
await();
vertx = null;
}