Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make EJB call with timeout

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

like image 298
lili Avatar asked Jan 18 '12 14:01

lili


2 Answers

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.

like image 101
Olof Åkesson Avatar answered Nov 09 '22 04:11

Olof Åkesson


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.

like image 22
Brett Kail Avatar answered Nov 09 '22 05:11

Brett Kail