diff --git a/src/main/java/net/geedge/asw/common/config/websocket/EnvironmentNovncWebSocketHandler.java b/src/main/java/net/geedge/asw/common/config/websocket/EnvironmentNovncWebSocketHandler.java index d73b0c7..4d75453 100644 --- a/src/main/java/net/geedge/asw/common/config/websocket/EnvironmentNovncWebSocketHandler.java +++ b/src/main/java/net/geedge/asw/common/config/websocket/EnvironmentNovncWebSocketHandler.java @@ -69,31 +69,34 @@ public class EnvironmentNovncWebSocketHandler extends TextWebSocketHandler { this.sessionId = (String) session.getAttributes().get("sessionId"); this.userId = (String) session.getAttributes().get("userId"); Constants.ENV_NOVNC_WEBSOCKET_SESSION.put(sessionId, session); - lastReceivedTime = T.DateUtil.currentSeconds(); // 初始化接收时间 + lastReceivedTime = System.currentTimeMillis(); // 初始化接收时间 startConnectionMonitor(session); } private void startConnectionMonitor(WebSocketSession session) { - scheduler.scheduleAtFixedRate(() -> { - if (System.currentTimeMillis() - lastReceivedTime > sessionTimeout) { - try { - if (session.isOpen()) { - log.info("current no connection info, clean no real use connection finshed, sessionId: {}", sessionId); + Thread.ofVirtual().start(() -> { + while (true) { + if (System.currentTimeMillis() - lastReceivedTime > sessionTimeout) { + try { + if (session.isOpen()) { + log.info("current no connection info, clean no real use connection finshed, sessionId: {}", sessionId); - // update session status - environmentSessionService.update(new LambdaUpdateWrapper() - .set(EnvironmentSessionEntity::getStatus, 2) - .eq(EnvironmentSessionEntity::getId, sessionId)); - - session.close(CloseStatus.NORMAL.withReason("current no connection info, clean no real use connection finshed.")); + // update session status + environmentSessionService.update(new LambdaUpdateWrapper() + .set(EnvironmentSessionEntity::getStatus, 2) + .eq(EnvironmentSessionEntity::getId, sessionId)); + session.close(CloseStatus.NORMAL.withReason("current no connection info, clean no real use connection finshed.")); + break; + } + // 每5分钟检查一次 + Thread.sleep(300000); + } catch (Exception e) { + log.error(e, "Error clean no real use connection"); } - } catch (IOException e) { - log.error(e, "Error clean no real use connection"); } - scheduler.shutdown(); // 关闭调度器 } - }, 0, 1, TimeUnit.MINUTES); // 每分钟检查一次 + }); }