diff --git a/src/main/java/com/tencentcloudapi/common/http/HttpConnection.java b/src/main/java/com/tencentcloudapi/common/http/HttpConnection.java index f837274e79..146a398478 100644 --- a/src/main/java/com/tencentcloudapi/common/http/HttpConnection.java +++ b/src/main/java/com/tencentcloudapi/common/http/HttpConnection.java @@ -19,12 +19,15 @@ import com.tencentcloudapi.common.exception.TencentCloudSDKException; import okhttp3.*; +import okhttp3.internal.Util; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.X509TrustManager; import java.io.IOException; import java.net.Proxy; +import java.util.concurrent.SynchronousQueue; +import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class HttpConnection { @@ -33,7 +36,7 @@ public class HttpConnection { // https://github.com/square/okhttp/issues/3372 // Creating dispatcher and connectionPool is expensive. // Share them between OkHttpClients by singleton's Builder. - private static final OkHttpClient clientSingleton = new OkHttpClient(); + private static final OkHttpClient clientSingleton = newOkhttpClient(); private OkHttpClient client; public HttpConnection(Integer connTimeout, Integer readTimeout, Integer writeTimeout) { @@ -150,4 +153,11 @@ public void setHttpClient(Object httpClient) { public Object getHttpClient() { return client; } + + private static OkHttpClient newOkhttpClient() { + ThreadPoolExecutor executor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60, TimeUnit.SECONDS, + new SynchronousQueue(), Util.threadFactory("OkHttp Dispatcher", true)); + Dispatcher dispatcher = new Dispatcher(executor); + return new OkHttpClient.Builder().dispatcher(dispatcher).build(); + } } diff --git a/src/test/java/com/tencentcloudapi/integration/common/DaemonThreadTest.java b/src/test/java/com/tencentcloudapi/integration/common/DaemonThreadTest.java new file mode 100644 index 0000000000..52d0aed400 --- /dev/null +++ b/src/test/java/com/tencentcloudapi/integration/common/DaemonThreadTest.java @@ -0,0 +1,33 @@ +package com.tencentcloudapi.integration.common; + +import com.tencentcloudapi.common.CommonClient; +import com.tencentcloudapi.common.Credential; +import org.junit.Test; + +import static org.junit.Assert.fail; + +public class DaemonThreadTest { + + @Test + // ensure OkHttp threads are daemon to allow clean JVM shutdown + public void testOkhttpUseDaemonThread() { + Credential cred = new Credential( + System.getenv("TENCENTCLOUD_SECRET_ID"), + System.getenv("TENCENTCLOUD_SECRET_KEY") + ); + CommonClient client = new CommonClient("cvm", "2017-03-12", cred, "ap-guangzhou"); + try { + client.call( + "DescribeInstances", + "{\"Limit\": 1, \"Filters\":[{\"Name\":\"zone\",\"Values\":[\"ap-guangzhou-1\"]}]}"); + } catch (Exception e) { + fail(e.toString()); + } + + for (Thread t : Thread.getAllStackTraces().keySet()) { + if (t.getName().startsWith("OkHttp") && !t.isDaemon()) { + fail("thread should be daemon: " + t.getName()); + } + } + } +}