kaptan
kaptan

Reputation: 3149

Process websocket incomming messages using multiple threads in tomcat

From what I understand (please correct me if I am wrong), in tomcat incoming websocket messages are processed sequentially. Meaning that if you have 100 incoming messages in one websocket, they will be processed using only one thread one-by-one from message 1 to message 100.

But this does not work for me. I need to concurrently process incoming messages in a websocket in order to increase my websocket throughput. The messages coming in do not depend on each other hence do not need to be processed sequentially.

The question is how to configure tomcat such that it would assign multiple worker threads per websocket to process incoming messages concurrently?

Any hint is appreciated.


This is where in tomcat code that I think it is blocking per websocket connection (which makes sense):

/**
 * Called when there is data in the ServletInputStream to process.
 *
 * @throws IOException if an I/O error occurs while processing the available
 *                     data
 */
public void onDataAvailable() throws IOException {
    synchronized (connectionReadLock) {
        while (isOpen() && sis.isReady()) {
            // Fill up the input buffer with as much data as we can
            int read = sis.read(
                    inputBuffer, writePos, inputBuffer.length - writePos);
            if (read == 0) {
                return;
            }
            if (read == -1) {
                throw new EOFException();
            }
            writePos += read;
            processInputBuffer();
        }
    }
}

Upvotes: 1

Views: 757

Answers (1)

Mark Thomas
Mark Thomas

Reputation: 16660

You can't configure Tomcat to do what you want. You need to write a message handler that consumes the message, passes it to an Executor (or similar for processing) and then returns.

Upvotes: 1

Related Questions