| | |
| | | } |
| | | } |
| | | |
| | | 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 |