|
@@ -28,29 +28,64 @@ public class PlatformService {
|
|
|
@Value("${loginKey}")
|
|
@Value("${loginKey}")
|
|
|
private String loginKey;
|
|
private String loginKey;
|
|
|
|
|
|
|
|
|
|
+ @Value("${maxRetries}")
|
|
|
|
|
+ private int maxRetries;
|
|
|
|
|
+
|
|
|
|
|
+ @Value("${retryDelay}")
|
|
|
|
|
+ private long retryDelay;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
public List<Map<String, String>> getAllData(){
|
|
public List<Map<String, String>> getAllData(){
|
|
|
-// System.out.println("---------------------------------------------------------------");
|
|
|
|
|
List<Map<String, String>> outputList = new ArrayList<>();
|
|
List<Map<String, String>> outputList = new ArrayList<>();
|
|
|
- String urlStr = UrlAddr+"api/getToken?"+loginKey;
|
|
|
|
|
|
|
+ String urlStr = UrlAddr + "api/getToken?" + loginKey;
|
|
|
log.info("1:url: {}", urlStr);
|
|
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 = "";
|
|
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()) {
|
|
if (!token.isEmpty()) {
|
|
|
log.info("2: Requesting RealTimeData with token in header");
|
|
log.info("2: Requesting RealTimeData with token in header");
|