From 43171c2cac03feec6bcfff6f4269c1d2bb5a8d3a Mon Sep 17 00:00:00 2001 From: zcxlsm Date: Sat, 12 Jul 2025 15:45:36 +0800 Subject: [PATCH] =?UTF-8?q?refactor(sis):=20=E5=8D=8E=E4=B8=BA=E7=9B=92?= =?UTF-8?q?=E5=AD=90=E5=BF=83=E8=B7=B3=E4=BF=9D=E6=B4=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sis/sdk/huawei/utils/HuaWeiHttp.java | 91 ++++++++++++++++--- 1 file changed, 78 insertions(+), 13 deletions(-) diff --git a/ruoyi-modules/Sis/src/main/java/org/dromara/sis/sdk/huawei/utils/HuaWeiHttp.java b/ruoyi-modules/Sis/src/main/java/org/dromara/sis/sdk/huawei/utils/HuaWeiHttp.java index 08d2427..6141c57 100644 --- a/ruoyi-modules/Sis/src/main/java/org/dromara/sis/sdk/huawei/utils/HuaWeiHttp.java +++ b/ruoyi-modules/Sis/src/main/java/org/dromara/sis/sdk/huawei/utils/HuaWeiHttp.java @@ -11,6 +11,10 @@ import org.dromara.common.redis.utils.RedisUtils; import org.springframework.stereotype.Component; import java.util.Map; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; /** * @author lsm @@ -37,13 +41,14 @@ public class HuaWeiHttp { // 发送请求获取响应 // 使用 try-with-resources 确保资源释放 try (HttpResponse response = HttpRequest.post(url) - .header("Content-Type", "application/json") - .header("Cache-Control", "no-cache") - .body(jsonStr) - .execute()) { + .header("Content-Type", "application/json") + .header("Cache-Control", "no-cache") + .body(jsonStr) + .execute()) { if (response.getStatus() == 200 && response.getCookie("JSESSIONID") != null) { RedisUtils.setCacheObject("JSESSIONID", response.getCookie("JSESSIONID").toString()); RedisUtils.expire("JSESSIONID", 1800); + this.keepAlive(); return true; } else { log.error("华为盒子登录失败,msg:{}", response.body()); @@ -52,6 +57,66 @@ public class HuaWeiHttp { return false; } + // 保活 + public void keepAlive() { + // 创建单线程调度器 + ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); + String url = BASE_URL + "/common/keepAlive"; + + // 保存任务引用,用于后续取消 + final ScheduledFuture[] heartbeatFuture = new ScheduledFuture[1]; + + // 心跳任务 + Runnable heartbeatTask = () -> { + log.info("开始心跳执行华为盒子Session保活"); + // 发送请求获取响应 + // 使用 try-with-resources 确保资源释放 + try (HttpResponse response = HttpRequest.post(url) + .header("Content-Type", "application/json") + .header("Cache-Control", "no-cache") + .header("Cookie", RedisUtils.getCacheObject("JSESSIONID")) + .execute()) { + if (response.getStatus() == 200 && JSONUtil.parseObj(response.body()).getInt("resultCode") == 0) { + RedisUtils.expire("JSESSIONID", 1800); + log.info("保活成功"); + }else { + log.info("保活失败"); + shutdownHeartbeat(scheduler, heartbeatFuture[0]); + } + } catch (Exception e) { + // 异常:立即关闭心跳 + log.error("保活请求异常: {}", e.getMessage()); + shutdownHeartbeat(scheduler, heartbeatFuture[0]); + } + }; + + // 初始延迟0秒,之后每20分钟执行一次 + heartbeatFuture[0] = scheduler.scheduleAtFixedRate(heartbeatTask, 20, 20, TimeUnit.MINUTES); + log.info("心跳任务已启动,首次执行将在20分钟后"); + + // 添加关闭钩子确保程序退出时关闭调度器 + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + if (!scheduler.isShutdown()) { + scheduler.shutdown(); + log.info("程序退出,心跳调度器已关闭"); + } + })); + } + + private static void shutdownHeartbeat(ScheduledExecutorService scheduler, ScheduledFuture future) { + // 取消心跳任务 + if (future != null && !future.isCancelled()) { + future.cancel(false); + log.error("保活失败,心跳任务已取消"); + } + + // 关闭调度器 + if (!scheduler.isShutdown()) { + scheduler.shutdown(); + log.info("心跳调度器已关闭"); + } + } + /** * 发起Post请求 * @@ -70,11 +135,11 @@ public class HuaWeiHttp { String api = BASE_URL + url; try (HttpResponse response = HttpRequest.post(api) - .header("Content-Type", "application/json") - .header("Cache-Control", "no-cache") - .header("Cookie", RedisUtils.getCacheObject("JSESSIONID")) - .body(jsonStr) - .execute()) { + .header("Content-Type", "application/json") + .header("Cache-Control", "no-cache") + .header("Cookie", RedisUtils.getCacheObject("JSESSIONID")) + .body(jsonStr) + .execute()) { if (response.isOk()) { return response.body(); } else { @@ -109,10 +174,10 @@ public class HuaWeiHttp { String fullUrl = HttpUtil.urlWithForm(api, map, null, false); try (HttpResponse response = HttpRequest.delete(fullUrl) - .header("Content-Type", "application/json") - .header("Cache-Control", "no-cache") - .header("Cookie", RedisUtils.getCacheObject("JSESSIONID")) - .execute()) { + .header("Content-Type", "application/json") + .header("Cache-Control", "no-cache") + .header("Cookie", RedisUtils.getCacheObject("JSESSIONID")) + .execute()) { return response.body(); } }