在 tomcat 中使用多个线程处理 websocket 传入消息

Process websocket incomming messages using multiple threads in tomcat

据我了解(如果我错了请纠正我),在 tomcat 中传入的 websocket 消息是按顺序处理的。这意味着如果您在一个 websocket 中有 100 条传入消息,它们将仅使用一个线程 一个接一个 从消息 1 到消息 100 进行处理。

但这对我不起作用。我需要同时处理 websocket 中的传入消息,以增加我的 websocket 吞吐量。传入的消息不相互依赖,因此不需要按顺序处理。

问题是如何配置 tomcat 以便为每个 websocket 分配多个工作线程来同时处理传入的消息?

如有任何提示,我们将不胜感激。


这是 tomcat code 中我认为它阻塞每个 websocket 连接的地方(这是有道理的):

/**
 * 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();
        }
    }
}

您无法配置 Tomcat 来执行您想要的操作。您需要编写一个消息处理程序来使用消息,将其传递给 Executor(或类似的处理程序)然后 returns.