首页 > 其他 > 详细

(十四) Thunder分布式RPC框架 - 链式调用

时间:2016-04-03 02:05:26      阅读:812      评论:0      收藏:0      [点我收藏+]

Thunder(QQ 群 471164539)发布在淘宝代码基地 http://code.taobao.org/p/Thunder/

基于jdeferred的Promise框架,实现单端的链式调用,主要实现同步链式调用,异步链式调用,同步/异步的混合的链式调用

?

1. 同步链式调用

同步链式调用,是基于Promise的DonePipe通道,Thunder框架封装成PromiseDonePipe,泛型化入参(T)和出参(T_OUT),通过PromiseExecutor决定调用的方向。当捕获异常的时候,PromiseExecutor将终止下一个同步调用。同步链式调用的示例如下,step1和step2方法代表业务端的调用方法。整个链式调用通过resolve方法初始化入参

public class PromiseSyncTest {
    private static final Logger LOG = LoggerFactory.getLogger(PromiseSyncTest.class);

    @Test
    public void test() throws Exception {
        PromiseExecutor<Long> promiseExecutor = new PromiseExecutor<Long>();
        promiseExecutor.currentPromise().then(new PromiseDonePipe<Long, Date>() {
            @Override
            public Date onDone(Long result) throws Exception {
                LOG.info("同步调用: Long={}", result);

                return step1(result);
            }
        }).then(new PromiseDonePipe<Date, String>() {
            @Override
            public String onDone(Date result) throws Exception {
                LOG.info("同步调用: Date={}", result);

                return step2(result);
            }
        }).then(new PromiseDonePipe<String, Void>() {
            @Override
            public Void onDone(String result) throws Exception {
                LOG.info("调用完成: String={}", result);

                return null;
            }
        });
        promiseExecutor.currentPromise().resolve(1459604027910L);

        System.in.read();
    }

    private Date step1(Long result) {
        return new Date(result);
    }

    private String step2(Date result) {
        // throw new IllegalArgumentException("Exception");
        
        return new SimpleDateFormat(ThunderConstants.DATE_FORMAT).format(result);
    }
}

?

2. 异步链式调用

异步链式调用,是基于Promise的DoneCallback,同时结合Thunder的Callback,Thunder框架封装成PromiseCallback,泛型化入参(T)和出参(T_OUT),通过PromiseExecutor决定调用的方向。当回调方法含有异常的时候,PromiseExecutor将终止下一个异步回调,业务端如果想使用该功能,必须把回调类继承PromiseCallback异步链式调用的示例如下,step1,step2和step3方法代表业务端的调用方法,分别进行三级Callback调用

?

public class PromiseAsyncTest {
    private static final Logger LOG = LoggerFactory.getLogger(PromiseAsyncTest.class);

    private PromiseCallback<Long, Date> callback1 = new PromiseCallback<Long, Date>() {
        @Override
        public Date onResult(Long result) throws Exception {
            LOG.info("异步回调 : Long={}", result);

            return new Date(result);
        }

        @Override
        public void onError(Exception e) throws Exception {
            
        }

        @Override
        public void onThen(Date result) {           
            step2(result);
        }
    };

    private PromiseCallback<Date, String> callback2 = new PromiseCallback<Date, String>() {
        @Override
        public String onResult(Date result) throws Exception {
            LOG.info("异步回调 : Date={}", result);

            return new SimpleDateFormat(ThunderConstants.DATE_FORMAT).format(result);
        }

        @Override
        public void onError(Exception e) throws Exception {
            
        }

        @Override
        public void onThen(String result) {
            step3(result);
        }
    };

    private PromiseCallback<String, Void> callback3 = new PromiseCallback<String, Void>() {
        @Override
        public Void onResult(String result) throws Exception {
            LOG.info("异步回调 : String={}", result);

            return null;
        }

        @Override
        public void onError(Exception e) throws Exception {

        }

        @Override
        public void onThen(Void result) {
            LOG.info("调用完成");
        }
    };

    @Test
    public void test() throws Exception {
        step1(1459604027910L);

        System.in.read();
    }

    private void step1(Long result) {
        try {
            callback1.call(result, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void step2(Date result) {
        try {
            callback2.call(result, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void step3(String result) {
        try {
            callback3.call(result, null);
            
            // callback3.call(value, new IllegalArgumentException("Exception"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

?

3. 同步/异步的混合链式调用

同步/异步的混合链式调用,是基于上述两种方式的混合。示例是先同步后异步的调用模式

public class PromiseMixTest {
    private static final Logger LOG = LoggerFactory.getLogger(PromiseMixTest.class);

    private PromiseCallback<Date, String> callback = new PromiseCallback<Date, String>() {
        @Override
        public String onResult(Date result) throws Exception {
            LOG.info("异步回调 : Date={}", result);

            return new SimpleDateFormat(ThunderConstants.DATE_FORMAT).format(result);
        }

        @Override
        public void onError(Exception e) throws Exception {

        }

        @Override
        public void onThen(String result) {
            step2(result);
        }
    };

    @Test
    public void test() throws Exception {
        PromiseExecutor<Long> promiseExecutor = new PromiseExecutor<Long>();
        promiseExecutor.currentPromise().then(new PromiseDonePipe<Long, Void>() {
            @Override
            public Void onDone(Long result) throws Exception {
                LOG.info("同步调用 : Long={}", result);
                
                Date date = new Date(result);

                step1(date);

                return null;
            }
        });
        promiseExecutor.currentPromise().resolve(1459604027910L);

        System.in.read();
    }

    private void step1(Date result) {
        try {
            callback.call(result, null);

            // callback.call(value, new IllegalArgumentException("Exception"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void step2(String result) {
        LOG.info("调用完成 : String={}", result);
    }
}



(十四) Thunder分布式RPC框架 - 链式调用

原文:http://nepxion.iteye.com/blog/2288237

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!