Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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<Runnable>(), Util.threadFactory("OkHttp Dispatcher", true));
Dispatcher dispatcher = new Dispatcher(executor);
return new OkHttpClient.Builder().dispatcher(dispatcher).build();
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
}