https://bz.apache.org/bugzilla/show_bug.cgi?id=62633
Bug ID: 62633
Summary: WsSession leaks if cache Session are used for group
sending
Product: Tomcat 8
Version: 8.5.31
Hardware: PC
Status: NEW
Severity: normal
Priority: P2
Component: WebSocket
Assignee: [email protected]
Reporter: [email protected]
Target Milestone: ----
Created attachment 36098
--> https://bz.apache.org/bugzilla/attachment.cgi?id=36098&action=edit
code and war
public class MySessionContainer {
private final static Map<String, WsSessionUser> WS_SESSION_USER_MAP = new
ConcurrentHashMap<>();
public static void addClient(String id, WsSessionUser user) {
WS_SESSION_USER_MAP.put(id, user);
}
public static WsSessionUser getClient(String id) {
return WS_SESSION_USER_MAP.get(id);
}
public static WsSessionUser removeClient(String id) {
return WS_SESSION_USER_MAP.remove(id);
}
}
public class WsSessionUser {
private Session session;
private ThreadPoolExecutor threadPoolExecutor = new
ThreadPoolExecutor(1, 1, 1, TimeUnit.MINUTES,
new LinkedBlockingQueue<>(2));
public WsSessionUser(Session session) {
this.session = session;
}
public void sendMessageByPool(String message) {
try {
if (!threadPoolExecutor.isShutdown()) {
threadPoolExecutor.execute(() ->
sendMessage(message));
}
} catch (RejectedExecutionException e) {
threadPoolExecutor.shutdownNow();
try {
if (session.isOpen()) {
session.close();
}
} catch (IOException ignored) {
}
}
}
public void sendMessage(String message) {
try {
if (session.isOpen()) {
session.getBasicRemote().sendText(message);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public ThreadPoolExecutor getThreadPoolExecutor() {
return threadPoolExecutor;
}
public Session getSession() {
return session;
}
}
@ServerEndpoint("/ws/server")
public class MyWebSocketServer {
@OnOpen
public void onOpen(Session session) {
System.out.println("server OnOpen");
MySessionContainer.addClient(session.getId(), new
WsSessionUser(session));
}
@OnClose
public void onClose(Session session) {
System.out.println("server OnClose");
WsSessionUser wsSessionUser =
MySessionContainer.removeClient(session.getId());
if (wsSessionUser != null) {
ThreadPoolExecutor threadPoolExecutor =
wsSessionUser.getThreadPoolExecutor();
if (!threadPoolExecutor.isShutdown()) {
threadPoolExecutor.shutdownNow();
}
try {
wsSessionUser.getSession().close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@OnMessage
public void onMessage(String message, Session session) {
System.out.println("server OnMessage");
}
@OnError
public void onError(Session session, Throwable error) {
System.out.println("server OnError");
}
}
--
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]