首页 文章

如何使用Retrofit 2.0限制并行请求?

提问于
浏览
2

我的目标是通过抛出异常来限制并行执行请求的数量 .

例如,我只想要一个执行请求:

someApi.getUser(
    result -> print("ok: " + result), exception -> print("error: " + exception)
); // this request will be executed in 5 seconds

someApi.getServerInfo(
    result -> print("ok: " + result), exception -> print("error: " + exception)
); // there I want to catch exception like ExecutorIsBusy

如何使用Retrofit 2.0实现它?

2 回答

  • 1

    我不确定抛出 Exception 是最好的方法,但我不知道你的用例所以我不会讨论这一点:)

    无论如何,@丹尼尔的评论实际上指向了一个相当不错的方向 . 如果您正在使用 retrofitOkHttp ,则 OkHttpClient 将处理"concurrent requests"内容 . 阅读文档,您可以看到OkHttp使用 Dispatcher 来处理并行异步请求(Dispatcher docs) .

    所以有两点有趣:

    • 方法 setMaxRequests(int maxRequests) :定义最大并发请求数

    • 方法 executed(RealCall call) :实际执行请求

    我认为你可以做到这一点来实现你的目标:

    • 创建自定义 Dispatcher

    • 如果请求的电流数超过 maxRequests ,则覆盖 executed(RealCall call) 方法抛出异常

    • 使用 OkHttpClient 中使用的自定义 Dispatcher retrofit

  • 1

    使用RxJava并采用上述注释中的方法,这是一个例子:

    假设这些操作附加到按钮上 .

    public void runObservable1(View view) {
        if (!taskRunning){
            try{
                taskRunning = true;
                subsonicService.runTask1()
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .compose(this.<Result>bindUntilEvent(ActivityEvent.DESTROY))
                        .subscribe(new Subscriber<Result>() {
                            @Override
                            public void onCompleted() {
                                taskRunning = false;
                            }
    
                            @Override
                            public void onError(Throwable e) {
    
                            }
    
                            @Override
                            public void onNext(Result result) {
                                //your logic here
                            }
                        });
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            Toast.makeText(this, "Task is running you must wait", Toast.LENGTH_SHORT).show();
        }
    
    
    }
    
    public void runObservable2(View view) {
    
        if (!taskRunning){
            try{
                taskRunning = true;
                subsonicService.runTask2()
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .compose(this.<Result>bindUntilEvent(ActivityEvent.DESTROY))
                        .subscribe(new Subscriber<Result>() {
                            @Override
                            public void onCompleted() {
                                taskRunning = false;
                            }
    
                            @Override
                            public void onError(Throwable e) {
    
                            }
    
                            @Override
                            public void onNext(Result result) {
                                //Logic here
                            }
                        });
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            Toast.makeText(this, "Task is running you must wait", Toast.LENGTH_SHORT).show();
        }
    }
    

    我也不是RxJava的专家,因此可能有一个操作员可以使这更容易 .

相关问题