All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.smallrye.reactive.messaging.providers.helpers.VertxContext Maven / Gradle / Ivy

package io.smallrye.reactive.messaging.providers.helpers;

import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.function.Consumer;

import io.vertx.core.Context;
import io.vertx.core.Vertx;

// TODO move to smallrye-common-vertx-context
public class VertxContext {

    public static Context getRootContext(Context context) {
        return io.smallrye.common.vertx.VertxContext.getRootContext(context);
    }

    public static Context createNewDuplicatedContext() {
        return io.smallrye.common.vertx.VertxContext.createNewDuplicatedContext();
    }

    public static void runOnContext(Context context, Runnable runnable) {
        if (Vertx.currentContext() == context) {
            runnable.run();
        } else {
            context.runOnContext(x -> runnable.run());
        }
    }

    public static void runOnEventLoopContext(Context context, Runnable runnable) {
        if (Vertx.currentContext() == context && Context.isOnEventLoopThread()) {
            runnable.run();
        } else {
            context.runOnContext(x -> runnable.run());
        }
    }

    public static  CompletionStage runOnContext(Context context, Consumer> runnable) {
        CompletableFuture future = new CompletableFuture<>();
        runOnContext(context, () -> runnable.accept(future));
        return future;
    }

    public static  CompletionStage runOnEventLoopContext(Context context, Consumer> runnable) {
        CompletableFuture future = new CompletableFuture<>();
        runOnEventLoopContext(context, () -> runnable.accept(future));
        return future;
    }

    public static  CompletionStage callOnContext(Context context, Callable callable) {
        return runOnContext(context, future -> {
            try {
                future.complete(callable.call());
            } catch (Throwable reason) {
                future.completeExceptionally(reason);
            }
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy