io.vertx.core.Future.complete()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(9.1k)|赞(0)|评价(0)|浏览(162)

本文整理了Java中io.vertx.core.Future.complete()方法的一些代码示例,展示了Future.complete()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Future.complete()方法的具体详情如下:
包路径:io.vertx.core.Future
类名称:Future
方法名:complete

Future.complete介绍

[英]Set a null result. Any handler will be called, if there is one, and the future will be marked as completed.
[中]设置一个空结果。将调用任何处理程序(如果有),并且未来将标记为已完成。

代码示例

代码示例来源:origin: eclipse-vertx/vert.x

@Override
public void handle(Future<T> fut) {
 try {
  T result = perform();
  fut.complete(result);
 } catch (Exception e) {
  fut.fail(e);
 }
}

代码示例来源:origin: vert-x3/vertx-examples

@Override
 public void start(Future<Void> future) throws Exception {
  // The `my-verticle` is deployed using the following convention:
  // `maven:` + groupId + `:` + artifactId + `:` + version + `::` + verticle name
  vertx.deployVerticle("maven:io.vertx:maven-service-factory-verticle:3.6.2::my-verticle",
    ar -> {
     if (ar.succeeded()) {
      future.complete();
     } else {
      future.fail(ar.cause());
     }
    }
  );
 }
}

代码示例来源:origin: eclipse-vertx/vert.x

/**
 * Map the failure of a future to a specific {@code value}.<p>
 *
 * When this future fails, this {@code value} will complete the future returned by this method call.<p>
 *
 * When this future succeeds, the result will be propagated to the returned future.
 *
 * @param value the value that eventually completes the mapped future
 * @return the mapped future
 */
default Future<T> otherwise(T value) {
 Future<T> ret = Future.future();
 setHandler(ar -> {
  if (ar.succeeded()) {
   ret.complete(result());
  } else {
   ret.complete(value);
  }
 });
 return ret;
}

代码示例来源:origin: eclipse-vertx/vert.x

@Override
 public void start(Future<Void> startFuture) throws Exception {
  vertx.deployVerticle("java:" + ChildVerticle.class.getName(), ar -> {
   if (ar.succeeded()) {
    startFuture.complete(null);
   } else {
    ar.cause().printStackTrace();
   }
  });
 }
}

代码示例来源:origin: eclipse-vertx/vert.x

private void initializeHaManager(Handler<AsyncResult<Vertx>> resultHandler) {
 this.executeBlocking(fut -> {
  // Init the manager (i.e register listener and check the quorum)
  // after the event bus has been fully started and updated its state
  // it will have also set the clustered changed view handler on the ha manager
  haManager.init();
  fut.complete();
 }, false, ar -> {
  if (ar.succeeded()) {
   if (metrics != null) {
    metrics.vertxCreated(this);
   }
   resultHandler.handle(Future.succeededFuture(this));
  } else {
   log.error("Failed to initialize HAManager", ar.cause());
   resultHandler.handle(Future.failedFuture(ar.cause()));
  }
 });
}

代码示例来源:origin: vert-x3/vertx-examples

private Future<String> anAsyncAction() {
 Future<String> future = Future.future();
 // mimic something that take times
 vertx.setTimer(100, l -> future.complete("world"));
 return future;
}

代码示例来源:origin: eclipse-vertx/vert.x

