使用tomcat中的多个线程处理websocket包含消息

时间:2015-10-27 00:16:40

标签: multithreading tomcat concurrency websocket throughput

根据我的理解(如果我错了请纠正我),在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();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

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