Search code examples
websocketnettythreadpooltomcat8servlet-3.1

Tomcat 8.5 threading model


My application uses Web Sockets via JSR 356 implementation by Tomcat 8.5 (servlet 3.1). According to the JSR, it is supposed to use NIO but I am not sure to understand.

When a client requests a Web Socket connection, a permanent conversation is created between the client and the server. In this case, I would like to know if each request by client or server is treated by a different thread of the Tomcat pool or if the same thread is always dedicated to this connection.

Moreover, how does this thread treat the request ? Does it wait for a JDBC call for example (blocking IO) or is it released in this case (NIO) ? Compared to Netty well known to use only NIO, how the Tomcat threading model is different/similar in the websocket case ?


Solution

  • A thread is allocated from the pool to process incoming data on a WebSocket connection when there is data to process. Once complete the thread returns to the pool. It is possible (likely in fact) that a different thread will be used each time.

    Outgoing messages will use blocking or non-blocking IO depending on which part of the API is used.

    JDBC calls typically block but that is controlled by the JDBC driver. How the application handles JDBC calls and the relationship - if any - to WebSocket messages is up to the application.