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

1
.idea/compiler.xml generated
View File

@@ -14,6 +14,7 @@
<outputRelativeToContentRoot value="true" />
<processorPath useClasspath="false">
<entry name="$MAVEN_REPOSITORY$/org/projectlombok/lombok/1.18.32/lombok-1.18.32.jar" />
<entry name="$MAVEN_REPOSITORY$/org/projectlombok/lombok/1.18.32/lombok-1.18.32.jar" />
</processorPath>
<module name="server" />
</profile>

View File

@@ -16,7 +16,7 @@
<description>Spring Boot WebFlux + MyBatis + MySQL backend</description>
<properties>
<java.version>21</java.version>
<java.version>17</java.version>
<mybatis-plus.version>3.5.8</mybatis-plus.version>
</properties>

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

View File

@@ -53,7 +53,7 @@ public class DeviceTaskUpdateServiceTest {
List<LinkTask> tasks = Arrays.asList(task1, task2);
when(linkTaskMapper.findByMachineIdAndStatus("f1", "LOGGED_IN")).thenReturn(tasks);
when(linkTaskMapper.update(any(LinkTask.class))).thenReturn(1);
when(linkTaskMapper.updatePointsIfGreater(anyLong(), anyInt())).thenReturn(1);
when(gameCompletionDetectionService.detectGameCompletion("f1", "5300", "EVENT_LISTENER")).thenReturn(false);
// 执行测试
@@ -62,13 +62,11 @@ public class DeviceTaskUpdateServiceTest {
// 验证结果
verify(gameCompletionDetectionService).detectGameCompletion("f1", "5300", "EVENT_LISTENER");
verify(linkTaskMapper).findByMachineIdAndStatus("f1", "LOGGED_IN");
verify(linkTaskMapper, times(2)).update(any(LinkTask.class));
verify(linkTaskMapper, times(2)).updatePointsIfGreater(anyLong(), eq(5300));
// 验证任务状态仍为LOGGED_IN,但点数更新
// 验证任务状态仍为LOGGED_IN点数更新通过数据库完成,不修改内存对象)
assertEquals("LOGGED_IN", task1.getStatus());
assertEquals("LOGGED_IN", task2.getStatus());
assertEquals(Integer.valueOf(5300), task1.getCompletedPoints());
assertEquals(Integer.valueOf(5300), task2.getCompletedPoints());
}
@Test
@@ -126,7 +124,7 @@ public class DeviceTaskUpdateServiceTest {
// 验证结果
verify(gameCompletionDetectionService).detectGameCompletion("f1", "5300", "EVENT_LISTENER");
verify(linkTaskMapper).findByMachineIdAndStatus("f1", "LOGGED_IN");
verify(linkTaskMapper, never()).update(any(LinkTask.class));
verify(linkTaskMapper, never()).updatePointsIfGreater(anyLong(), anyInt());
}
@Test