Example usage for io.vertx.core Vertx runOnContext

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

Introduction

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

Prototype

void runOnContext(Handler<Void> action);

Source Link

Document

Puts the handler on the event queue for the current context so it will be run asynchronously ASAP after all preceeding events have been handled.

Usage

From source file:examples.hystrix.HystrixExamples.java

License:Open Source License

public void exampleHystrix3(Vertx vertx) {
    vertx.runOnContext(v -> {
        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 w  w w. j  ava2s.co m*/
        });
    });
}

From source file:io.reactiverse.pgclient.PgClient.java

License:Apache License

/**
 * Connects to the database and returns the connection if that succeeds.
 * <p/>//from  w ww. ja va  2 s . c  o  m
 * The connection interracts directly with the database is not a proxy, so closing the
 * connection will close the underlying connection to the database.
 *
 * @param vertx the vertx instance
 * @param options the connect options
 * @param handler the handler called with the connection or the failure
 */
static void connect(Vertx vertx, PgConnectOptions options, Handler<AsyncResult<PgConnection>> handler) {
    Context ctx = Vertx.currentContext();
    if (ctx != null) {
        PgConnectionFactory client = new PgConnectionFactory(ctx, false, options);
        client.create(ar -> {
            if (ar.succeeded()) {
                Connection conn = ar.result();
                PgConnectionImpl p = new PgConnectionImpl(client, ctx, conn);
                conn.init(p);
                handler.handle(Future.succeededFuture(p));
            } else {
                handler.handle(Future.failedFuture(ar.cause()));
            }
        });
    } else {
        vertx.runOnContext(v -> {
            if (options.isUsingDomainSocket() && !vertx.isNativeTransportEnabled()) {
                handler.handle(Future.failedFuture("Native transport is not available"));
            } else {
                connect(vertx, options, handler);
            }
        });
    }
}

From source file:net.kuujo.vertigo.impl.VertigoFactoryImpl.java

License:Apache License

@Override
public void vertigoAsync(VertigoOptions options, Handler<AsyncResult<Vertigo>> resultHandler) {
    Vertx vertx = Vertx.vertx();
    vertx.runOnContext(aVoid -> {
        Future.<Vertigo>succeededFuture(new VertigoImpl(vertx, options)).setHandler(resultHandler);
    });//w  w  w  .ja  va 2  s  .co  m
}

From source file:org.apache.servicecomb.demo.pojo.client.CodeFirstPojoClient.java

License:Apache License

private void testCodeFirstCompletableFuture(CodeFirstPojoIntf codeFirst) {
    if (!CodeFirstPojoClientIntf.class.isInstance(codeFirst)) {
        return;//from   w w w. j a  v a 2  s .c o m
    }

    Vertx vertx = VertxUtils.getOrCreateVertxByName("transport", null);
    CountDownLatch latch = new CountDownLatch(1);
    // vertx.runOnContext in normal thread is not a good practice
    // here just a test, not care for this.
    vertx.runOnContext(V -> {
        InvocationContext context = new InvocationContext();
        context.addContext("k", "v");
        ContextUtils.setInvocationContext(context);
        CompletableFuture<String> future = ((CodeFirstPojoClientIntf) codeFirst).sayHiAsync("someone");

        future.thenCompose(result -> {
            TestMgr.check("someone sayhi, context k: v", result);

            TestMgr.check(true, context == ContextUtils.getInvocationContext());

            return ((CodeFirstPojoClientIntf) codeFirst).sayHiAsync("someone 1");
        }).whenComplete((r, e) -> {
            TestMgr.check("someone 1 sayhi, context k: v", r);
            latch.countDown();
        });

        ContextUtils.removeInvocationContext();
    });

    try {
        latch.await();
    } catch (InterruptedException e) {
        throw new IllegalStateException(e);
    }
}