Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[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<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


Back to the top