From 8f2ce95165f90e51d41b083610d00ae346f3a852 Mon Sep 17 00:00:00 2001
From: kunlong-luo <kunlong_luo@163.com>
Date: 星期四, 02 二月 2023 14:36:19 +0800
Subject: [PATCH] fix: 修复空指针和类型转换问题
---
src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java | 39 ++++++++++++++++++++++-----------------
1 files changed, 22 insertions(+), 17 deletions(-)
diff --git a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java
index bf9551d..99a695e 100644
--- a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java
+++ b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java
@@ -23,27 +23,34 @@
private final static Logger logger = LoggerFactory.getLogger(ZLMRESTfulUtils.class);
-
-
+ private OkHttpClient client;
public interface RequestCallback{
void run(JSONObject response);
}
private OkHttpClient getClient(){
- OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
- //todo 鏆傛椂鍐欐瓒呮椂鏃堕棿 鍧囦负5s
- httpClientBuilder.connectTimeout(5,TimeUnit.SECONDS); //璁剧疆杩炴帴瓒呮椂鏃堕棿
- httpClientBuilder.readTimeout(5,TimeUnit.SECONDS); //璁剧疆璇诲彇瓒呮椂鏃堕棿
- if (logger.isDebugEnabled()) {
- HttpLoggingInterceptor logging = new HttpLoggingInterceptor(message -> {
- logger.debug("http璇锋眰鍙傛暟锛�" + message);
- });
- logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
- // OkHttp閫茶娣诲姞鏀旀埅鍣╨oggingInterceptor
- httpClientBuilder.addInterceptor(logging);
+ if (client == null) {
+ OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
+ //todo 鏆傛椂鍐欐瓒呮椂鏃堕棿 鍧囦负5s
+ // 璁剧疆杩炴帴瓒呮椂鏃堕棿
+ httpClientBuilder.connectTimeout(5,TimeUnit.SECONDS);
+ // 璁剧疆璇诲彇瓒呮椂鏃堕棿
+ httpClientBuilder.readTimeout(5,TimeUnit.SECONDS);
+ // 璁剧疆杩炴帴姹�
+ httpClientBuilder.connectionPool(new ConnectionPool(16, 5, TimeUnit.MINUTES));
+ if (logger.isDebugEnabled()) {
+ HttpLoggingInterceptor logging = new HttpLoggingInterceptor(message -> {
+ logger.debug("http璇锋眰鍙傛暟锛�" + message);
+ });
+ logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
+ // OkHttp閫茶娣诲姞鏀旀埅鍣╨oggingInterceptor
+ httpClientBuilder.addInterceptor(logging);
+ }
+ client = httpClientBuilder.build();
}
- return httpClientBuilder.build();
+ return client;
+
}
@@ -164,9 +171,7 @@
.build();
logger.info(request.toString());
try {
- OkHttpClient client = new OkHttpClient.Builder()
- .readTimeout(10, TimeUnit.SECONDS)
- .build();
+ OkHttpClient client = getClient();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
if (targetPath != null) {
--
Gitblit v1.8.0