Example usage for io.vertx.core Context runOnContext

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

Introduction

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

Prototype

void runOnContext(Handler<Void> action);

Source Link

Document

Run the specified action asynchronously on the same context, some time after the current execution has completed.

Usage

From source file:es.upv.grycap.opengateway.core.http.VertxHttp2Client.java

License:Apache License

private Callback wrapCallback(final Handler<AsyncResult<HttpResponse>> resultHandler) {
    final Context context = vertx.getOrCreateContext();
    return new Callback() {
        @Override/*from   w w w . jav a2s.  c  o  m*/
        public void onResponse(final Response response) throws IOException {
            context.runOnContext(v -> {
                if (!response.isSuccessful())
                    resultHandler.handle(
                            failedFuture(new IOException(String.format("Unexpected code: %s", response))));
                else
                    resultHandler.handle(succeededFuture(new HttpResponse(response)));
            });
        }

        @Override
        public void onFailure(final Request request, final IOException throwable) {
            context.runOnContext(v -> {
                resultHandler.handle(failedFuture(
                        new IllegalStateException(String.format("Failed request: %s", request), throwable)));
            });
        }
    };
}

From source file:examples.CoreExamples.java

License:Open Source License

public void runInContextWithData(Vertx vertx) {
    final Context context = vertx.getOrCreateContext();
    context.put("data", "hello");
    context.runOnContext((v) -> {
        String hello = context.get("data");
    });/*  w w  w.  j  av  a2  s.c o m*/
}

From source file:examples.hystrix.HystrixExamples.java

License:Open Source License

public void exampleHystrix3(Vertx vertx) {
    vertx.runOnContext(v -> {/*ww w  . j  av a  2  s.com*/
        Context context = vertx.getOrCreateContext();
        HystrixCommand<String> command = getSomeCommandInstance();
        command.observe().subscribe(result -> {
            context.runOnContext(v2 -> {
                // Back on context (event loop or worker)
                String r = result;
            });
        });
    });
}

From source file:examples.VertxProtonExamples.java

License:Apache License

public void example5(Vertx vertx, ProtonClient client) {
    Context myContext = vertx.getOrCreateContext();

    myContext.runOnContext(x -> {
        client.connect("hostname", 5672, connectResult -> {
            // In this case the context will be 'myContext' from earlier
        });/*w ww .  ja v a  2s. c  o  m*/
    });
}

From source file:io.atomix.vertx.AtomixClusterManager.java

License:Apache License

@Override
public void getLockWithTimeout(String name, long timeout, Handler<AsyncResult<Lock>> handler) {
    Context context = vertx.getOrCreateContext();
    atomix.getLock(name).whenComplete((lock, error) -> {
        if (error == null) {
            lock.tryLock(Duration.ofMillis(timeout)).whenComplete((lockResult, lockError) -> {
                if (lockError == null) {
                    if (lockResult == null) {
                        context.runOnContext(v -> Future
                                .<Lock>failedFuture(new VertxException("Timed out waiting to get lock " + name))
                                .setHandler(handler));
                    } else {
                        context.runOnContext(v -> Future.<Lock>succeededFuture(new AtomixLock(vertx, lock))
                                .setHandler(handler));
                    }/*w w w.j av  a2s.  co  m*/
                } else {
                    context.runOnContext(v -> Future.<Lock>failedFuture(lockError).setHandler(handler));
                }
            });
        } else {
            context.runOnContext(v -> Future.<Lock>failedFuture(error).setHandler(handler));
        }
    });
}

From source file:io.atomix.vertx.AtomixClusterManager.java

License:Apache License

@Override
public synchronized void join(Handler<AsyncResult<Void>> handler) {
    Context context = vertx.getOrCreateContext();
    active = true;/* www.jav a2s.  c om*/
    atomix.getGroup("__atomixVertx").whenComplete((group, groupError) -> {
        if (groupError == null) {
            this.group = group;
            group.join().whenComplete((member, joinError) -> {
                if (joinError == null) {
                    this.member = member;
                    group.onJoin(this::handleJoin);
                    group.onLeave(this::handleLeave);
                    context.runOnContext(v -> Future.<Void>succeededFuture().setHandler(handler));
                } else {
                    context.runOnContext(v -> Future.<Void>failedFuture(joinError).setHandler(handler));
                }
            });
        } else {
            context.runOnContext(v -> Future.<Void>failedFuture(groupError).setHandler(handler));
        }
    });
}

From source file:io.atomix.vertx.AtomixClusterManager.java

License:Apache License

@Override
public synchronized void leave(Handler<AsyncResult<Void>> handler) {
    Context context = vertx.getOrCreateContext();
    if (active) {
        active = false;//from   ww w.  jav a2 s .co m
        member.leave().whenComplete((leaveResult, leaveError) -> {
            if (leaveError == null) {
                group = null;
                member = null;
                context.runOnContext(v -> Future.<Void>succeededFuture().setHandler(handler));
            } else {
                context.runOnContext(v -> Future.<Void>failedFuture(leaveError).setHandler(handler));
            }
        });
    } else {
        context.runOnContext(v -> Future.<Void>succeededFuture().setHandler(handler));
    }
}

From source file:io.atomix.vertx.VertxFutures.java

License:Apache License

/**
 * Wraps a void Vert.x handler.//from   ww  w.j a v a  2 s.  c  om
 */
static <T> BiConsumer<T, Throwable> voidHandler(Handler<AsyncResult<Void>> handler, Context context) {
    Assert.notNull(handler, "handler");
    Assert.notNull(context, "context");
    return (result, error) -> {
        if (error == null) {
            context.runOnContext(v -> Future.<Void>succeededFuture().setHandler(handler));
        } else {
            context.runOnContext(v -> Future.<Void>failedFuture(error).setHandler(handler));
        }
    };
}

From source file:io.atomix.vertx.VertxFutures.java

License:Apache License

/**
 * Wraps a Vert.x handler.// w w w  .  j a  v a2  s  . c  om
 */
static <T> BiConsumer<T, Throwable> resultHandler(Handler<AsyncResult<T>> handler, Context context) {
    Assert.notNull(handler, "handler");
    Assert.notNull(context, "context");
    return (result, error) -> {
        if (error == null) {
            context.runOnContext(v -> Future.succeededFuture(result).setHandler(handler));
        } else {
            context.runOnContext(v -> Future.<T>failedFuture(error).setHandler(handler));
        }
    };
}

From source file:io.atomix.vertx.VertxFutures.java

License:Apache License

/**
 * Converts a return value.//from w w  w . j a  va2 s .c om
 */
static <T, U> BiConsumer<T, Throwable> convertHandler(Handler<AsyncResult<U>> handler, Function<T, U> converter,
        Context context) {
    return (result, error) -> {
        if (error == null) {
            context.runOnContext(v -> Future.succeededFuture(converter.apply(result)).setHandler(handler));
        } else {
            context.runOnContext(v -> Future.<U>failedFuture(error).setHandler(handler));
        }
    };
}