Can you consider let com.alipay.sofa.rpc.message.ResponseFuture inherit java.util.concurrent.CompletionStage?
See original GitHub issue现有的sofarpc的异步使用方式有回调和Future两种,都有比较大的局限性;
- 回调
- service级别的回调意义不大,功能和Filter有所重合;
- 调用级别的回调处理起来比较繁琐,写起来也不直观;
- Future
- 目前的Future使用时依然得阻塞当前线程等待结果;
- 无法兼容reactor这样的框架;
java1.8引入了java.util.concurrent.CompletableFuture这个强大的异步编程辅助类,目前sofarpc的使用者只能通过这样的代码勉强做到兼容:
public static void main(String... args) {
// omit
// .setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE);
CompletableFuture<?> future = new CompletableFuture<>();
RpcInvokeContext.getContext().setResponseCallback(new CallbackImpl(future));
RemoteService.invoke();
future.thenApply(result -> {
// omit
})
}
static class CallbackImpl <T> implements SofaResponseCallback <T> {
private final CompletableFuture<T> future;
public CallbackImpl(CompletableFuture<T> future) {
this.future = future;
}
@Override
@SuppressWarnings("unchecked")
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
// BTW why the appResponse type is Object rather than T?
future.complete((T)appResponse);
}
@Override
public void onAppException(Throwable throwable, String methodName, RequestBase request) {
future.completeExceptionally(throwable);
}
@Override
public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
future.completeExceptionally(sofaException);
}
}
是否可以考虑让com.alipay.sofa.rpc.message.ResponseFuture继承java.util.concurrent.CompletionStage, 来提供更好的异步编程支持?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:5
- Comments:7 (1 by maintainers)
Top Results From Across the Web
com.alipay.sofa.rpc.message.AbstractResponseFuture ... - Tabnine
@Override public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { long realTimeOut = unit.
Read more >Extension mechanism - SOFAStack
The following sections introduce how to extend through the SPI interaction method. SOFARPC provides the capabilities of ExtensionLoader. Design ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
您好,我想认领这个任务
@guyeu 非常好的建议,不知道你对于这块是不是感兴趣,随时欢迎提PR一起共建,当然我们也会一起看下。
@guyeu Good suggestion, I don’t know if you are interested in this, we advocate providing PR to join us any time.Of course we will look at it together.