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 <[email protected]> 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<Integer, Session> 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 > [email protected] > To unsubscribe from this list, visit > https://www.eclipse.org/mailman/listinfo/jetty-users >
_______________________________________________ jetty-users mailing list [email protected] To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jetty-users
