From 73e608df889ebeca1c87cb8d77676923b8872a83 Mon Sep 17 00:00:00 2001
From: dd <gitluke@outlook.com>
Date: Fri, 26 Dec 2025 02:40:01 +0800
Subject: [PATCH] 1
---
src/main/java/com/nq/service/impl/PriceServicesImpl.java | 83 +++++++++++++++++++++++++----------------
1 files changed, 51 insertions(+), 32 deletions(-)
diff --git a/src/main/java/com/nq/service/impl/PriceServicesImpl.java b/src/main/java/com/nq/service/impl/PriceServicesImpl.java
index f33d4db..b9dab10 100644
--- a/src/main/java/com/nq/service/impl/PriceServicesImpl.java
+++ b/src/main/java/com/nq/service/impl/PriceServicesImpl.java
@@ -109,58 +109,77 @@
}
}
- public String doGet(String pid){
- String apiUrl = PropertiesUtil.getProperty("JS_IN_HTTP_URL") + "stock?key=" + PropertiesUtil.getProperty("JS_IN_KEY");
- String result = null;
+ private static String lastResult = null;
+ private static long lastExecuteTime = 0;
+ private static final long MIN_INTERVAL_MS = 3000;
+ private static final Object cacheLock = new Object();
- try {
- // 添加请求间隔,避免频率过高
- Thread.sleep(1000); // 等待1秒再请求
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
+ public static String doGet(String pid) {
+ long currentTime = System.currentTimeMillis();
+
+ // 第一次快速检查(不加锁)
+ if (lastResult != null && (currentTime - lastExecuteTime) < MIN_INTERVAL_MS) {
+ return lastResult;
}
+
+ // 同步块内再次检查并执行
+ synchronized (cacheLock) {
+ currentTime = System.currentTimeMillis();
+ if (lastResult != null && (currentTime - lastExecuteTime) < MIN_INTERVAL_MS) {
+ return lastResult;
+ }
+
+ // 执行POST请求
+ String newResult = doActualPost(pid);
+
+ // 更新缓存
+ lastResult = newResult;
+ lastExecuteTime = System.currentTimeMillis();
+
+ return newResult;
+ }
+ }
+
+ private static String doActualPost(String pid) {
+ String apiUrl = PropertiesUtil.getProperty("JS_IN_HTTP_URL") + "stock?key=" + PropertiesUtil.getProperty("JS_IN_KEY");
try {
URL url = new URL(apiUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- // 设置超时时间
- connection.setConnectTimeout(10000);
- connection.setReadTimeout(10000);
connection.setRequestMethod("POST");
- connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
+ connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setDoOutput(true);
+ connection.setConnectTimeout(5000);
+ connection.setReadTimeout(10000);
- // JSON格式请求体
- String jsonInputString = "{\"pid\":\"" + pid + "\"}";
+ String postData = "pid=" + pid;
try (OutputStream os = connection.getOutputStream()) {
- byte[] input = jsonInputString.getBytes("utf-8");
+ byte[] input = postData.getBytes("utf-8");
os.write(input, 0, input.length);
}
- // 检查HTTP响应码
- int responseCode = connection.getResponseCode();
- if (responseCode == 429) {
- // 如果还是429,等待更长时间后重试
- Thread.sleep(3000);
- return doGet(pid); // 递归重试(注意设置最大重试次数)
- }
-
// 读取响应
- try (BufferedReader in = new BufferedReader(
- new InputStreamReader(connection.getInputStream()))) {
- String inputLine;
- StringBuilder response = new StringBuilder();
- while ((inputLine = in.readLine()) != null) {
- response.append(inputLine);
- }
- result = response.toString();
+ BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
+ String inputLine;
+ StringBuilder response = new StringBuilder();
+
+ while ((inputLine = in.readLine()) != null) {
+ response.append(inputLine);
}
+ in.close();
+
+ return response.toString();
+
} catch (Exception e) {
e.printStackTrace();
+ // 返回缓存或错误信息
+ if (lastResult != null) {
+ return lastResult;
+ }
+ return "{\"error\":\"请求失败:" + e.getMessage() + "\"}";
}
- return result;
}
@Override
--
Gitblit v1.9.3