فهرست منبع

20241127 语义检索

lrj 9 ماه پیش
والد
کامیت
1e5b4825a7

+ 1 - 1
src/main/java/cn/cslg/pas/common/utils/DateUtils.java

@@ -16,7 +16,7 @@ import java.time.format.DateTimeFormatter;
 import java.util.*;
 import java.util.regex.Pattern;
 
-public class DateUtils {
+public class               DateUtils {
     public static final String START_TIME = " 00:00:00";
     public static final String END_TIME = " 23:59:59";
     public final static String FORMAT_STRING = "yyyy-MM-dd HH:mm:ss";

+ 39 - 7
src/main/java/cn/cslg/pas/service/common/PatentStarApiService.java

@@ -25,12 +25,11 @@ import cn.cslg.pas.service.novelty.AssoRetrieveRecordProjectService;
 import cn.cslg.pas.service.query.FormatQueryService;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.sun.istack.NotNull;
 import lombok.RequiredArgsConstructor;
 import lombok.experimental.Accessors;
 import lombok.extern.slf4j.Slf4j;
-import okhttp3.FormBody;
-import okhttp3.OkHttpClient;
-import okhttp3.Request;
+import okhttp3.*;
 import okhttp3.Response;
 import org.dom4j.Document;
 import org.dom4j.DocumentException;
@@ -189,10 +188,10 @@ public class PatentStarApiService {
         String signMd5 = FormatUtil.MD5(Sign);
         // 创建一个OkHttpClient对象
         OkHttpClient okHttpClient = new OkHttpClient.Builder()
-                .connectTimeout(600, TimeUnit.SECONDS)
-                .writeTimeout(600, TimeUnit.SECONDS)
-                .readTimeout(600, TimeUnit.SECONDS)
-                .dns(new XDns(100000))
+                .connectTimeout(1, TimeUnit.SECONDS)
+                .writeTimeout(1, TimeUnit.SECONDS)
+                .readTimeout(1, TimeUnit.SECONDS)
+                .addInterceptor(new PatentStarApiService.OkhttpInterceptor(3))
                 .build();
         // 创建一个RequestBody(参数1:数据类型 参数2传递的json串)
         FormBody.Builder builder = new FormBody.Builder();
@@ -1339,4 +1338,37 @@ public class PatentStarApiService {
     }
 
 
+    public static class OkhttpInterceptor implements Interceptor {
+        // 最大重试次数
+        private int maxRentry;
+
+        public OkhttpInterceptor(int maxRentry) {
+            this.maxRentry = maxRentry;
+        }
+
+        @NotNull
+        @Override
+        public Response intercept(@NotNull Interceptor.Chain chain) throws IOException {
+            /* 递归 2次下发请求,如果仍然失败 则返回 null ,但是 intercept must not return null.
+             * 返回 null 会报 IllegalStateException 异常
+             * */
+            return retry(chain, 0);//这个递归真的很舒服
+        }
+
+        Response retry(Chain chain, int retryCent) {
+            Request request = chain.request();
+            Response response = null;
+            try {
+               System.out.println("第" + (retryCent + 1) + "次执行发http请求.");
+                response = chain.proceed(request);
+            } catch (Exception e) {
+                if (maxRentry > retryCent) {
+                    response = retry(chain, retryCent + 1);
+                }
+            } finally {
+                return response;
+            }
+        }
+    }
+
 }