Tomcat 6 中请求的工作线程重用策略
我试图找到一些有关 Web 服务器中工作线程重用策略(尤其是 Tomcat 6)的文档,但无法轻松找到任何内容,因此向专家提出了这个问题!
根据我的理解,网络服务使用工作线程池来服务 http 请求。
在我们的应用程序中,我们确实在映射中维护一些信息,其中键是当前线程(有点绕回线程本地的方式)。
我们最近在代码中发现了一个错误,我们依赖这样一个事实:当会话过期(我们配置为 2 小时)时,我们的 HttpSessionBindingListener 实现类将收到回调,并且在该方法中我们将使用当前线程来查询地图并清理一些数据。这种方法的问题在于,Web 服务器并不总是为您提供一个与 2 小时前的同一会话关联的工作线程,因此我们没有正确进行清理。
发现这个问题后,我实际上对另一个场景感到困惑。对于我们来说,我们已经看到这段代码在 80% 的情况下工作,即获得一个正确清理数据的工作线程。
我有点困惑为什么这会起作用?了解 servlet 容器内部结构的人可以对此有所了解吗?
I was trying to find some documentation about worker thread reuse policy in web servers especially tomcat 6 but could not find anything easily, so bringing this up with the experts!
As per my understanding, a web serve uses a pool of worker threads to service http requests.
In our application, we do maintain some information in a map with key being the current Thread (sort of a round about way to do thread local).
We recently spotted a bug in our code where we were relying on the fact that when a session expired (for us configured to be 2 hours), our HttpSessionBindingListener implementing class would receive get the callback and in the method we would use the current thread to query the map and clean up some data. The issue with this approach is that web server would not always give you a worker thread which was associated to the same session 2 hours ago and therefore we were not doing the clean up properly.
After spotting this issue, I am actually confused about the scenario the other way. For us we have seen this code work 80% of the times i.e. getting a worker thread which cleans up the data correctly.
I am little confused on why this ever works? Can somone with knowledge of servlet container internals shed some light on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从版本 6 开始,Tomcat 的线程池在不同组件之间共享: http://tomcat .apache.org/tomcat-6.0-doc/config/executor.html
HTTP 连接器定义连接器可用的最大线程数:http://tomcat.apache.org/tomcat-6.0-doc/config/http .html
两者都默认为 200。
如果您今天的命中率为 +-80%,我想您的应用程序 80% 的时间都没有处于重负载状态。例如,Tomcat 平均仅使用池中的 1.5 个不同线程。我不知道如果激活异步 IO (AIO) 请求处理会发生什么。建议:转向 ThreadLocal 或其他例如基于会话的解决方案。
Since version 6 Tomcat's thread pool is shared across different components: http://tomcat.apache.org/tomcat-6.0-doc/config/executor.html
The HTTP connector defines the maximum number of threads available to the connector: http://tomcat.apache.org/tomcat-6.0-doc/config/http.html
Both default to 200.
If you have a hit rate of +-80% today I suppose your application is not under heavy load for 80% of the time. E.g. in average Tomcat only uses 1.5 different threads out of the pool. I have no idea what may happen if you activate asyncronous IO (AIO) request processing. Recommendation: move to ThreadLocal or another e.g. session based solution.