Parcourir la source

1.修改api增加timeout。

zhengqiang il y a 5 ans
Parent
commit
270ca18034

+ 20 - 37
common/src/main/java/com/jpsoft/smart/modules/common/utils/LApiUtil.java

@@ -1,29 +1,11 @@
 package com.jpsoft.smart.modules.common.utils;
 
-import cn.hutool.core.date.DatePattern;
-import cn.hutool.core.date.DateTime;
-import cn.hutool.core.date.DateUtil;
 import cn.hutool.http.HttpRequest;
-import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
-import com.aliyun.oss.common.utils.HttpUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
-import org.apache.http.HttpEntity;
-import org.apache.http.client.methods.CloseableHttpResponse;
-import org.apache.http.client.methods.HttpDelete;
-import org.apache.http.entity.StringEntity;
-import org.apache.http.impl.client.CloseableHttpClient;
-import org.apache.http.impl.client.HttpClientBuilder;
-import org.apache.http.impl.client.HttpClients;
-import org.apache.http.util.EntityUtils;
 import org.springframework.stereotype.Component;
-import org.springframework.web.multipart.MultipartFile;
-
-import java.io.IOException;
 import java.math.BigDecimal;
-import java.math.BigInteger;
-import java.util.Date;
 import java.util.HashMap;
 
 /**
@@ -33,51 +15,52 @@ import java.util.HashMap;
 @Slf4j
 @Component
 public class LApiUtil {
-
+    private final static int TIMEOUT = 10*1000;
 
     public static JSONObject PostRequest(String url,HashMap map){
-
-
-        String body = HttpRequest.post(url).body(new JSONObject(map).toJSONString()).execute().body();
+        String body = HttpRequest.post(url)
+                .timeout(TIMEOUT)
+                .body(new JSONObject(map).toJSONString()).execute().body();
         JSONObject jsonbody = JSONObject.parseObject(body);
         return jsonbody;
-
-
     }
 
     public static JSONObject GetRequest(String url) throws Exception{
+        String body = HttpRequest.get(url)
+                .timeout(TIMEOUT)
+                .execute().body();
 
-        String body = HttpRequest.get(url).execute().body();
         JSONObject jsonbody = JSONObject.parseObject(body);
         return jsonbody;
-
-
     }
-    public static JSONObject DeletRequest(String url) {
-       String body = HttpRequest.delete(url).execute().body();
+
+    public static JSONObject DeleteRequest(String url) {
+       String body = HttpRequest.delete(url)
+               .timeout(TIMEOUT)
+               .execute().body();
        JSONObject jsonbody = JSONObject.parseObject(body);
        return jsonbody;
-
-
     }
 
     public static JSONObject putRequest(String url){
-        String body = HttpRequest.put(url).execute().body();
+        String body = HttpRequest.put(url)
+                .timeout(TIMEOUT)
+                .execute().body();
+
         JSONObject jsonbody = JSONObject.parseObject(body);
         return jsonbody;
     }
 
     public static JSONObject putRequest(String url,HashMap map){
-        String body = HttpRequest.put(url).body(new JSONObject(map).toJSONString()).execute().body();
+        String body = HttpRequest.put(url)
+                .timeout(TIMEOUT)
+                .body(new JSONObject(map).toJSONString()).execute().body();
+
         JSONObject jsonbody = JSONObject.parseObject(body);
         return jsonbody;
     }
 
-
-
-
     public static JSONObject getResponse(JSONObject dataJson) throws Exception{
-
         if (dataJson.getInteger("ResponseCode") == 1){
             throw new Exception("通用错误");
         }

+ 3 - 3
common/src/main/java/com/jpsoft/smart/modules/lapi/service/impl/LapiServiceImpl.java

@@ -224,7 +224,7 @@ public class LapiServiceImpl implements ILapiService {
             try {
                 //获取人员人脸库id
                 String faceDbId = getFaceDbId(deviceInfo, companyInfo.getName());
-                JSONObject jsonObject = LApiUtil.DeletRequest(deviceInfo.getIpAddress() + ":" + deviceInfo.getPort() + LApiConstant.DELETEPERSON + faceDbId + "/People/" + personInfo.getId() + "?LastChange=" + new Date().getTime());
+                JSONObject jsonObject = LApiUtil.DeleteRequest(deviceInfo.getIpAddress() + ":" + deviceInfo.getPort() + LApiConstant.DELETEPERSON + faceDbId + "/People/" + personInfo.getId() + "?LastChange=" + new Date().getTime());
                 JSONObject dataJson = jsonObject.getJSONObject("Response");
                 JSONObject response = LApiUtil.getResponse(dataJson);
                 if (dataJson.getInteger("ResponseCode") == 0 && dataJson.getInteger("StatusCode") == 0) {
@@ -354,7 +354,7 @@ public class LapiServiceImpl implements ILapiService {
 
         LapiResult lapiResult = new LapiResult();
         try {
-            JSONObject jsonObject = LApiUtil.DeletRequest(deviceInfo.getIpAddress() + ":" + deviceInfo.getPort() + LApiConstant.DELETELIBRARY + libraryId);
+            JSONObject jsonObject = LApiUtil.DeleteRequest(deviceInfo.getIpAddress() + ":" + deviceInfo.getPort() + LApiConstant.DELETELIBRARY + libraryId);
             JSONObject dataJson = jsonObject.getJSONObject("Response");
             JSONObject response = LApiUtil.getResponse(dataJson);
             lapiResult.setSuccess(true);
@@ -434,7 +434,7 @@ public class LapiServiceImpl implements ILapiService {
         for (LibraryVo libraryVo : list) {
             LapiResult lapiResult = new LapiResult();
             try {
-                JSONObject jsonObject1 = LApiUtil.DeletRequest(deviceInfo.getIpAddress() + ":" + deviceInfo.getPort() + LApiConstant.DELETELIBRARY + libraryVo.getID());
+                JSONObject jsonObject1 = LApiUtil.DeleteRequest(deviceInfo.getIpAddress() + ":" + deviceInfo.getPort() + LApiConstant.DELETELIBRARY + libraryVo.getID());
                 JSONObject dataJson1 = jsonObject1.getJSONObject("Response");
                 JSONObject response1 = LApiUtil.getResponse(dataJson1);
                 lapiResult.setSuccess(true);