Java Code Examples for io.vertx.core.Context#executeBlocking()

The following examples show how to use io.vertx.core.Context#executeBlocking() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VertxCompletableFuture.java    From gravitee-management-rest-api with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a new CompletableFuture that is asynchronously completed by a task running in the worker thread pool of
 * Vert.x
 * <p>
 * This method is different from {@link CompletableFuture#supplyAsync(Supplier)} as it does not use a fork join
 * executor, but the worker thread pool.
 *
 * @param context  the context in which the supplier is executed.
 * @param supplier a function returning the value to be used to complete the returned CompletableFuture
 * @param <T>      the function's return type
 * @return the new CompletableFuture
 */
public static <T> VertxCompletableFuture<T> supplyBlockingAsync(Context context, Supplier<T> supplier) {
    Objects.requireNonNull(supplier);
    VertxCompletableFuture<T> future = new VertxCompletableFuture<>(context);
    context.<T>executeBlocking(
            fut -> {
                try {
                    fut.complete(supplier.get());
                } catch (Throwable e) {
                    fut.fail(e);
                }
            },
            ar -> {
                if (ar.failed()) {
                    future.completeExceptionally(ar.cause());
                } else {
                    future.complete(ar.result());
                }
            }
    );
    return future;
}
 
Example 2
Source File: ContextScheduler.java    From vertx-rx with Apache License 2.0 6 votes vote down vote up
private void execute(Object o) {
  if (workerExecutor != null) {
    workerExecutor.executeBlocking(fut -> {
      run(null);
      fut.complete();
    }, ordered, null);
  } else {
    Context ctx = context != null ? context : vertx.getOrCreateContext();
    if (blocking) {
      ctx.executeBlocking(fut -> {
        run(null);
        fut.complete();
      }, ordered, null);
    } else {
      ctx.runOnContext(this::run);
    }
  }
}
 
Example 3
Source File: ContextScheduler.java    From vertx-rx with Apache License 2.0 6 votes vote down vote up
private void execute(Object arg) {
  if (workerExecutor != null) {
    workerExecutor.executeBlocking(fut -> {
      run(null);
      fut.complete();
    }, ordered, null);
  } else {
    Context ctx = context != null ? context : vertx.getOrCreateContext();
    if (blocking) {
      ctx.executeBlocking(fut -> {
        run(null);
        fut.complete();
      }, ordered, null);
    } else {
      ctx.runOnContext(this::run);
    }
  }
}
 
Example 4
Source File: VertxCompletableFuture.java    From gravitee-gateway with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a new CompletableFuture that is asynchronously completed by a task running in the worker thread pool of
 * Vert.x
 * <p>
 * This method is different from {@link CompletableFuture#supplyAsync(Supplier)} as it does not use a fork join
 * executor, but the worker thread pool.
 *
 * @param context  the context in which the supplier is executed.
 * @param supplier a function returning the value to be used to complete the returned CompletableFuture
 * @param <T>      the function's return type
 * @return the new CompletableFuture
 */
public static <T> VertxCompletableFuture<T> supplyBlockingAsync(Context context, Supplier<T> supplier) {
    Objects.requireNonNull(supplier);
    VertxCompletableFuture<T> future = new VertxCompletableFuture<>(context);
    context.<T>executeBlocking(
            fut -> {
                try {
                    fut.complete(supplier.get());
                } catch (Throwable e) {
                    fut.fail(e);
                }
            },
            ar -> {
                if (ar.failed()) {
                    future.completeExceptionally(ar.cause());
                } else {
                    future.complete(ar.result());
                }
            }
    );
    return future;
}
 
Example 5
Source File: VertxCompletableFuture.java    From gravitee-gateway with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a new CompletableFuture that is asynchronously completed by a action running in the worker thread pool of
 * Vert.x
 * <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;
}
 
Example 6
Source File: VertxCompletableFuture.java    From gravitee-management-rest-api with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a new CompletableFuture that is asynchronously completed by a task running in the worker thread pool of
 * Vert.x
 * <p>
 * This method is different from {@link CompletableFuture#supplyAsync(Supplier)} as it does not use a fork join
 * executor, but the worker thread pool.
 *
 * @param context  the context in which the supplier is executed.
 * @param supplier a function returning the value to be used to complete the returned CompletableFuture
 * @param <T>      the function's return type
 * @return the new CompletableFuture
 */
public static <T> VertxCompletableFuture<T> supplyBlockingAsync(Context context, Supplier<T> supplier) {
    Objects.requireNonNull(supplier);
    VertxCompletableFuture<T> future = new VertxCompletableFuture<>(context);
    context.<T>executeBlocking(
            fut -> {
                try {
                    fut.complete(supplier.get());
                } catch (Throwable e) {
                    fut.fail(e);
                }
            },
            ar -> {
                if (ar.failed()) {
                    future.completeExceptionally(ar.cause());
                } else {
                    future.complete(ar.result());
                }
            }
    );
    return future;
}
 
Example 7
Source File: VertxCompletableFuture.java    From gravitee-management-rest-api with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a new CompletableFuture that is asynchronously completed by a action running in the worker thread pool of
 * Vert.x
 * <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;
}
 
Example 8
Source File: VertxCompletableFuture.java    From gravitee-management-rest-api with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a new CompletableFuture that is asynchronously completed by a task running in the worker thread pool of
 * Vert.x
 * <p>
 * This method is different from {@link CompletableFuture#supplyAsync(Supplier)} as it does not use a fork join
 * executor, but the worker thread pool.
 *
 * @param context  the context in which the supplier is executed.
 * @param supplier a function returning the value to be used to complete the returned CompletableFuture
 * @param <T>      the function's return type
 * @return the new CompletableFuture
 */
public static <T> VertxCompletableFuture<T> supplyBlockingAsync(Context context, Supplier<T> supplier) {
    Objects.requireNonNull(supplier);
    VertxCompletableFuture<T> future = new VertxCompletableFuture<>(context);
    context.<T>executeBlocking(
            fut -> {
                try {
                    fut.complete(supplier.get());
                } catch (Throwable e) {
                    fut.fail(e);
                }
            },
            ar -> {
                if (ar.failed()) {
                    future.completeExceptionally(ar.cause());
                } else {
                    future.complete(ar.result());
                }
            }
    );
    return future;
}
 
Example 9
Source File: VertxCompletableFuture.java    From gravitee-management-rest-api with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a new CompletableFuture that is asynchronously completed by a action running in the worker thread pool of
 * Vert.x
 * <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;
}
 
Example 10
Source File: UsernamePasswordAuthProvider.java    From hono with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected Future<Device> doValidateCredentials(
        final UsernamePasswordCredentials deviceCredentials,
        final CredentialsObject credentialsOnRecord) {

    final Context currentContext = Vertx.currentContext();
    if (currentContext == null) {
        return Future.failedFuture(new IllegalStateException("not running on vert.x Context"));
    } else {
        final Promise<Device> result = Promise.promise();
        currentContext.executeBlocking(blockingCodeHandler -> {
            log.debug("validating password hash on vert.x worker thread [{}]", Thread.currentThread().getName());
            final boolean isValid = credentialsOnRecord.getCandidateSecrets().stream()
                    .anyMatch(candidateSecret -> pwdEncoder.matches(deviceCredentials.getPassword(), candidateSecret));
            if (isValid) {
                blockingCodeHandler.complete(new Device(deviceCredentials.getTenantId(), credentialsOnRecord.getDeviceId()));
            } else {
                blockingCodeHandler.fail(new ClientErrorException(HttpURLConnection.HTTP_UNAUTHORIZED, "bad credentials"));
            }
        }, false, result);
        return result.future();
    }
}
 
Example 11
Source File: VertxCompletableFuture.java    From gravitee-management-rest-api with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a new CompletableFuture that is asynchronously completed by a action running in the worker thread pool of
 * Vert.x
 * <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;
}
 
