ExecutorService 与异步注释
我有一个 EJB
列表,每个 EJB 可能运行超过 1 分钟。我想异步循环运行它们,然后在 10 秒超时后使用 Future
输出检查结果。
据我了解,方法上的 ExecutorService
和 @Asynchronous
注释都可以为我提供此功能。
什么时候应该使用Asynchronous
注释,什么时候最好使用ExecutorService
?
谢谢
I have a list of EJB
s, each one may run for more than 1 minute. I would like to run them in loop asynchronously, and then after a timeout of 10 seconds check for results using Future
output.
As I understand, both ExecutorService
and @Asynchronous
annotation on method can give me this functionality.
When should I use Asynchronous
annotations, and when it's better to use ExecutorService
?
thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从我的角度来看,我会坚持使用 EJB 功能,因此我会使用 @Asynchronous 注释,它似乎非常适合您的需求... ExecutorService 是为 Java SE 世界设计的,我不会建议您直接在 Java EE 6 服务器中使用它。
from my point I'd stick to the EJB features and as a consequence I would use the @Asynchronous annotation which seems to be very well suited for your needs... ExecutorService is made for the Java SE world and I won't suggest you to use it directly inside a Java EE 6 server.
谁在调用 EJB?如果它是 servlet,那么您可能会考虑异步 servlet 支持。
异步 EJB 比创建您自己的线程(通过 ThreadPoolExecutor)更有用,因为容器将在异步线程上建立线程上下文:上下文类加载器、java: 命名空间、安全性等。您的应用程序服务器还可能具有其他服务质量(例如,它管理的线程的监视、超时等)。
Who is calling the EJBs? If it's a servlet, then you might consider asynchronous servlet support.
Asynchronous EJBs are more useful than creating your own threads (via a ThreadPoolExecutor) because the container will establish thread contexts on the asynchronous thread: context class loader, java: namespaces, security, etc. Your application server might also have additional qualities of service (e.g., monitoring, timeouts, etc.) for threads that it manages.