fix: ASW-148 使用虚拟线程检查 session 超时

This commit is contained in:
zhangshuai
2024-11-08 12:27:38 +08:00
parent 37299e7180
commit c83e56fc80

View File

@@ -23,9 +23,6 @@ import java.net.http.HttpClient;
import java.net.http.WebSocket; import java.net.http.WebSocket;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.concurrent.CompletionStage; import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@Component @Component
public class EnvironmentNovncWebSocketHandler extends TextWebSocketHandler { public class EnvironmentNovncWebSocketHandler extends TextWebSocketHandler {
@@ -53,15 +50,12 @@ public class EnvironmentNovncWebSocketHandler extends TextWebSocketHandler {
private IEnvironmentSessionService environmentSessionService; private IEnvironmentSessionService environmentSessionService;
private ScheduledExecutorService scheduler;
public EnvironmentNovncWebSocketHandler() { public EnvironmentNovncWebSocketHandler() {
} }
public EnvironmentNovncWebSocketHandler(IEnvironmentService deviceService, IEnvironmentSessionService environmentSessionService, Integer sessionTimeout) { public EnvironmentNovncWebSocketHandler(IEnvironmentService deviceService, IEnvironmentSessionService environmentSessionService, Integer sessionTimeout) {
this.environmentService = deviceService; this.environmentService = deviceService;
this.environmentSessionService = environmentSessionService; this.environmentSessionService = environmentSessionService;
this.sessionTimeout = sessionTimeout; this.sessionTimeout = sessionTimeout;
this.scheduler = Executors.newSingleThreadScheduledExecutor();
} }
private void initFieldVal(WebSocketSession session) { private void initFieldVal(WebSocketSession session) {
@@ -195,10 +189,6 @@ public class EnvironmentNovncWebSocketHandler extends TextWebSocketHandler {
envWebsocket.sendClose(WebSocket.NORMAL_CLOSURE, "Normal closure"); envWebsocket.sendClose(WebSocket.NORMAL_CLOSURE, "Normal closure");
} }
Constants.ENV_NOVNC_WEBSOCKET_SESSION.remove(sessionId); Constants.ENV_NOVNC_WEBSOCKET_SESSION.remove(sessionId);
if (scheduler != null && !scheduler.isShutdown()) {
scheduler.shutdownNow(); // 停止调度器
scheduler.close();
}
super.afterConnectionClosed(session, status); super.afterConnectionClosed(session, status);
} }