Re: [jetty-users] Is static ConcurrentHashMap a reliable choice for WebSocketServlet?
Hi Lachlan, thank you for your comments! Over the weekend I have rewritten the custom WebSocketListener in my little word game to have a static ConcurrentHashMap of ConcurrentHashMaps with the outer key being user ids and with the inner keys being strings made of session.getRemoteAddress().getHostString() + ":" + session.getRemoteAddress().getPort() - because they identify the multiple Sessions for a user (when she/he uses several browser tabs or maybe uses desktop and mobile versions at the same time): https://gist.github.com/afarber/4f82205881ddb0223130f74b4e87abda And yes, I had also the structure in the servlet before and passed it by a custom WebSocketCreator. Both variants work and of course Jetty has only 1 process (I can see it on my Linux machine). At the same I have followed Joakim's advice and decreased the idle timeout to 5 minutes. And additionally I track when there is no human actions at the remote end for 5 minutes - and close those sessions at my servlet (to handle the abandoned browser tabs). I didn't have to use ping in my code... I have impression that Jetty source code is very stable and overall excellent, thank you Sorry if my mail was a bit offtopic Best regards Alex ___ jetty-users mailing list jetty-users@eclipse.org To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jetty-users
Re: [jetty-users] Is static ConcurrentHashMap a reliable choice for WebSocketServlet?
Alexander, I think the ConcurrentHashMap data structure is fine. However it doesn't necessarily need to be static. You could store it as a field in your WebSocketServlet and pass it in when you create your WebSocketListener. In your code I would only expect oldSession to be non-null when you have multiple connections for the same mUid. The standard case where you have no previous entry in the map for mUid, then the value returned by `SESSIONS.put(mUid, mSession)` will be null. Cheers, Lachlan On Sun, Feb 28, 2021 at 9:05 AM Alexander Farber wrote: > Good evening, > > In a custom WebSocketServlet in Jetty 9.4.37.v20210219 I would like to > maintain Session objects in a shared data structure. > > Is a static data structure like > > public final static Map SESSIONS = new > ConcurrentHashMap<>(); > > a good choice for that? > > I have a feeling it does not work reliably. Maybe Jetty starts several > Linux process and thus the static data structure is not shared among them? > > Because in my custom WebSocketListener I have a code: > > @Override > public void onWebSocketText(String str) { > // here the user is authenticated and mUid is found > Session oldSession = SESSIONS.put(mUid, mSession); > disconnect(oldSession); > } > > private void disconnect(Session session) { > LOG.info("disconnect: session={}", session); // surprisingly often > session is null > try { > session.close(); > session.disconnect(); > } catch (Exception ex) { > // ignore > } > } > > And often the old session printed by the above LOG is null, even though I > would expect it be non-null. > > Best regards > Alex > > ___ > jetty-users mailing list > jetty-users@eclipse.org > To unsubscribe from this list, visit > https://www.eclipse.org/mailman/listinfo/jetty-users > ___ jetty-users mailing list jetty-users@eclipse.org To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jetty-users
[jetty-users] Is static ConcurrentHashMap a reliable choice for WebSocketServlet?
Good evening, In a custom WebSocketServlet in Jetty 9.4.37.v20210219 I would like to maintain Session objects in a shared data structure. Is a static data structure like public final static Map SESSIONS = new ConcurrentHashMap<>(); a good choice for that? I have a feeling it does not work reliably. Maybe Jetty starts several Linux process and thus the static data structure is not shared among them? Because in my custom WebSocketListener I have a code: @Override public void onWebSocketText(String str) { // here the user is authenticated and mUid is found Session oldSession = SESSIONS.put(mUid, mSession); disconnect(oldSession); } private void disconnect(Session session) { LOG.info("disconnect: session={}", session); // surprisingly often session is null try { session.close(); session.disconnect(); } catch (Exception ex) { // ignore } } And often the old session printed by the above LOG is null, even though I would expect it be non-null. Best regards Alex ___ jetty-users mailing list jetty-users@eclipse.org To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jetty-users