Example usage for io.vertx.core Context isEventLoopContext

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

Introduction

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

Prototype

boolean isEventLoopContext();

Source Link

Document

Is the current context an event loop context?

Usage

From source file:examples.CoreExamples.java

License:Open Source License

public void retrieveContextType(Vertx vertx) {
    Context context = vertx.getOrCreateContext();
    if (context.isEventLoopContext()) {
        System.out.println("Context attached to Event Loop");
    } else if (context.isWorkerContext()) {
        System.out.println("Context attached to Worker Thread");
    } else if (context.isMultiThreadedWorkerContext()) {
        System.out.println("Context attached to Worker Thread - multi threaded worker");
    } else if (!Context.isOnVertxThread()) {
        System.out.println("Context not attached to a thread managed by vert.x");
    }// ww w.  ja v  a  2s.com
}

From source file:org.apache.servicecomb.faultinjection.FaultInjectionHandler.java

License:Apache License

@Override
public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception {

    // prepare the key and lookup for request count.
    String key = invocation.getTransport().getName() + invocation.getMicroserviceQualifiedName();
    AtomicLong reqCount = FaultInjectionUtil.getOperMetTotalReq(key);
    // increment the request count here after checking the delay/abort condition.
    long reqCountCurrent = reqCount.getAndIncrement();

    FaultParam param = new FaultParam(reqCountCurrent);
    Context currentContext = Vertx.currentContext();
    if (currentContext != null && currentContext.isEventLoopContext()) {
        param.setVertx(currentContext.owner());
    }// w  ww.  j  a  v a  2 s . co m

    FaultExecutor executor = new FaultExecutor(faultInjectionFeatureList, invocation, param);
    executor.execute(response -> {
        try {
            if (response.isFailed()) {
                asyncResp.complete(response);
            } else {
                invocation.next(asyncResp);
            }
        } catch (Exception e) {
            asyncResp.consumerFail(e);
        }
    });
}

From source file:org.apache.servicecomb.foundation.vertx.client.ClientPoolManager.java

License:Apache License

protected CLIENT_POOL findByContext(Context targetContext) {
    Context currentContext = targetContext != null ? targetContext : Vertx.currentContext();
    if (currentContext != null && currentContext.owner() == vertx && currentContext.isEventLoopContext()) {
        // standard reactive mode
        CLIENT_POOL clientPool = currentContext.get(id);
        if (clientPool != null) {
            return clientPool;
        }//from  ww w  .  java  2  s . c  o  m

        // this will make "client.thread-count" bigger than which in microservice.yaml
        // maybe it's better to remove "client.thread-count", just use "rest/highway.thread-count"
        return createClientPool(currentContext);
    }

    // not in correct context:
    // 1.normal thread
    // 2.vertx worker thread
    // 3.other vertx thread
    // select a existing context
    int idx = reactiveNextIndex.getAndIncrement() % pools.size();
    if (idx < 0) {
        idx = -idx;
    }
    return pools.get(idx);
}