feat: 降级Java版本至17,优化设备状态获取逻辑,添加超时处理和错误处理机制,更新数据库连接配置,修正测试用例中的方法调用

This commit is contained in:
yahaozhang
2025-09-16 11:26:36 +08:00
parent 75f116de8f
commit 093e72d191
6 changed files with 57 additions and 13 deletions

View File

@@ -9,9 +9,14 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import reactor.core.Exceptions;
import reactor.core.publisher.Mono;
import java.time.LocalDateTime;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;
@Service
@Slf4j
@@ -39,8 +44,24 @@ public class DeviceStatusCheckService {
log.debug("检查设备状态: 设备ID={}, 检查原因={}", machineId, reason);
try {
// 1. 获取设备状态
Map<String, Object> deviceStatus = scriptClient.getDeviceStatus(machineId).block();
// 1. 获取设备状态(增加超时与错误兜底)
Map<String, Object> deviceStatus = scriptClient
.getDeviceStatus(machineId)
.timeout(Duration.ofSeconds(5))
.onErrorResume(throwable -> {
Throwable root = Exceptions.unwrap(throwable);
if (root instanceof TimeoutException) {
log.warn("获取设备状态超时设备ID: {}", machineId);
} else if (root instanceof InterruptedException) {
Thread.currentThread().interrupt();
log.warn("获取设备状态被中断设备ID: {}", machineId);
} else {
log.error("获取设备状态失败设备ID: {}", machineId, root);
}
return Mono.empty();
})
.blockOptional()
.orElse(null);
if (deviceStatus == null || deviceStatus.isEmpty()) {
log.warn("获取设备状态失败设备ID: {}", machineId);
return;
@@ -61,6 +82,12 @@ public class DeviceStatusCheckService {
}
} catch (Exception e) {
Throwable root = Exceptions.unwrap(e);
if (root instanceof InterruptedException) {
Thread.currentThread().interrupt();
log.warn("检查设备状态被中断设备ID: {}", machineId);
return;
}
log.error("检查设备状态时发生异常设备ID: {}", machineId, e);
}
}

View File

@@ -4,9 +4,11 @@ import com.gameplatform.server.model.dto.device.DeviceStatusResponse;
import com.gameplatform.server.service.device.DeviceStatusCheckService;
import com.gameplatform.server.service.external.ScriptClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import reactor.core.Exceptions;
import reactor.core.publisher.Mono;
import java.time.Duration;
import java.util.List;
/**
@@ -32,8 +34,24 @@ public class DeviceStatusCheckTask {
log.debug("开始定时检查空闲设备");
try {
// 1. 获取所有设备状态
DeviceStatusResponse deviceStatus = scriptClient.checkAvailableDeviceStatus().block();
// 1. 获取所有设备状态(增加超时与错误兜底)
DeviceStatusResponse deviceStatus = scriptClient
.checkAvailableDeviceStatus()
.timeout(Duration.ofSeconds(5))
.onErrorResume(throwable -> {
Throwable root = Exceptions.unwrap(throwable);
if (root instanceof java.util.concurrent.TimeoutException) {
log.warn("获取所有设备状态超时,跳过本次检查");
} else if (root instanceof InterruptedException) {
Thread.currentThread().interrupt();
log.warn("获取所有设备状态被中断,跳过本次检查");
} else {
log.error("获取所有设备状态失败", root);
}
return Mono.empty();
})
.blockOptional()
.orElse(null);
if (deviceStatus == null) {
log.warn("获取设备状态失败,跳过本次检查");

View File

@@ -3,7 +3,7 @@ spring:
name: gameplatform-server
datasource:
url: jdbc:mysql://120.46.74.24:3306/login_task_db?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&sessionVariables=innodb_lock_wait_timeout=30
url: jdbc:mysql://192.140.164.137:3306/login_task_db?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&sessionVariables=innodb_lock_wait_timeout=30
username: login_task_db
password: 3MaXfeWJ4d6cGMrL
driver-class-name: com.mysql.cj.jdbc.Driver