private void testAnySucceeded2(BiFunction<Future<String>, Future<Integer>, CompositeFuture> any) {
 Future<String> f1 = Future.future();
 Future<Integer> f2 = Future.future();
 CompositeFuture composite = any.apply(f1, f2);
 Checker<CompositeFuture> checker = new Checker<>(composite);
 f1.fail("failure");
 checker.assertNotCompleted();
 f2.complete(3);
 checker.assertSucceeded(composite);
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testAsyncDeployCalledSynchronously() throws Exception {
 MyAsyncVerticle verticle = new MyAsyncVerticle(f -> f.complete(null), f -> f.complete(null));
 vertx.deployVerticle(verticle, ar -> {
  assertTrue(ar.succeeded());
  testComplete();
 });
 await();
}

代码示例来源:origin: eclipse-vertx/vert.x

if (ar.cause() instanceof ReplyException) {
  ReplyException cause = (ReplyException) ar.cause();
  assertSame(ReplyFailure.NO_HANDLERS, cause.failureType());
 } else {
  fail(ar.cause());
vertices[0].executeBlocking(fut -> {
 vertices[0].eventBus().send("blah", "blah", ar -> {
  assertTrue(ar.failed());
  complete();
 });
 fut.complete();
}, false, null);

代码示例来源:origin: eclipse-vertx/vert.x

deploymentManager.undeployAll(ar1 -> {
 HAManager haManager = haManager();
 Future<Void> haFuture = Future.future();
 if (haManager != null) {
  this.executeBlocking(fut -> {
   haManager.stop();
   fut.complete();
  }, false, haFuture);
 } else {
  haFuture.complete();
       log.error("Failure in shutting down server", res.cause());

代码示例来源:origin: eclipse-vertx/vert.x

@Override
 public void start(Future<Void> startFuture) throws Exception {
  vertx.executeBlocking(fut -> {
   try {
    SECONDS.sleep(5);
    fut.complete();
   } catch (InterruptedException e) {
    fut.fail(e);
   }
  }, startFuture);
 }
}, deploymentOptions, onSuccess(did -> {

代码示例来源:origin: eclipse-vertx/vert.x

@Override
 public void start() throws Exception {
  vertx.executeBlocking(fut -> {
   thread.set(Thread.currentThread());
   assertTrue(Context.isOnVertxThread());
   assertTrue(Context.isOnWorkerThread());
   assertFalse(Context.isOnEventLoopThread());
   assertTrue(Thread.currentThread().getName().startsWith(poolName + "-"));
   fut.complete();
  }, onSuccess(v -> {
   vertx.undeploy(context.deploymentID());
  }));
 }
}, new DeploymentOptions().setWorkerPoolName(poolName), onSuccess(v -> {}));

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testExecuteBlockingTTCL() throws Exception {
 ClassLoader cl = Thread.currentThread().getContextClassLoader();
 assertNotNull(cl);
 CountDownLatch latch = new CountDownLatch(1);
 AtomicReference<ClassLoader> blockingTCCL = new AtomicReference<>();
 vertx.<String>executeBlocking(future -> {
  future.complete("whatever");
  blockingTCCL.set(Thread.currentThread().getContextClassLoader());
 }, ar -> {
  assertTrue(ar.succeeded());
  assertEquals("whatever", ar.result());
  latch.countDown();
 });
 assertSame(cl, Thread.currentThread().getContextClassLoader());
 awaitLatch(latch);
 assertSame(cl, blockingTCCL.get());
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testToString() {
 assertEquals("Future{unresolved}", Future.future().toString());
 assertEquals("Future{result=abc}", Future.succeededFuture("abc").toString());
 assertEquals("Future{cause=It's like that, and that's the way it is}", Future.failedFuture("It's like that, and that's the way it is").toString());
 Future<String> f = Future.future();
 f.complete("abc");
 assertEquals("Future{result=abc}", f.toString());
 f = Future.future();
 f.fail("abc");
 assertEquals("Future{cause=abc}", f.toString());
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testAsyncUndeployFailure() throws Exception {
 long delay = 1000;
 MyAsyncVerticle verticle = new MyAsyncVerticle(f-> f.complete(null), f -> vertx.setTimer(delay, id -> f.fail(new Exception("foobar"))));
 vertx.deployVerticle(verticle, ar -> {
  assertTrue(ar.succeeded());
  long start = System.currentTimeMillis();
  vertx.undeploy(ar.result(), ar2 -> {
   assertFalse(ar2.succeeded());
   long now = System.currentTimeMillis();
   assertTrue(now - start >= delay);
   assertFalse(vertx.deploymentIDs().contains(ar.result()));
   testComplete();
  });
 });
 await();
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testSetResultOnCompletedFuture() {
 ArrayList<Future<Object>> futures = new ArrayList<>();
 futures.add(Future.succeededFuture());
 futures.add(Future.succeededFuture());
 futures.add(Future.succeededFuture(new Object()));
 futures.add(Future.succeededFuture(new Object()));
 futures.add(Future.failedFuture(new Exception()));
 futures.add(Future.failedFuture(new Exception()));
 for (Future<Object> future : futures) {
  try {
   future.complete(new Object());
   fail();
  } catch (IllegalStateException ignore) {
  }
  assertFalse(future.tryComplete(new Object()));
  try {
   future.complete(null);
   fail();
  } catch (IllegalStateException ignore) {
  }
  assertFalse(future.tryComplete(null));
  try {
   future.fail(new Exception());
   fail();
  } catch (IllegalStateException ignore) {
  }
  assertFalse(future.tryFail(new Exception()));
 }
}

代码示例来源:origin: eclipse-vertx/vert.x

/**
 * Map the result of a future to a specific {@code value}.<p>
 *
 * When this future succeeds, this {@code value} will complete the future returned by this method call.<p>
 *
 * When this future fails, the failure will be propagated to the returned future.
 *
 * @param value the value that eventually completes the mapped future
 * @return the mapped future
 */
default <V> Future<V> map(V value) {
 Future<V> ret = Future.future();
 setHandler(ar -> {
  if (ar.succeeded()) {
   ret.complete(value);
  } else {
   ret.fail(ar.cause());
  }
 });
 return ret;
}

代码示例来源:origin: eclipse-vertx/vert.x

@Override
 public void start(Future<Void> startFuture) throws Exception {
  vertx.deployVerticle("java:" + OtherSourceVerticle.class.getName().replace('.', '/') + ".java", new DeploymentOptions(), ar -> {
   if (ar.succeeded()) {
    startFuture.complete((Void) null);
   } else {
    ar.cause().printStackTrace();
   }
  });
 }
}

代码示例来源:origin: eclipse-vertx/vert.x

private void createHaManager(VertxOptions options, Handler<AsyncResult<Vertx>> resultHandler) {
 this.<Map<String, String>>executeBlocking(fut -> {
  fut.complete(clusterManager.getSyncMap(CLUSTER_MAP_NAME));
 }, false, ar -> {
  if (ar.succeeded()) {
   Map<String, String> clusterMap = ar.result();
   haManager = new HAManager(this, deploymentManager, clusterManager, clusterMap, options.getQuorumSize(), options.getHAGroup(), options.isHAEnabled());
   startEventBus(resultHandler);
  } else {
   log.error("Failed to start HAManager", ar.cause());
   resultHandler.handle(Future.failedFuture(ar.cause()));
  }
 });
}

代码示例来源:origin: vert-x3/vertx-examples

private Future<String> anotherAsyncAction(String name) {
 Future<String> future = Future.future();
 // mimic something that take times
 vertx.setTimer(100, l -> future.complete("hello " + name));
 return future;
}

相关文章