refactor(sis): 帮助类优化
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run

This commit is contained in:
zcxlsm 2025-08-03 17:43:08 +08:00
parent 5028649266
commit bd742b9f16
3 changed files with 123 additions and 71 deletions

View File

@ -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("心跳调度器已关闭");
}
}
}

View File

@ -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请求

View File

@ -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请求
*