Example 12
Source File: VertxCompletableFuture.java    From gravitee-management-rest-api with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a new CompletableFuture that is asynchronously completed by a task running in the worker thread pool of
 * Vert.x
 * <p>
 * This method is different from {@link CompletableFuture#supplyAsync(Supplier)} as it does not use a fork join
 * executor, but the worker thread pool.
 *
 * @param context  the context in which the supplier is executed.
 * @param supplier a function returning the value to be used to complete the returned CompletableFuture
 * @param <T>      the function's return type
 * @return the new CompletableFuture
 */
public static <T> VertxCompletableFuture<T> supplyBlockingAsync(Context context, Supplier<T> supplier) {
    Objects.requireNonNull(supplier);
    VertxCompletableFuture<T> future = new VertxCompletableFuture<>(context);
    context.<T>executeBlocking(
            fut -> {
                try {
                    fut.complete(supplier.get());
                } catch (Throwable e) {
                    fut.fail(e);
                }
            },
            ar -> {
                if (ar.failed()) {
                    future.completeExceptionally(ar.cause());
                } else {
                    future.complete(ar.result());
                }
            }
    );
    return future;
}
 
Example 13
Source File: VertxCompletableFuture.java    From gravitee-management-rest-api with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a new CompletableFuture that is asynchronously completed by a action running in the worker thread pool of
 * Vert.x
 * <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;
}
 
