@Override public void start() throws Exception { Future<String> future = anAsyncAction(); future.compose(this::anotherAsyncAction) .setHandler(ar -> { if (ar.failed()) { System.out.println("Something bad happened"); ar.cause().printStackTrace(); } else { System.out.println("Result: " + ar.result()); } }); }
@Override public void undeploy(String deploymentID, Handler<AsyncResult<Void>> completionHandler) { HAManager haManager = haManager(); Future<Void> haFuture = Future.future(); if (haManager != null && haManager.isEnabled()) { this.executeBlocking(fut -> { haManager.removeFromHA(deploymentID); fut.complete(); }, false, haFuture); } else { haFuture.complete(); } haFuture.compose(v -> { Future<Void> deploymentFuture = Future.future(); deploymentManager.undeployVerticle(deploymentID, deploymentFuture); return deploymentFuture; }).setHandler(completionHandler); }
@Test public void testComposeFailure() { Exception cause = new Exception(); Future<String> f1 = Future.future(); Future<Integer> f2 = Future.future(); Checker<Integer> checker = new Checker<>(f2); f1.compose(string -> f2.complete(string.length()), f2); f1.fail(cause); checker.assertFailed(cause); Future<String> f3 = Future.future(); Future<Integer> f4 = f3.compose(string -> Future.succeededFuture(string.length())); checker = new Checker<>(f4); f3.fail(cause); checker.assertFailed(cause); }
@Test public void testComposeSuccessToFailure() { Throwable cause = new Throwable(); AtomicReference<String> ref = new AtomicReference<>(); Future<Integer> c = Future.future(); Future<String> f3 = Future.future(); Future<Integer> f4 = f3.compose(string -> { ref.set(string); return c; }); Checker<Integer> checker = new Checker<>(f4); f3.complete("abcdef"); c.fail(cause); checker.assertFailed(cause); }
@Test public void testComposeWithNullFunction() { Future<Integer> fut = Future.future(); try { fut.compose((Function<Integer, Future<Integer>>) null); fail(); } catch (NullPointerException ignore) { } }
@Test public void testComposeFails() { RuntimeException cause = new RuntimeException(); Future<String> f1 = Future.future(); Future<Integer> f2 = Future.future(); Checker<Integer> checker = new Checker<>(f2); f1.compose(string -> { throw cause; }, f2); f1.complete("foo"); checker.assertFailed(cause); Future<String> f3 = Future.future(); Future<Integer> f4 = f3.compose(string -> { throw cause; }); checker = new Checker<>(f4); f3.complete("foo"); checker.assertFailed(cause); }
@Test public void testComposeSuccessToSuccess() { Future<String> f1 = Future.future(); Future<Integer> f2 = Future.future(); Checker<Integer> checker = new Checker<>(f2); f1.compose(string -> f2.complete(string.length()), f2); checker.assertNotCompleted(); f1.complete("abcdef"); checker.assertSucceeded(6); AtomicReference<String> ref = new AtomicReference<>(); Future<Integer> c = Future.future(); Future<String> f3 = Future.future(); Future<Integer> f4 = f3.compose(string -> { ref.set(string); return c; }); checker = new Checker<>(f4); f3.complete("abcdef"); checker.assertNotCompleted(); assertEquals("abcdef", ref.get()); c.complete(6); checker.assertSucceeded(6); }
@Test public void testComposeFailsAfterCompletion() { Future<String> f1 = Future.future(); Future<Integer> f2 = Future.future(); Checker<Integer> checker = new Checker<>(f2); RuntimeException cause = new RuntimeException(); f1.compose(string -> { f2.complete(46); throw cause; }, f2); try { f1.complete("foo"); fail(); } catch (Exception e) { assertEquals(cause, e); } checker.assertSucceeded(46); }
/** * @param message example: {format: "png", categories: [shenghuo, buyi]} */ private void getGushiciFromRedis(Message<JsonObject> message) { JsonArray realCategory = new JsonArray() .add("png".equals(message.body().getString("format")) ? "img" : "json") .addAll(message.body().getJsonArray("categories")); checkAndGetKey(realCategory) .compose(key -> Future.<String>future(s -> redisClient.srandmember(key, s))) // 从 set 随机返回一个对象 .setHandler(res -> { if (res.succeeded()) { message.reply(res.result()); } else { if (res.cause() instanceof ReplyException) { ReplyException exception = (ReplyException) res.cause(); message.fail(exception.failureCode(), exception.getMessage()); } message.fail(500, res.cause().getMessage()); } }); }
@Override public void start() { // 读取配置文件 RedisOptions redisOptions = new RedisOptions() .setHost(config().getJsonObject("redis").getString("host","127.0.0.1")) .setPort(config().getJsonObject("redis").getInteger("port",6379)) .setSelect(config().getJsonObject("redis").getInteger("select",0)); // 配置 RuntimeError 错误记录 vertx.exceptionHandler(error -> log.error(error)); // 顺序部署 Verticle Future.<Void>succeededFuture() .compose(v -> Future.<String>future(s -> vertx.deployVerticle(new ApiVerticle(),new DeploymentOptions().setConfig(config()), s))) .compose(v -> Future.<String>future(s -> vertx.deployVerticle(new DataService(redisOptions), new DeploymentOptions().setConfig(config()),s))) .compose(v -> Future.<String>future(s -> vertx.deployVerticle(new LogService(redisOptions), s))) .compose(v -> log.info("Vert.x started successfully"), Future.future().setHandler(ar -> { if (ar.failed()) { log.error("Vert.x failed to start", ar.cause()); } })); } }
@Override public void undeploy(String deploymentID, Handler<AsyncResult<Void>> completionHandler) { HAManager haManager = haManager(); Future<Void> haFuture = Future.future(); if (haManager != null && haManager.isEnabled()) { this.executeBlocking(fut -> { haManager.removeFromHA(deploymentID); fut.complete(); }, false, haFuture); } else { haFuture.complete(); } haFuture.compose(v -> { Future<Void> deploymentFuture = Future.future(); deploymentManager.undeployVerticle(deploymentID, deploymentFuture); return deploymentFuture; }).setHandler(completionHandler); }
@Test public void testComposeFailure() { Exception cause = new Exception(); Future<String> f1 = Future.future(); Future<Integer> f2 = Future.future(); Checker<Integer> checker = new Checker<>(f2); f1.compose(string -> f2.complete(string.length()), f2); f1.fail(cause); checker.assertFailed(cause); Future<String> f3 = Future.future(); Future<Integer> f4 = f3.compose(string -> Future.succeededFuture(string.length())); checker = new Checker<>(f4); f3.fail(cause); checker.assertFailed(cause); }
@Test public void testComposeSuccessToFailure() { Throwable cause = new Throwable(); AtomicReference<String> ref = new AtomicReference<>(); Future<Integer> c = Future.future(); Future<String> f3 = Future.future(); Future<Integer> f4 = f3.compose(string -> { ref.set(string); return c; }); Checker<Integer> checker = new Checker<>(f4); f3.complete("abcdef"); c.fail(cause); checker.assertFailed(cause); }
@Test public void testComposeWithNullFunction() { Future<Integer> fut = Future.future(); try { fut.compose((Function<Integer, Future<Integer>>) null); fail(); } catch (NullPointerException ignore) { } }
@Test public void testComposeFails() { RuntimeException cause = new RuntimeException(); Future<String> f1 = Future.future(); Future<Integer> f2 = Future.future(); Checker<Integer> checker = new Checker<>(f2); f1.compose(string -> { throw cause; }, f2); f1.complete("foo"); checker.assertFailed(cause); Future<String> f3 = Future.future(); Future<Integer> f4 = f3.compose(string -> { throw cause; }); checker = new Checker<>(f4); f3.complete("foo"); checker.assertFailed(cause); }
@Test public void testComposeSuccessToSuccess() { Future<String> f1 = Future.future(); Future<Integer> f2 = Future.future(); Checker<Integer> checker = new Checker<>(f2); f1.compose(string -> f2.complete(string.length()), f2); checker.assertNotCompleted(); f1.complete("abcdef"); checker.assertSucceeded(6); AtomicReference<String> ref = new AtomicReference<>(); Future<Integer> c = Future.future(); Future<String> f3 = Future.future(); Future<Integer> f4 = f3.compose(string -> { ref.set(string); return c; }); checker = new Checker<>(f4); f3.complete("abcdef"); checker.assertNotCompleted(); assertEquals("abcdef", ref.get()); c.complete(6); checker.assertSucceeded(6); }
@Test public void testComposeFailsAfterCompletion() { Future<String> f1 = Future.future(); Future<Integer> f2 = Future.future(); Checker<Integer> checker = new Checker<>(f2); RuntimeException cause = new RuntimeException(); f1.compose(string -> { f2.complete(46); throw cause; }, f2); try { f1.complete("foo"); fail(); } catch (Exception e) { assertEquals(cause, e); } checker.assertSucceeded(46); }
@Override protected Future<Void> delete(Reconciliation reconciliation) { return deleteKafka(reconciliation) .compose(i -> deleteZk(reconciliation)) .map((Void) null); }
@Override public void start(Future<Void> future) throws Exception { super.start(); // create the service instance accountService = new JdbcAccountServiceImpl(vertx, config()); // register the service proxy on event bus ProxyHelper.registerService(AccountService.class, vertx, accountService, SERVICE_ADDRESS); // publish the service and REST endpoint in the discovery infrastructure publishEventBusService(SERVICE_NAME, SERVICE_ADDRESS, AccountService.class) .compose(servicePublished -> deployRestVerticle()) .setHandler(future.completer()); }
@Override public void start(Future<Void> future) throws Exception { super.start(); crudService = new StoreCRUDServiceImpl(vertx, config()); ProxyHelper.registerService(StoreCRUDService.class, vertx, crudService, SERVICE_ADDRESS); // publish service and deploy REST verticle publishEventBusService(SERVICE_NAME, SERVICE_ADDRESS, StoreCRUDService.class) .compose(servicePublished -> deployRestVerticle(crudService)) .setHandler(future.completer()); }