Example usage for io.vertx.core Context executeBlocking

List of usage examples for io.vertx.core Context executeBlocking

Introduction

In this page you can find the example usage for io.vertx.core Context executeBlocking.

Prototype

<T> Future<@Nullable T> executeBlocking(Handler<Promise<T>> blockingCodeHandler, boolean ordered);

Source Link

Document

Same as #executeBlocking(Handler,boolean,Handler) but with an handler called when the operation completes

Usage

From source file:io.gravitee.fetcher.http.vertx.VertxCompletableFuture.java

License:Apache License

/**
 * Returns a new CompletableFuture that is asynchronously completed by a action running in the worker thread pool of
 * Vert.x/*from  w  ww .  j a  v a  2s.c  o  m*/
 * <p>
 * This method is different from {@link CompletableFuture#runAsync(Runnable)} as it does not use a fork join
 * executor, but the worker thread pool.
 *
 * @param context  the Vert.x context
 * @param runnable the action, when its execution completes, it completes the returned CompletableFuture. If the
 *                 execution throws an exception, the returned CompletableFuture is completed exceptionally.
 * @return the new CompletableFuture
 */
public static VertxCompletableFuture<Void> runBlockingAsync(Context context, Runnable runnable) {
    Objects.requireNonNull(runnable);
    VertxCompletableFuture<Void> future = new VertxCompletableFuture<>(Objects.requireNonNull(context));
    context.executeBlocking(fut -> {
        try {
            runnable.run();
            future.complete(null);
        } catch (Throwable e) {
            future.completeExceptionally(e);
        }
    }, null);
    return future;
}