Example 14
Source File: VertxCompletableFuture.java    From gravitee-management-rest-api with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a new CompletableFuture that is asynchronously completed by a task running in the worker thread pool of
 * Vert.x
 * <p>
 * This method is different from {@link CompletableFuture#supplyAsync(Supplier)} as it does not use a fork join
 * executor, but the worker thread pool.
 *
 * @param context  the context in which the supplier is executed.
 * @param supplier a function returning the value to be used to complete the returned CompletableFuture
 * @param <T>      the function's return type
 * @return the new CompletableFuture
 */
public static <T> VertxCompletableFuture<T> supplyBlockingAsync(Context context, Supplier<T> supplier) {
    Objects.requireNonNull(supplier);
    VertxCompletableFuture<T> future = new VertxCompletableFuture<>(context);
    context.<T>executeBlocking(
            fut -> {
                try {
                    fut.complete(supplier.get());
                } catch (Throwable e) {
                    fut.fail(e);
                }
            },
            ar -> {
                if (ar.failed()) {
                    future.completeExceptionally(ar.cause());
                } else {
                    future.complete(ar.result());
                }
            }
    );
    return future;
}
 
Example 15
Source File: VertxCompletableFuture.java    From gravitee-management-rest-api with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a new CompletableFuture that is asynchronously completed by a action running in the worker thread pool of
 * Vert.x
 * <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;
}
 
Example 16
Source File: VertxCompletableFuture.java    From vertx-completable-future with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a new CompletableFuture that is asynchronously completed by a task running in the worker thread pool of
 * Vert.x
 * <p>
 * This method is different from {@link CompletableFuture#supplyAsync(Supplier)} as it does not use a fork join
 * executor, but the worker thread pool.
 *
 * @param context  the context in which the supplier is executed.
 * @param supplier a function returning the value to be used to complete the returned CompletableFuture
 * @param <T>      the function's return type
 * @return the new CompletableFuture
 */
public static <T> VertxCompletableFuture<T> supplyBlockingAsync(Context context, Supplier<T> supplier) {
  Objects.requireNonNull(supplier);
  VertxCompletableFuture<T> future = new VertxCompletableFuture<>(context);
  context.<T>executeBlocking(
      fut -> {
        try {
          fut.complete(supplier.get());
        } catch (Throwable e) {
          fut.fail(e);
        }
      },
      false,
      ar -> {
        if (ar.failed()) {
          future.completeExceptionally(ar.cause());
        } else {
          future.complete(ar.result());
        }
      }
  );
  return future;
}
 
Example 17
Source File: VertxCompletableFuture.java    From vertx-completable-future with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a new CompletableFuture that is asynchronously completed by a action running in the worker thread pool of
 * Vert.x
 * <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.<Void>executeBlocking(
      fut -> {
        try {
          runnable.run();
          fut.complete(null);
        } catch (Throwable e) {
          fut.fail(e);
        }
      },
      false,
      ar -> {
        if (ar.failed()) {
          future.completeExceptionally(ar.cause());
        } else {
          future.complete(ar.result());
        }
      }
  );
  return future;
}