Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can you consider let com.alipay.sofa.rpc.message.ResponseFuture inherit java.util.concurrent.CompletionStage? #945

Open
hu-chia opened this issue Jun 9, 2020 · 9 comments
Assignees
Labels
enhancement New feature or request later This will be worked on in later version question Further information is requested

Comments

@hu-chia
Copy link
Contributor

hu-chia commented Jun 9, 2020

现有的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, 来提供更好的异步编程支持?

@sofastack-bot sofastack-bot bot changed the title 是否可以考虑让com.alipay.sofa.rpc.message.ResponseFuture继承java.util.concurrent.CompletionStage? Can you consider let com.alipay.sofa.rpc.message.ResponseFuture inherit java.util.concurrent.CompletionStage? Jun 9, 2020
@sofastack-bot
Copy link

sofastack-bot bot commented Jun 9, 2020

Hi @guyeu, we detect non-English characters in the issue. This comment is an auto translation by @sofastack-robot to help other users to understand this issue.

We encourage you to describe your issue in English which is more friendly to other users.

The existing asynchronous usage of sofarpc includes callback and Future, both of which have relatively large limitations;-Callback-service level callbacks have little significance, and functions and Filter overlap;-Call level callbacks are more cumbersome to handle , It is not intuitive to write;-Future-the current Future still has to block the current thread to wait for the result;-incompatible with the framework such as reactor; java1.8 introduced java.util.concurrent.CompletableFuture this powerful asynchronous programming auxiliary class At present, users of sofarpc can only barely achieve compatibility through this code: java public static void main(String... args) {// omit // .setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE); CompletableFuture <?> future = new CompletableFuture&lt;&gt;(); RpcInvokeContext.getContext().setResponseCallback(new CallbackImpl(future)); RemoteService.invoke(); future.thenApply(result -&gt; {// omit })} static class CallbackImpl <T> implements SofaResponseCallback <T> {private final CompletableFuture <T> future; public CallbackImpl(CompletableFuture <T> future) {this.future = future;} @Override @SuppressWarnings(&quot;unchecked&quot;) public void onAppResponse(Object appResponse, String methodName, RequestBase request) {// BTW Why is the type of appResponse at this location Object instead of 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);}} Can I consider letting com.alipay.sofa.rpc.message.ResponseFuture inherit java.util.concurrent.CompletionStage, to provide better asynchronous programming support?

@sofastack-bot sofastack-bot bot added the question Further information is requested label Jun 9, 2020
@leizhiyuan
Copy link
Contributor

以前代码是基于jdk6的。一直兼容jdk6.所以这个也没用,我们看一下

@OrezzerO OrezzerO self-assigned this Apr 20, 2021
@OrezzerO OrezzerO added the enhancement New feature or request label Apr 20, 2021
@EvenLjj
Copy link
Collaborator

EvenLjj commented Jun 3, 2021

@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.

@stale
Copy link

stale bot commented Feb 8, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix This will not be worked on label Feb 8, 2022
@EvenLjj EvenLjj removed the wontfix This will not be worked on label Feb 9, 2022
@stale
Copy link

stale bot commented Apr 10, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix This will not be worked on label Apr 10, 2022
@EvenLjj EvenLjj added the later This will be worked on in later version label Apr 10, 2022
@stale stale bot removed the wontfix This will not be worked on label Apr 10, 2022
@sofastack-bot sofastack-bot bot added the wontfix This will not be worked on label Apr 10, 2022
@EvenLjj EvenLjj removed the wontfix This will not be worked on label Apr 10, 2022
@krisjin
Copy link

krisjin commented Jul 15, 2022

@EvenLjj hi, is this issue is solved?

@nanfeng1999
Copy link

您好,我想认领这个任务

@dajitui
Copy link
Contributor

dajitui commented Apr 4, 2023

https://github.com/dajitui/sofa-rpc/actions/runs/4606971144
我拉了一个pr,但是build有问题,有大佬帮忙看看吗

@leizhiyuan
Copy link
Contributor

代码格式不对,format 一下

mvn clean -DskipTests package install

本地执行一下,会自动格式化,然后commit一下

2023-04-04T11:33:08.6981177Z Please commit your change before run this shell, un commit files:
2023-04-04T11:33:08.6982136Z  M core/api/src/main/java/com/alipay/sofa/rpc/message/AbstractResponseFuture.java
2023-04-04T11:33:08.7005322Z ##[error]Process completed with exit code 1.
2023-04-04T11:33:08.7022164Z ##[debug]Finishing: Build with Maven
2023-04-04T11:33:08.7032852Z ##[debug]Evaluating condition for step: 'Test with Maven'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request later This will be worked on in later version question Further information is requested
Projects
None yet
Development

No branches or pull requests

7 participants