1
dd
2025-12-26 73e608df889ebeca1c87cb8d77676923b8872a83
1
2 files modified
99 ■■■■ changed files
src/main/java/com/nq/service/impl/PriceServicesImpl.java 59 ●●●● patch | view | raw | blame | history
src/main/java/com/nq/service/impl/StockServiceImpl.java 40 ●●●● patch | view | raw | blame | history
src/main/java/com/nq/service/impl/PriceServicesImpl.java
@@ -109,25 +109,52 @@
        }
    }
    public String doGet(String pid) {
        // 从配置中获取 API URL,并拼接 key
    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();
    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");
        String result = null;
        try {
            Thread.sleep(3000);
            URL url = new URL(apiUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 设置请求方法为 POST
            connection.setRequestMethod("POST");
            // 设置请求头
            connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
            connection.setDoOutput(true); // 允许向连接输出
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setDoOutput(true);
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(10000);
            // 构建 POST 数据
            String postData = "pid=" + pid;
            // 发送 POST 请求
            try (OutputStream os = connection.getOutputStream()) {
                byte[] input = postData.getBytes("utf-8");
                os.write(input, 0, input.length);
@@ -136,17 +163,23 @@
            // 读取响应
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
            StringBuilder response = new StringBuilder();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            result = response.toString();
            return response.toString();
        } catch (Exception e) {
            e.printStackTrace();
            // 返回缓存或错误信息
            if (lastResult != null) {
                return lastResult;
            }
            return "{\"error\":\"请求失败:" + e.getMessage() + "\"}";
        }
        return result;
    }
    @Override
src/main/java/com/nq/service/impl/StockServiceImpl.java
@@ -432,14 +432,41 @@
    }
        /*股票日线-K线*/
    /*股票日线-K线*/
    // 只需要缓存一个最近返回的Object
    private static Object lastResult = null;
    private static long lastExecuteTime = 0;
    private static final long MIN_INTERVAL_MS = 3000;
    private static final Object cacheLock = new Object();
    @Override
    public Object getKData(String pid, String interval, String stockType) {
        try {
            Thread.sleep(3000);
        }catch (Exception e){
            e.printStackTrace();
        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;
            }
            // 获取新数据
            Object newResult = doGetKData(pid, interval, stockType);
            // 更新缓存
            lastResult = newResult;
            lastExecuteTime = System.currentTimeMillis();
            return newResult;
        }
    }
    private Object doGetKData(String pid, String interval, String stockType) {
        EStockType eStockType = EStockType.getEStockTypeByCode(stockType);
        Object object = HttpUtil.get(eStockType.stockUrl + "kline?pid=" + pid + "&interval=" + interval + "&key=" + eStockType.stockKey);
        Gson gson = new Gson();
@@ -449,12 +476,13 @@
        BigDecimal nowPrice = iPriceServices.getNowPrice(stock.getStockCode());
        Map singleStock = getSingleStock(stock.getStockCode());
        StockVO stockVO = (StockVO)singleStock.get("stock");
        // 修改 List 中的最后一条数据
        kData lastData = dataList.get(dataList.size() - 1);
        lastData.setC(nowPrice.toString());
        lastData.setO(stockVO.getOpen_px());
        lastData.setH(stockVO.getToday_max());
        lastData.setL(stockVO.getToday_min());
        return gson.toJson(dataList);
    }