Commit 2a8a3289b899944622496fb200987fe629430f4d

Authored by unknown
1 parent e945a1ed
Exists in master

1.控制OkHttp线程池数量

build.gradle
... ... @@ -59,7 +59,7 @@ uploadArchives {
59 59 authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)
60 60 }
61 61 pom.project {
62   - version '1.1.112'
  62 + version '1.1.114'
63 63 artifactId ARTIFACT_Id
64 64 groupId GROUP_ID
65 65 packaging TYPE
... ...
src/main/java/com/taover/util/UtilHttpByOkHttp.java
... ... @@ -24,8 +24,7 @@ import okhttp3.ResponseBody;
24 24  
25 25 public class UtilHttpByOkHttp {
26 26 //防止线程并发,使用threadlocal管理client
27   - private static ThreadLocal<OkHttpClient> localHttpClientForHttp = new ThreadLocal<OkHttpClient>();
28   - private static ThreadLocal<OkHttpClient> localHttpClientForHttps = new ThreadLocal<OkHttpClient>();
  27 + private static Map<String, OkHttpClient> localHttpClient = new HashMap<String, OkHttpClient>();
29 28  
30 29 public static final String METHOD_GET = "GET";
31 30 public static final String METHOD_POST = "POST";
... ... @@ -131,38 +130,38 @@ public class UtilHttpByOkHttp {
131 130 }
132 131  
133 132 public static OkHttpClient getHttpClient(String url, int timeoutInSecond){
134   - OkHttpClient client = null;
  133 + OkHttpClient client = null;
135 134 boolean isHttps = url.trim().toLowerCase().startsWith("https");
136   - if(isHttps){
137   - client = localHttpClientForHttps.get();
138   - }else{
139   - client = localHttpClientForHttp.get();
140   - }
141   -
142   - if(client != null){
143   - return client;
144   - }
145   -
146   - if(isHttps){
147   - client = buildOkHttpClientForHttps(timeoutInSecond);
148   - }else{
149   - OkHttpClient.Builder builder = new OkHttpClient.Builder();
150   - builder.connectTimeout(timeoutInSecond, TimeUnit.SECONDS)
151   - .readTimeout(timeoutInSecond, TimeUnit.SECONDS)
152   - .writeTimeout(timeoutInSecond,TimeUnit.SECONDS)
153   - .retryOnConnectionFailure(true);
154   - client = builder.build();
155   - }
156   -
157   - if(isHttps){
158   - localHttpClientForHttps.set(client);
159   - }else{
160   - localHttpClientForHttp.set(client);
161   - }
162   -
  135 + String mapKey = getHttpClientMapKey(isHttps, timeoutInSecond);
  136 + synchronized (localHttpClient) {
  137 + client = localHttpClient.get(mapKey);
  138 + if(client != null){
  139 + return client;
  140 + }
  141 +
  142 + if(isHttps){
  143 + client = buildOkHttpClientForHttps(timeoutInSecond);
  144 + }else{
  145 + OkHttpClient.Builder builder = new OkHttpClient.Builder();
  146 + builder.connectTimeout(timeoutInSecond, TimeUnit.SECONDS)
  147 + .readTimeout(timeoutInSecond, TimeUnit.SECONDS)
  148 + .writeTimeout(timeoutInSecond,TimeUnit.SECONDS)
  149 + .retryOnConnectionFailure(true);
  150 + client = builder.build();
  151 + }
  152 + localHttpClient.put(mapKey, client);
  153 + }
163 154 return client;
164 155 }
165 156  
  157 + private static String getHttpClientMapKey(boolean isHttps, int timeoutInSecond) {
  158 + if(isHttps) {
  159 + return "HTTPS_"+timeoutInSecond;
  160 + }else {
  161 + return "HTTP_"+timeoutInSecond;
  162 + }
  163 + }
  164 +
166 165 /**
167 166 * 并发请求复用连接池,避免内存溢出
168 167 * @param okHttpClient
... ...
src/test/java/OkHttpDebug.java
  1 +import java.util.Collections;
  2 +import java.util.HashMap;
  3 +import java.util.Map;
  4 +
  5 +import com.taover.util.UtilHttpByOkHttp;
1 6  
2 7 public class OkHttpDebug {
  8 + public static void main(String args[]) {
  9 + //loadRequestPerMinute(10);
  10 +
  11 + //testWarePushOrder(100);
  12 +
  13 + multiRequest(10);
  14 + }
  15 +
  16 + private static void multiRequest(int count) {
  17 + for(int sequence=0; sequence<count; ++sequence) {
  18 + new RequestThread(sequence).start();
  19 + }
  20 + }
  21 +
  22 + private static void testWarePushOrder(int pushCount) {
  23 + Map<String, Object> params = new HashMap<String, Object>();
  24 + for(int i=0; i<pushCount; ++i) {
  25 + try {
  26 + if(i%2 == 0) {
  27 + params.put("wareId", 20);
  28 + }else {
  29 + params.put("wareId", 34);
  30 + }
  31 + Thread.sleep(3000);
  32 + long starTime = System.currentTimeMillis();
  33 + String response = UtilHttpByOkHttp.sendPostForm("http://localhost/api/schedule/ware/order", null, params);
  34 + long endTime = System.currentTimeMillis();
  35 + System.out.println("success["+i+"]["+(endTime-starTime)+"]:"+response);
  36 + } catch (Exception e) {
  37 + e.printStackTrace();
  38 + }
  39 + }
  40 + }
  41 +
  42 + private static void loadRequestPerMinute(int i) {
  43 + double gapSecond = 60.0/i;
  44 + int sequence = 0;
  45 + while(true) {
  46 + ++sequence;
  47 + new RequestThread(sequence).start();
  48 + try {
  49 + Thread.sleep((long)(gapSecond*1000));
  50 + } catch (InterruptedException e) {
  51 + e.printStackTrace();
  52 + }
  53 + }
  54 + }
  55 +}
  56 +
  57 +
  58 +class RequestThread extends Thread{
  59 + int sequence = 0;
  60 + public RequestThread(int sequence) {
  61 + this.sequence = sequence;
  62 + }
3 63  
  64 + @Override
  65 + public void run() {
  66 + try {
  67 + long starTime = System.currentTimeMillis();
  68 + UtilHttpByOkHttp.sendGet("http://api.wxorder.taover.com/v1/wxorderorder?page=1&size=100&controlStatus=", Collections.singletonMap("Authorization", "bearer;eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0ZW5hbnQiOiIxNiIsInVzZXJuYW1lIjoiMTM2MjEwNTEyMzAiLCJ1c2VyaWQiOiIxNyIsImlzcyI6ImFkbWluIiwiYXVkIjoiMDk4ZjZiY2Q0NjIxZDM3M2NhZGU0ZTgzMjYyN2I0ZjYiLCJleHAiOjE2MDI4NDM5MzIsIm5iZiI6MTYwMjY3MTEzMn0.PbXwPqpFYQB7dwAeB9ELr6dho29k_YZCtcVoECeo-wM"), 60);
  69 + Thread.sleep(2000);
  70 + long endTime = System.currentTimeMillis();
  71 + System.out.println("success["+this.sequence+"]:"+(endTime-starTime));
  72 + } catch (Exception e) {
  73 + System.out.println("fail");
  74 + }
  75 + }
4 76 }
  77 +
... ...