管理容器中线程池的最佳实践是什么?
我需要能够在容器中实现两个线程任务,并且需要了解执行此操作的最佳实践。以下是我需要完成的两类任务:
- 在 Web 服务调用期间,我需要启动一个线程,该线程在发送响应后继续处理。处理完成后,不需要将消息返回给原始发件人。
- Web 服务调用可能需要生成多个需要彼此并行运行的线程。应阻止对原始请求的响应,直到所有工作人员完成为止。响应的元素将从每个线程的结果片段中提取。
当然,我可以创建自己的 java.util.concurrent.Executor 实例并使用它,但我怀疑容器可能足够智能,可以提供它们管理的实例。
FWIW - 我正在 JDK 1.5.0 上使用 WebSphere 6.1(我知道,古老......但它就是这样)。我正在运行使用 Apache CXF 开发的 Web 服务,因此我位于 servlet 容器中,但使用 Spring 配置。
I need to be able to achieve two threading tasks in a container and need to understand the best practices for doing this. Here are the two classes of task I need to accomplish:
- During a web services call, I need to start a thread that continues processing after the response has been sent. No message is required back to the original sender when processing is complete.
- A web services call may need to spawn multiple threads that need to run in parallel to each other. The response to the original request should be blocked until all the workers have completed. Elements of the response will be drawn from pieces of each of the thread's results.
Of course, I could create my own instance of a java.util.concurrent.Executor
and use it, but I suspect containers might be smart enough to provide one that they manage.
FWIW - I'm using WebSphere 6.1 on JDK 1.5.0 (I know, ancient...but it is what it is). I am running web services developed using Apache CXF, so I'm in the servlet container, but configured with Spring.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于 1),您可能想查看异步 Bean。或者使用消息驱动 Bean,它拾取并操作您发送到队列的消息。您可能还想看看来自 Spring 的 Quartz 内容。我认为使用 Servlet 3(在 WAS 6.1 上没有机会!),您可能会在没有异步工作管理器或 JMS 方法的情况下获得异步支持,但在那之前我不知道比这些模式更好的方法。
对于2),通常阻止请求是一件有风险的事情(如果超时怎么办)。但是,您位于 servlet 容器中,因此您可以使用 java.util.concurrent 中的某些内容,例如您提到的 ExecutorService。或者使用消息传递将工作发送到其他地方并阻止直到它完成。
For 1) you might want to look at Asynchronous Beans. Alternatively use a Message Driven Bean which picks up and actions a message you send to a Queue. There's the Quartz stuff from Spring you might want to look at too. I think with Servlet 3 (no chance on WAS 6.1!) you might get the async support without the Async Work Manager or JMS approach, but until then I don't know a better way than these patterns.
For 2) generally blocking the request is a risky business (what if you hit the timeout). However, you're in the servlet container so you're ok using something from java.util.concurrent e.g. ExecutorService as you mentioned. Alternatively use messaging to send the work off somewhere else and block until it completes.
一般来说,我不会从容器内部启动线程,因为 j2ee 完全合规性打开时您的应用程序可能会死掉。在完全合规的情况下,不允许创建线程。您想要做的是设置一个 JMS 队列,将“待完成的工作”提交到其中。然后,您可以让
MDB
侦听队列,该队列执行您的线程本应执行的实际操作。Generally, I wouldn't start threads from inside a container because there is the chance that j2ee full compliance is turned on and your app would die. Under full compliance threads are not allowed to be created. What you want to do is set up a JMS queue that you submit your "work to be done" to. You can then have an
MDB
listening to the queue which performs that actual operation that your thread would have done.