Thunder(QQ 群 471164539)发布在淘宝代码基地 http://code.taobao.org/p/Thunder/
基于jdeferred的Promise框架,实现单端的链式调用,主要实现同步链式调用,异步链式调用,同步/异步的混合的链式调用
?
同步链式调用,是基于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);
}
}
?
异步链式调用,是基于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();
}
}
}
?
同步/异步的混合链式调用,是基于上述两种方式的混合。示例是先同步后异步的调用模式
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);
}
}
原文:http://nepxion.iteye.com/blog/2288237