瀏覽代碼

从云平台获取数据时如果报错,增加重试机制,并通过配置设置等待时间和最大重试次数

huangws 8 月之前
父節點
當前提交
3f1c79c9fc

+ 53 - 18
GetDataByRESTful/src/main/java/com/jyxt/getdatabyrestful/service/PlatformService.java

@@ -28,29 +28,64 @@ public class PlatformService {
     @Value("${loginKey}")
     private String loginKey;
 
+    @Value("${maxRetries}")
+    private int maxRetries;
+
+    @Value("${retryDelay}")
+    private long retryDelay;
+
+
     public List<Map<String, String>> getAllData(){
-//        System.out.println("---------------------------------------------------------------");
         List<Map<String, String>> outputList = new ArrayList<>();
-        String urlStr = UrlAddr+"api/getToken?"+loginKey;
+        String urlStr = UrlAddr + "api/getToken?" + loginKey;
         log.info("1:url: {}", urlStr);
-        String retVal = apiClientService.sendGet(urlStr);
-        log.info("1:retVal: {}", retVal);
+
+        // 重试配置 - 任何异常都重试
+//        final int maxRetries = 12;
+//        final long retryDelay = 6_000; // 10分钟(毫秒)
+        String retVal = null;
         String token = "";
-        try {
-            // 解析JSON字符串
-            ObjectMapper mapper = new ObjectMapper();
-            Map<String, Object> response = mapper.readValue(retVal, new TypeReference<Map<String, Object>>() {});
-
-            // 提取data字段
-            Map<String, Object> data = (Map<String, Object>) response.get("data");
-            if (data != null) {
-                token = (String) data.get("token"); // 提取token值
-                log.info("Extracted token: {}", token);
-            } else {
-                log.error("'data' field is missing in response");
+        boolean tokenObtained = false;
+
+        // 重试循环(最多12次)
+        for (int attempt = 1; attempt <= maxRetries; attempt++) {
+            try {
+                log.info("Attempt {}/{} to get token", attempt, maxRetries);
+                retVal = apiClientService.sendGet(urlStr);
+                log.info("1:retVal: {}", retVal);
+
+                // 解析响应获取token
+                ObjectMapper mapper = new ObjectMapper();
+                Map<String, Object> response = mapper.readValue(retVal, new TypeReference<Map<String, Object>>() {});
+                Map<String, Object> data = (Map<String, Object>) response.get("data");
+                if (data != null) {
+                    token = (String) data.get("token");
+                    if (token != null && !token.isEmpty()) {
+                        log.info("Extracted token: {}", token);
+                        tokenObtained = true;
+                        break; // 成功获取token,跳出循环
+                    }
+                }
+                log.warn("Token extraction failed in attempt {}/{}", attempt, maxRetries);
+            } catch (Exception e) {
+                log.error("Attempt {}/{} failed: {}", attempt, maxRetries, e.getMessage());
             }
-        } catch (Exception e) {
-            log.error("JSON parsing failed", e);
+
+            // 如果未成功且还有重试机会,则等待
+            if (attempt < maxRetries) {
+                log.info("Waiting {} minutes before next attempt...", retryDelay / 60000);
+                try {
+                    Thread.sleep(retryDelay);
+                } catch (InterruptedException ie) {
+                    Thread.currentThread().interrupt();
+                    throw new RuntimeException("Retry wait interrupted", ie);
+                }
+            }
+        }
+
+        // 检查是否成功获取token
+        if (!tokenObtained) {
+            throw new RuntimeException("Unable to obtain token after " + maxRetries + " attempts");
         }
         if (!token.isEmpty()) {
             log.info("2: Requesting RealTimeData with token in header");

+ 3 - 1
GetDataByRESTful/src/main/resources/application.properties

@@ -11,4 +11,6 @@ logging.level.root=INFO
 config.qrymode=1
 UrlAddr=http://www.0531yun.com/
 loginKey=loginName=h241021fkj&password=h241021fkj
-api.resources.path=/ibps/platform/v3/auth/resources/getDefaultResources
+api.resources.path=/ibps/platform/v3/auth/resources/getDefaultResources
+maxRetries=12
+retryDelay=600000