Commit bdd9ff1fd7a176f4518d2215770f37b042cfc970
1 parent
0629d4cf
优化okhttp并发参数
Showing
1 changed file
with
22 additions
and
17 deletions
src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java
| ... | ... | @@ -23,27 +23,34 @@ public class ZLMRESTfulUtils { |
| 23 | 23 | |
| 24 | 24 | private final static Logger logger = LoggerFactory.getLogger(ZLMRESTfulUtils.class); |
| 25 | 25 | |
| 26 | - | |
| 27 | - | |
| 26 | + private OkHttpClient client; | |
| 28 | 27 | |
| 29 | 28 | public interface RequestCallback{ |
| 30 | 29 | void run(JSONObject response); |
| 31 | 30 | } |
| 32 | 31 | |
| 33 | 32 | private OkHttpClient getClient(){ |
| 34 | - OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder(); | |
| 35 | - //todo 暂时写死超时时间 均为5s | |
| 36 | - httpClientBuilder.connectTimeout(5,TimeUnit.SECONDS); //设置连接超时时间 | |
| 37 | - httpClientBuilder.readTimeout(5,TimeUnit.SECONDS); //设置读取超时时间 | |
| 38 | - if (logger.isDebugEnabled()) { | |
| 39 | - HttpLoggingInterceptor logging = new HttpLoggingInterceptor(message -> { | |
| 40 | - logger.debug("http请求参数:" + message); | |
| 41 | - }); | |
| 42 | - logging.setLevel(HttpLoggingInterceptor.Level.BASIC); | |
| 43 | - // OkHttp進行添加攔截器loggingInterceptor | |
| 44 | - httpClientBuilder.addInterceptor(logging); | |
| 33 | + if (client == null) { | |
| 34 | + OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder(); | |
| 35 | + //todo 暂时写死超时时间 均为5s | |
| 36 | + // 设置连接超时时间 | |
| 37 | + httpClientBuilder.connectTimeout(5,TimeUnit.SECONDS); | |
| 38 | + // 设置读取超时时间 | |
| 39 | + httpClientBuilder.readTimeout(5,TimeUnit.SECONDS); | |
| 40 | + // 设置连接池 | |
| 41 | + httpClientBuilder.connectionPool(new ConnectionPool(16, 10, TimeUnit.SECONDS)); | |
| 42 | + if (logger.isDebugEnabled()) { | |
| 43 | + HttpLoggingInterceptor logging = new HttpLoggingInterceptor(message -> { | |
| 44 | + logger.debug("http请求参数:" + message); | |
| 45 | + }); | |
| 46 | + logging.setLevel(HttpLoggingInterceptor.Level.BASIC); | |
| 47 | + // OkHttp進行添加攔截器loggingInterceptor | |
| 48 | + httpClientBuilder.addInterceptor(logging); | |
| 49 | + } | |
| 50 | + client = httpClientBuilder.build(); | |
| 45 | 51 | } |
| 46 | - return httpClientBuilder.build(); | |
| 52 | + return client; | |
| 53 | + | |
| 47 | 54 | } |
| 48 | 55 | |
| 49 | 56 | |
| ... | ... | @@ -164,9 +171,7 @@ public class ZLMRESTfulUtils { |
| 164 | 171 | .build(); |
| 165 | 172 | logger.info(request.toString()); |
| 166 | 173 | try { |
| 167 | - OkHttpClient client = new OkHttpClient.Builder() | |
| 168 | - .readTimeout(10, TimeUnit.SECONDS) | |
| 169 | - .build(); | |
| 174 | + OkHttpClient client = getClient(); | |
| 170 | 175 | Response response = client.newCall(request).execute(); |
| 171 | 176 | if (response.isSuccessful()) { |
| 172 | 177 | if (targetPath != null) { | ... | ... |