use of io.vertx.core.Future in project vertx-openshift-it by cescoffier.
the class HttpConfigDeployment method start.
@Override
public void start(Future<Void> future) {
// Create a router object.
Router router = Router.router(vertx);
eventBus = vertx.eventBus();
router.get("/conf").handler(this::config);
router.get("/*").handler(StaticHandler.create());
// Create the HTTP server and pass the "accept" method to the request handler.
vertx.createHttpServer().requestHandler(router::accept).listen(// Retrieve the port from the configuration, default to 8080.
config().getInteger("http.port", 8080), ar -> {
if (ar.succeeded()) {
eventBus.consumer("event-bus-config", msg -> {
eventbusContent.add(msg.body());
}).completionHandler(res -> {
if (res.succeeded()) {
System.out.println("Registration succeeded: event-bus-config");
} else {
System.out.println("Registration failed: " + res.cause());
}
});
System.out.println("Server starter on port " + ar.result().actualPort());
} else {
System.out.println("Unable to start server: " + ar.cause().getMessage());
}
future.handle(ar.mapEmpty());
});
}
use of io.vertx.core.Future in project vert.x by eclipse.
the class Http2ConnectionBase method close.
@Override
public Future<Void> close() {
PromiseInternal<Void> promise = context.promise();
ChannelPromise pr = chctx.newPromise();
ChannelPromise channelPromise = pr.addListener(promise);
handlerContext.writeAndFlush(Unpooled.EMPTY_BUFFER, pr);
channelPromise.addListener((ChannelFutureListener) future -> shutdown(0L));
return promise.future();
}
use of io.vertx.core.Future in project vert.x by eclipse.
the class FaultToleranceVerticle method start.
@Override
public void start() throws Exception {
JsonObject config = config();
id = config.getInteger("id");
numAddresses = config.getInteger("addressesCount");
List<Future> registrationFutures = new ArrayList<>(numAddresses);
for (int i = 0; i < numAddresses; i++) {
Promise<Void> registrationFuture = Promise.promise();
registrationFutures.add(registrationFuture.future());
vertx.eventBus().consumer(createAddress(id, i), msg -> msg.reply("pong")).completionHandler(registrationFuture);
}
Promise<Void> registrationFuture = Promise.promise();
registrationFutures.add(registrationFuture.future());
vertx.eventBus().consumer("ping", this::ping).completionHandler(registrationFuture);
CompositeFuture.all(registrationFutures).onSuccess(ar -> vertx.eventBus().send("control", "start"));
}
use of io.vertx.core.Future in project vert.x by eclipse.
the class Http2ServerTest method testUpgradeToClearText.
private void testUpgradeToClearText(HttpMethod method, Buffer expected, Handler<HttpServerOptions> optionsConfig) throws Exception {
server.close();
AtomicInteger serverConnectionCount = new AtomicInteger();
optionsConfig.handle(serverOptions);
server = vertx.createHttpServer(serverOptions.setHost(DEFAULT_HTTP_HOST).setPort(DEFAULT_HTTP_PORT).setUseAlpn(false).setSsl(false).setInitialSettings(new io.vertx.core.http.Http2Settings().setMaxConcurrentStreams(20000))).connectionHandler(conn -> serverConnectionCount.incrementAndGet());
server.requestHandler(req -> {
assertEquals("http", req.scheme());
assertEquals(method, req.method());
assertEquals(HttpVersion.HTTP_2, req.version());
assertEquals(10000, req.connection().remoteSettings().getMaxConcurrentStreams());
assertFalse(req.isSSL());
req.bodyHandler(body -> {
assertEquals(expected, body);
vertx.setTimer(10, id -> {
req.response().end();
});
});
}).connectionHandler(conn -> {
assertNotNull(conn);
serverConnectionCount.incrementAndGet();
});
startServer(testAddress);
AtomicInteger clientConnectionCount = new AtomicInteger();
client = vertx.createHttpClient(clientOptions.setUseAlpn(false).setSsl(false).setInitialSettings(new io.vertx.core.http.Http2Settings().setMaxConcurrentStreams(10000)));
Promise<HttpClientResponse> p1 = Promise.promise();
p1.future().onComplete(onSuccess(resp -> {
assertEquals(HttpVersion.HTTP_2, resp.version());
// assertEquals(20000, req.connection().remoteSettings().getMaxConcurrentStreams());
assertEquals(1, serverConnectionCount.get());
assertEquals(1, clientConnectionCount.get());
Promise<HttpClientResponse> p2 = Promise.promise();
p2.future().onComplete(onSuccess(resp2 -> {
testComplete();
}));
doRequest(method, expected, null, p2);
}));
doRequest(method, expected, conn -> clientConnectionCount.incrementAndGet(), p1);
await();
}
use of io.vertx.core.Future in project vert.x by eclipse.
the class Http2ServerTest method testStreamPauseResume.
private void testStreamPauseResume(Function<HttpServerRequest, Future<ReadStream<Buffer>>> streamProvider) throws Exception {
Buffer expected = Buffer.buffer();
String chunk = TestUtils.randomAlphaString(1000);
AtomicBoolean done = new AtomicBoolean();
AtomicBoolean paused = new AtomicBoolean();
Buffer received = Buffer.buffer();
server.requestHandler(req -> {
Future<ReadStream<Buffer>> fut = streamProvider.apply(req);
fut.onComplete(onSuccess(stream -> {
vertx.setPeriodic(1, timerID -> {
if (paused.get()) {
vertx.cancelTimer(timerID);
done.set(true);
// Let some time to accumulate some more buffers
vertx.setTimer(100, id -> {
stream.resume();
});
}
});
stream.handler(received::appendBuffer);
stream.endHandler(v -> {
assertEquals(expected, received);
testComplete();
});
stream.pause();
}));
});
startServer();
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
int id = request.nextStreamId();
request.encoder.writeHeaders(request.context, id, POST("/form").set("content-type", "text/plain"), 0, false, request.context.newPromise());
request.context.flush();
Http2Stream stream = request.connection.stream(id);
class Anonymous {
void send() {
boolean writable = request.encoder.flowController().isWritable(stream);
if (writable) {
Buffer buf = Buffer.buffer(chunk);
expected.appendBuffer(buf);
request.encoder.writeData(request.context, id, buf.getByteBuf(), 0, false, request.context.newPromise());
request.context.flush();
request.context.executor().execute(this::send);
} else {
request.encoder.writeData(request.context, id, Unpooled.EMPTY_BUFFER, 0, true, request.context.newPromise());
request.context.flush();
paused.set(true);
}
}
}
new Anonymous().send();
});
fut.sync();
await();
}
Aggregations