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
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With