refactor(sis): 华为盒子心跳保活

This commit is contained in:
zcxlsm 2025-07-12 15:45:36 +08:00
parent 6246f897cb
commit 43171c2cac

View File

@ -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();
}
}