解决关闭WebSSH页面时session不回收导致的内存泄露问题

This commit is contained in:
2023-03-09 21:24:37 +08:00
parent 6a810a77ac
commit b30725a3e2
2 changed files with 7 additions and 6 deletions
@@ -138,7 +138,7 @@ public class CheckInstancesStatus{
hostMachine.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_ONLINE); hostMachine.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_ONLINE);
hostMachines.add(hostMachine); hostMachines.add(hostMachine);
} }
} catch (HttpException exception) { } catch (Exception exception) {
HostMachine hostMachine = hostMachineService.getById(detector.getHostMachineId()); HostMachine hostMachine = hostMachineService.getById(detector.getHostMachineId());
if (Objects.nonNull(hostMachine)) { if (Objects.nonNull(hostMachine)) {
hostMachine.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_OFFLINE); hostMachine.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_OFFLINE);
@@ -69,12 +69,12 @@ public class SshHandler {
} }
private static final AtomicInteger OnlineCount = new AtomicInteger(0); private static final AtomicInteger ONLINE_COUNT = new AtomicInteger(0);
/** /**
* concurrent包的线程安全Set,用来存放每个客户端对应的Session对象。 * concurrent包的线程安全Set,用来存放每个客户端对应的Session对象。
*/ */
private static CopyOnWriteArraySet<javax.websocket.Session> sessionSet = new CopyOnWriteArraySet<javax.websocket.Session>(); private static final CopyOnWriteArraySet<javax.websocket.Session> sessionSet = new CopyOnWriteArraySet<javax.websocket.Session>();
/** /**
@@ -90,7 +90,7 @@ public class SshHandler {
sshItem.setUser(sshLink.getName()); sshItem.setUser(sshLink.getName());
sshItem.setPassword(sshLink.getPassword()); sshItem.setPassword(sshLink.getPassword());
// 在线数加1 // 在线数加1
int cnt = OnlineCount.incrementAndGet(); int cnt = ONLINE_COUNT.incrementAndGet();
log.info("有连接加入,当前连接数为:{},sessionId={}", cnt, session.getId()); log.info("有连接加入,当前连接数为:{},sessionId={}", cnt, session.getId());
HandlerItem handlerItem = null; HandlerItem handlerItem = null;
try { try {
@@ -115,8 +115,9 @@ public class SshHandler {
*/ */
@OnClose @OnClose
public void onClose(javax.websocket.Session session) { public void onClose(javax.websocket.Session session) {
destroy(session);
sessionSet.remove(session); sessionSet.remove(session);
int cnt = OnlineCount.decrementAndGet(); int cnt = ONLINE_COUNT.decrementAndGet();
log.info("有连接关闭,当前连接数为:{}", cnt); log.info("有连接关闭,当前连接数为:{}", cnt);
} }
@@ -281,7 +282,7 @@ public class SshHandler {
System.out.println("#####:" + msg); System.out.println("#####:" + msg);
session.getBasicRemote().sendText(msg); session.getBasicRemote().sendText(msg);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace();
} }
} }
} }