使用tomcat中的多个线程处理websocket进入消息 [英] Process websocket incomming messages using multiple threads in tomcat

查看:1137
本文介绍了使用tomcat中的多个线程处理websocket进入消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的理解(请纠正我,如果我错了),在tomcat传入的websocket消息按顺序处理。这意味着如果您在一个WebSocket中有100个传入邮件,则它们将只使用一个线程逐个从邮件1处理到邮件100.



但这不适用于我。我需要同时处理在websocket中的传入消息,以增加我的websocket吞吐量。进入的消息不依赖于彼此,因此不需要顺序处理。



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



任何提示都可以使用。






这是 tomcat代码,我认为它是阻塞每个websocket连接(其中有意义):

  / ** 
当ServletInputStream中有数据要处理时调用。
*
* @throws IOException如果在处理可用的
*数据时发生I / O错误
* /
public void onDataAvailable()throws IOException {
synchronized(connectionReadLock){
while(isOpen()&& sis.isReady()){
//用尽可能多的数据填充输入缓冲区
int read = sis.read(
inputBuffer,writePos,inputBuffer.length - writePos);
if(read == 0){
return;
}
if(read == -1){
throw new EOFException();
}
writePos + = read;
processInputBuffer();
}
}
}


解决方案>

您不能配置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();
        }
    }
}

解决方案

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.

这篇关于使用tomcat中的多个线程处理websocket进入消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