Make EJB call with timeout

三世轮回 提交于 2019-12-10 14:55:32

问题


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


回答1:


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.




回答2:


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.




回答3:


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 the Future.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 using Future.cancel and SessionContext.wasCancelCalled.



来源:https://stackoverflow.com/questions/8912166/make-ejb-call-with-timeout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!