当与Retrofit RxJavaCallAdapterFactory一起使用时,Okhttp会忽略Dispatcher设置 [英] Okhttp ignores Dispatcher setting when used with Retrofit RxJavaCallAdapterFactory

查看:1620
本文介绍了当与Retrofit RxJavaCallAdapterFactory一起使用时,Okhttp会忽略Dispatcher设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下OkHttp和Retrofit的初始化:

Consider the following initializations of OkHttp and Retrofit:

public static SomeServiceRestInterface newRestService(String apiUrl) {
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(apiUrl)
                    .client(createOkHttpClient())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io()))
                    .addConverterFactory(createGsonConverter())
                    .build();
            return retrofit.create(SomeServiceRestInterface.class);
}

private static OkHttpClient createOkHttpClient() {
                    Dispatcher dispatcher = new Dispatcher();
                    dispatcher.setMaxRequestsPerHost(1);
                    dispatcher.setMaxRequests(1);
                    OkHttpClient.Builder builder = new OkHttpClient.Builder()
                                .dispatcher(dispatcher).build()
}

在测试其余调用时,我注意到Okhttp根本不接受setMaxRequestsPerHost或setMaxRequests设置.这是同时发送的3个请求的日志:

When testing the rest calls, I've noticed that Okhttp doesn't honor the setMaxRequestsPerHost or setMaxRequests settings at all. Here is the log of 3 requests dispatched simultaneously:

23/07 04:14:22.668 [RxIoScheduler-4] DEBUG - --> POST https://XXX/1 http/1.1
23/07 04:14:22.668 [RxIoScheduler-4] DEBUG - Content-Length: 0
23/07 04:14:22.668 [RxIoScheduler-4] DEBUG - --> END POST (0-byte body)
23/07 04:14:22.672 [RxIoScheduler-7] DEBUG - --> POST https://XXX/2 http/1.1
23/07 04:14:22.673 [RxIoScheduler-7] DEBUG - Content-Length: 0
23/07 04:14:22.673 [RxIoScheduler-7] DEBUG - --> END POST (0-byte body)
23/07 04:14:22.676 [RxIoScheduler-6] DEBUG - --> POST https://XXX/3 http/1.1
23/07 04:14:22.677 [RxIoScheduler-6] DEBUG - Content-Length: 0
23/07 04:14:22.677 [RxIoScheduler-6] DEBUG - --> END POST (0-byte body)

其中XXX是相同的域,而1/2/3是不同的路径.

where XXX is the same domain, 1/2/3 are different paths.

我不确定为什么,但是我认为这可能与addCallAdapterFactory中设置的RxJava Scheduler有关.

I'm not sure why but I thought this possibly has to do with the RxJava Scheduler set in addCallAdapterFactory.

这是一个错误吗?还是我想念什么?

Is this a bug? or am I missing something?

我正在使用okhttp 3.4.1,并进行了改造2.1.0.

I'm using okhttp 3.4.1, and retrofit 2.1.0.

推荐答案

在此问题上引用杰克·沃顿(Jake Wharton):

To quote Jake Wharton on this issue:

Observable for Retrofit的实现执行请求 同步依赖于应用的调度程序进行任何必要的操作 限制.如果您需要OkHttp的Dispatcher的限制 荣幸,那么您将不得不为Observable编写一个自定义的CallAdapter 使用Call.enqueue而不是Call.execute.

The implementation of Observable for Retrofit executes requests synchronously relying on the applied Scheduler for any necessarily limiting. If you need the limits from OkHttp's Dispatcher to be honored, then you'll have to write a custom CallAdapter for Observable which uses Call.enqueue instead of Call.execute.

我们目前没有计划支持此计划,尽管很可能 在假设的OkHttp v4上构建的翻新版v3可能会使它成为 默认值(尽管还有很长的路要走).

We currently have no plans to support this, although it's likely that Retrofit v3 built on a hypothetical OkHttp v4 might make this the default (though this is a long way off).

如果您使用了Retrofit's Call,这将与您看到的行为相同 并称为.execute(),甚至使用OkHttp的Call及其.execute().

This is the same behavior you would see if you used Retrofit's Call and called .execute(), or even used OkHttp's Call with its .execute().

这篇关于当与Retrofit RxJavaCallAdapterFactory一起使用时,Okhttp会忽略Dispatcher设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