From 8f7db635a31f2817fa920e4d43bc8718f1bc070d Mon Sep 17 00:00:00 2001 From: chenjialing <595168663@qq.com> Date: 星期三, 15 六月 2022 14:13:43 +0800 Subject: [PATCH] 优化----okhttp连接访问超时设置,访问异常code设置为-2 --- src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java | 36 ++++++++++++++++++++++++++++++------ 1 files changed, 30 insertions(+), 6 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 5caae66..5e0d317 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 @@ -12,6 +12,7 @@ import java.io.*; import java.net.ConnectException; +import java.net.SocketTimeoutException; import java.util.HashMap; import java.util.Map; import java.util.Objects; @@ -28,6 +29,9 @@ 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); @@ -47,7 +51,10 @@ return null; } String url = String.format("http://%s:%s/index/api/%s", mediaServerItem.getIp(), mediaServerItem.getHttpPort(), api); - JSONObject responseJSON = null; + JSONObject responseJSON = new JSONObject(); + //-2鑷畾涔夋祦濯掍綋 璋冪敤閿欒鐮� + responseJSON.put("code",-2); + responseJSON.put("msg","娴佸獟浣撹皟鐢ㄥけ璐�"); FormBody.Builder builder = new FormBody.Builder(); builder.add("secret",mediaServerItem.getSecret()); @@ -78,11 +85,20 @@ response.close(); Objects.requireNonNull(response.body()).close(); } - } catch (ConnectException e) { - logger.error(String.format("杩炴帴ZLM澶辫触: %s, %s", e.getCause().getMessage(), e.getMessage())); - logger.info("璇锋鏌edia閰嶇疆骞剁‘璁LM宸插惎鍔�..."); }catch (IOException e) { logger.error(String.format("[ %s ]璇锋眰澶辫触: %s", url, e.getMessage())); + + if(e instanceof SocketTimeoutException){ + //璇诲彇瓒呮椂瓒呮椂寮傚父 + logger.error(String.format("璇诲彇ZLM鏁版嵁澶辫触: %s, %s", url, e.getMessage())); + } + if(e instanceof ConnectException){ + //鍒ゆ柇杩炴帴寮傚父锛屾垜杩欓噷鏄姤Failed to connect to 10.7.5.144 + logger.error(String.format("杩炴帴ZLM澶辫触: %s, %s", url, e.getMessage())); + } + + }catch (Exception e){ + logger.error(String.format("璁块棶ZLM澶辫触: %s, %s", url, e.getMessage())); } }else { client.newCall(request).enqueue(new Callback(){ @@ -105,8 +121,16 @@ @Override public void onFailure(@NotNull Call call, @NotNull IOException e) { - logger.error(String.format("杩炴帴ZLM澶辫触: %s, %s", e.getCause().getMessage(), e.getMessage())); - logger.info("璇锋鏌edia閰嶇疆骞剁‘璁LM宸插惎鍔�..."); + logger.error(String.format("杩炴帴ZLM澶辫触: %s, %s", call.request().toString(), e.getMessage())); + + if(e instanceof SocketTimeoutException){ + //璇诲彇瓒呮椂瓒呮椂寮傚父 + logger.error(String.format("璇诲彇ZLM鏁版嵁澶辫触: %s, %s", call.request().toString(), e.getMessage())); + } + if(e instanceof ConnectException){ + //鍒ゆ柇杩炴帴寮傚父锛屾垜杩欓噷鏄姤Failed to connect to 10.7.5.144 + logger.error(String.format("杩炴帴ZLM澶辫触: %s, %s", call.request().toString(), e.getMessage())); + } } }); } -- Gitblit v1.8.0