multithreadingjakarta-eewebsockettyrus

Is every method of ServerEndpoint executed by different thread?


I use GF 4 as JavaEE server.

This is how I understand servlet processing: There is a pool of threads and when request comes one thread from this pool is taken to process the request. After that the thread is put back to pool.

Based on the information above I suppose (I am not sure) that websockets (server end points) are processed this way: There is pool of threads, when

It all means that every method of ServerEndpoint can be executed by different threads. Is my understanding right?


Solution

  • Yes.

    The ServerEndpoint instance lives as long as the associated WebSocket session is available as Session argument during @OnOpen. During that WebSocket session, many HTTP and WebSocket requests may be fired. Each such request accounts as an individual thread.

    In other words, if your ServerEndpoint class needs to deal with instance variables in multiple methods for some reason, it must be implemented in a thread safe manner. Depending on the concrete functional requirement, you'd probably better use Session#getUserProperties() instead to carry around state associated with the WS session (think of it as session attributes).

    Noted should be that this all is regardless of the container and WS implementation used.