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

The following examples show how to use io.vertx.core.Context#isEventLoopContext() . 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: TestClientPoolManager.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@Test
public void findByContext_worker(@Mocked Context workerContext) {
  HttpClientWithContext pool = new HttpClientWithContext(null, null);
  pools.add(pool);

  new Expectations() {
    {
      Vertx.currentContext();
      result = workerContext;
      workerContext.owner();
      result = vertx;
      workerContext.isEventLoopContext();
      result = false;
    }
  };

  Assert.assertSame(pool, poolMgr.findByContext());
}
 
Example 2
Source File: ClientPoolManager.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
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;
    }

    // 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);
}
 
Example 3
Source File: FaultInjectionHandler.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@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());
  }

  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);
    }
  });
}