liliang
liliang

Reputation: 21

OkHttp Per Request Timeout Settting?

Click Here To show my issue,I just want set per request timeout and other paramters, so just use:

<!-- language: lang-java -->

 private static final OkHttpClient globalOkHttpClient = new OkHttpClient.Builder().build();
 private static final OkHttpClient otherOkHttpClient10 = globalOkHttpClient.newBuilder()
.connectTimeout(10,TimeUnit.SECONDS)
.build();
 private static final OkHttpClient otherOkHttpClient20 = globalOkHttpClient.newBuilder()
.connectTimeout(20,TimeUnit.SECONDS)
.build();

But that may not reusing a single OkHttp instance? So,I just want to ask?How to set OkHttpClient timeout Per Request. My OkHttp3 maven dependency:

<!-- language: lang-xml -->

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>3.9.0</version>
</dependency>

Upvotes: 2

Views: 1499

Answers (1)

Yuri Schimke
Yuri Schimke

Reputation: 13498

Separate from your issue but worth noting, the connect timeout is defaulted to 10 seconds. so you can use a single client here.

If this is the case you shouldn't have 500k OkHttp threads. perhaps debug where each thread is started. It shouldn't be by default.

If you need per request settings, you can make what is effectively a shallow copy

https://github.com/square/okhttp/blob/master/samples/guide/src/main/java/okhttp3/recipes/PerCallSettings.java

Upvotes: 1

Related Questions