进行带有超时的 EJB 调用
我有一个调用 EJB B
的 EJB A
。 UI 等待响应的时间不应超过 30 秒。如果某些数据丢失,它应该返回部分响应。
如何在 EJB B
上定义超时(30 秒的时间限制)?
我可以将 EJB B
定义为返回 Future
的 Asynchronous
,然后执行 Future.get(30, TimeUnit.SECONDS). 但这是最好的解决方案吗?
谢谢
PS 我用 glassfish 3.1
I have an EJB A
that invokes EJB B
. The UI should not wait for more than 30 seconds for a response. If some data is missing, it should return a partial response.
How can I define a timeout (time limit of 30 seconds) on EJB B
?
I can define EJB B
as Asynchronous
that returns Future
, and then do Future.get(30, TimeUnit.SECONDS)
.
But is it the best solution?
thank you
P.S. I use glassfish 3.1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议为此使用事务超时。
我认为没有标准的配置方法,因此它取决于应用程序服务器。我假设您想针对每个类或方法专门设置它。
对于WebLogic,您可以在“transaction-descriptor”中的“weblogic-ejb-jar.xml”中指定它,或者使用注释“@TransactionTimeoutSeconds”。
http://docs.oracle.com/cd/E12839_01/web.1111/e13719/ejb_jar_ref.htm#i1506703
http://docs.oracle.com/cd/E21764_01/web .1111/e13720/annotations.htm#i1438354
对于JBoss AS 您可以使用注释“@TransactionTimeout”或在“jboss.xml”中设置事务超时。
https://community.jboss.org/wiki/TransactionTimeout
我确信有类似的配置选项在每个应用程序服务器中。
I would suggest using the transaction timeout for this.
I don't think there is a standard way of configuring it so it would depend on the application server. I assume you want to set it specifically per class or method.
For WebLogic you can specify it in "weblogic-ejb-jar.xml" in the "transaction-descriptor" or use the annotation "@TransactionTimeoutSeconds".
http://docs.oracle.com/cd/E12839_01/web.1111/e13719/ejb_jar_ref.htm#i1506703
http://docs.oracle.com/cd/E21764_01/web.1111/e13720/annotations.htm#i1438354
For JBoss AS you could set the transaction timeout using the annotation "@TransactionTimeout" or in "jboss.xml".
https://community.jboss.org/wiki/TransactionTimeout
I am sure there are similar configuration options in every application server.
无法中断目标 EJB。唯一真正的选择是目标 EJB 协作并定期检查它是否超出了预期的目标响应时间。
即使您使用@Asynchronous并且Future.get超时,您也只是解除了客户端等待结果的阻塞;目标EJB将继续执行并消耗资源。但是,使用异步方法,您确实可以使用
Future.cancel
和SessionContext.wasCancelCalled
进行一些内置协作取消。There is no way to interrupt a target EJB. The only real option is for the target EJB to cooperatively and periodically check whether it has exceeded the intended target response time.
Even if you use
@Asynchronous
and theFuture.get
times out, you've simply unblocked the client from waiting for the result; the target EJB will continue to execute and consume resources. However, with asynchronous methods, you do have the benefit of some builtin cooperative cancellation usingFuture.cancel
andSessionContext.wasCancelCalled
.要为 bean 配置适用于其所有方法的超时,您必须在 glassfish-ejb-jar.xml 中配置属性
cmt-timeout-in-seconds
。该超时值由启动新事务的 bean 的所有方法使用,当它们加入其他正在进行的事务时不适用。
还可以参考此链接了解有关超时的更多详细信息。
To configure timeout for a bean which applies to all its methods, you have to configure the attribute
cmt-timeout-in-seconds
in glassfish-ejb-jar.xml.This timeout value is used by all the methods of the bean that initiates a new transaction, not applicable when they join other ongoing transaction.
Also can refer this link for further details on timeout.