【问题标题】:Process websocket incomming messages using multiple threads in tomcat使用tomcat中的多个线程处理websocket传入消息
【发布时间】:2015-10-27 00:16:40
【问题描述】:

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

【问题讨论】:

    标签: multithreading tomcat concurrency websocket throughput


    【解决方案1】:

    你不能配置 Tomcat 来做你想做的事。您需要编写一个消息处理程序来使用该消息,将其传递给 Executor(或类似的处理程序)然后返回。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-28
      • 2019-03-04
      • 1970-01-01
      • 2021-05-29
      • 1970-01-01
      相关资源
      最近更新 更多