From bd742b9f1636942a36bf05f480b95f791ba9c525 Mon Sep 17 00:00:00 2001 From: zcxlsm Date: Sun, 3 Aug 2025 17:43:08 +0800 Subject: [PATCH] =?UTF-8?q?refactor(sis):=20=E5=B8=AE=E5=8A=A9=E7=B1=BB?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dromara/sis/runner/HuaweiBoxRunner.java | 105 ++++++++++++++++++ .../dromara/sis/sdk/e8/utils/E8ApiUtil.java | 12 +- .../sis/sdk/huawei/utils/HuaWeiHttp.java | 77 ++----------- 3 files changed, 123 insertions(+), 71 deletions(-) create mode 100644 ruoyi-modules/Sis/src/main/java/org/dromara/sis/runner/HuaweiBoxRunner.java diff --git a/ruoyi-modules/Sis/src/main/java/org/dromara/sis/runner/HuaweiBoxRunner.java b/ruoyi-modules/Sis/src/main/java/org/dromara/sis/runner/HuaweiBoxRunner.java new file mode 100644 index 00000000..f926f6c9 --- /dev/null +++ b/ruoyi-modules/Sis/src/main/java/org/dromara/sis/runner/HuaweiBoxRunner.java @@ -0,0 +1,105 @@ +package org.dromara.sis.runner; + +import cn.hutool.http.HttpRequest; +import cn.hutool.http.HttpResponse; +import cn.hutool.json.JSONUtil; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.dromara.common.redis.utils.RedisUtils; +import org.dromara.sis.sdk.huawei.utils.HuaWeiHttp; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.stereotype.Component; + +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + +/** + * @author lsm + * @apiNote HuaweiBoxRunner + * @since 2025/8/3 + */ +@Slf4j +@Component +@RequiredArgsConstructor +public class HuaweiBoxRunner implements ApplicationRunner { + + @Value("${huawei.url}") + private String BASE_URL; + + private final HuaWeiHttp huaWeiHttp; + + @Override + public void run(ApplicationArguments args) throws Exception { + Boolean login = huaWeiHttp.login(); + if (login) { + log.info("华为盒子登录成功"); + this.keepAlive(); + } else { + log.error("华为盒子登录失败"); + } + } + + // 保活 + 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.get(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("心跳调度器已关闭"); + } + } +} diff --git a/ruoyi-modules/Sis/src/main/java/org/dromara/sis/sdk/e8/utils/E8ApiUtil.java b/ruoyi-modules/Sis/src/main/java/org/dromara/sis/sdk/e8/utils/E8ApiUtil.java index e7f9b45c..e23dac16 100644 --- a/ruoyi-modules/Sis/src/main/java/org/dromara/sis/sdk/e8/utils/E8ApiUtil.java +++ b/ruoyi-modules/Sis/src/main/java/org/dromara/sis/sdk/e8/utils/E8ApiUtil.java @@ -5,6 +5,7 @@ import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import cn.hutool.json.JSONUtil; import org.dromara.sis.sdk.e8.domain.ApiResp; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.util.ObjectUtils; @@ -21,9 +22,14 @@ import java.util.stream.Collectors; @Component public class E8ApiUtil { - private static final String BASE_URL = "http://192.168.24.8:50014"; - private static final String SECRET_KEY = "ZG4ocLq1"; - private static final String KEY = "b97c7090379f490bb4b2ead0f57fd1bf"; + @Value("e8.url") + private String BASE_URL; + + @Value("e8.secretKey") + private String SECRET_KEY; + + @Value("e8.key") + private String KEY; /** * 发起Post请求 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 6141c570..63f15cca 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 @@ -8,13 +8,10 @@ import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import lombok.extern.slf4j.Slf4j; import org.dromara.common.redis.utils.RedisUtils; +import org.springframework.beans.factory.annotation.Value; 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 @@ -25,9 +22,14 @@ import java.util.concurrent.TimeUnit; @Component public class HuaWeiHttp { - private static final String BASE_URL = "https://192.168.24.100:18531"; - private static final String USERNAME = "huawei"; - private static final String PASSWORD = "qweasd123"; + @Value("${huawei.url}") + private String BASE_URL; + + @Value("${huawei.username}") + private String USERNAME; + + @Value("${huawei.password}") + private String PASSWORD; // 每次调用请求需要登录,获取JSESSIONID public Boolean login() { @@ -48,7 +50,6 @@ public class HuaWeiHttp { 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()); @@ -57,66 +58,6 @@ 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请求 *