首页  

vertx future compose CompositeFuture     所属分类 vertx 浏览量 258
package dyyx;

import io.vertx.core.*;

public class FutureComposeDemo {
    public static void main(String[]args)throws Exception{

        Future< String> f = a("");
        f.compose(FutureComposeDemo::b)
                .compose(FutureComposeDemo::c)
                .onSuccess(System.out::println)
                .onFailure(e ->e.printStackTrace());
        
        Future< String> fa = a("");
        Future< String> fb = b("");
        Future< String> fc = c("");

        CompositeFuture.any(fa,fb,fc).onComplete(ar -> System.out.println(ar.result().list()));

        fa = a("");
        fb = b("");
        fc = c("");

        CompositeFuture.all(fa,fb,fc)
                // .onComplete(ar -> System.out.println(ar.result().list()))
                .onFailure(e -> e.printStackTrace())
                .onSuccess( v -> System.out.println(v.list()));
    }

    private static Future< String> a(String str){
        Promise< String> promise = Promise.promise();
        doSleep(1000);
        promise.complete(" a "+str);
        return promise.future();
    }
    private static Future< String> b(String str){
        Promise< String> promise = Promise.promise();
        doSleep(1100);
        promise.complete(" b "+str);
        // promise.fail("b error");
        return promise.future();
    }
    private static Future< String> c(String str){
        Promise< String> promise = Promise.promise();
        doSleep(1200);
        promise.complete(" c "+str);
        return promise.future();
    }

    public static void doSleep(long time){
        try{
            Thread.sleep(time);
        }catch(Exception e){
            //
        }
    }
}


c b a [ a , b , c ] [ a , b , c ] b方法 返回错误时 promise.fail("b error"); io.vertx.core.impl.NoStackTraceThrowable: b error [ a , null, c ] io.vertx.core.impl.NoStackTraceThrowable: b error

上一篇     下一篇
vertx架构简介

CompletableFuture 例子

CompletionStage 说明

vertx future高级组合操作

vertx future 与 CompletionStage 互操作

vertx webclient 使用