2

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>
liliang
  • 21
  • 4

1 Answers1

1

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

Yuri Schimke
  • 12,435
  • 3
  • 35
  • 69
  • I write otherOkHttpClient just demo new other OkHttpClient Instance timeout different with the defaulted to 10 seconds, If 20 seconds,30 seconds....So,how to do that,my biz logic has 100 supplier http interfaces to invoke. – liliang Feb 27 '18 at 01:50