@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()); } } ); } }
/** * 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; }
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())); } }); }
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); }
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);
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());
@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 -> {
@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 -> {}));
@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()); }
@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()); }
@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(); }
@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())); } }
/** * 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; }
@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(); } }); } }
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())); } }); }