防止Tomcat中的线程阻塞

发布于 2024-10-07 09:34:45 字数 355 浏览 0 评论 0原文

我有一个 Java servlet,它充当部署在同一 Tomcat 实例上的其他 Web 服务的外观。我的包装器 servlet 创建了N 个线程,每个线程都调用一个 Web 服务、整理响应并将其发送回客户端。 Web 服务作为不同的应用程序全部部署在同一个 Tomcat 实例上。

在部署几个小时后,我发现这个外观包装器服务上出现线程阻塞,导致 Tomcat 实例崩溃。所有被阻止的线程都是此外观 Web 服务的端点(例如 http://domain/appContext/facadeService

有没有办法控制这种线程阻塞,因为实际执行处理的可用线程匮乏?防止此类僵局的最佳实践是什么?

I have a Java servlet that acts as a facade to other webservices deployed on the same Tomcat instance. My wrapper servlet creates N more threads, each which invokes a webservice, collates the response and sends it back to the client. The webservices are deployed all on the same Tomcat instance as different applications.

I am seeing thread blocking on this facade wrapper service after a few hours of deployment which brings down the Tomcat instance. All blocked threads are endpoints to this facade webservice (like http://domain/appContext/facadeService)

Is there a way to control such thread-blocking, due to starvation of available threads that actually do the processing? What are the best practices to prevent such deadlocks?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜是你 2024-10-14 09:34:45

此问题的常见解决方案是使用 Executor 框架。您需要将 Web 服务调用表示为 Callable 并将其按原样或作为 Collection传递给执行程序(请参阅 Javadoc 以获取完整的列表)选项)。

您有两种选择来控制时间。首先是使用 Executor 类的适当方法的参数,在其中指定最大 Web 服务超时。另一种选择是获取结果(表示为 Future)并使用 .get(long, TimeUnit) 指定您可以的最大时间量等待结果。

The common solution to this problem is to use the Executor framework. You need to express your web service call as Callable and pass it to the executor either as it stands, or as a Collection<Callable> (see the Javadoc for complete list of options).

You have two choices to control the time. First is to use parameters of an appropriate method of the Executor class where you specify the max web service timeout. Another option is to do get the result (which is expressed as Future<T>) and use .get(long, TimeUnit) to specify the maximum amount of time you can wait for a result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文