4

I can not use Executor and Future for trapping TimeOutException as it is 1.4 I need to timeout after 30 seconds if method is not complete.

//Caller class
public static void main() {

EJBMethod() // has to timeout after 30 seconds

}

//EJB method in some other class
public void EJBMethod() {

}

An approach that I am thinking is to wrap this method call in a Runnable and set some volatile boolean from run() after method is over. THen , in caller, we can sleep for 30 seconds after calling that method and once woke up, I will check the boolean in caller if it is SET. If not set, then we need to stop that thread.

1
  • 1
    Note that it is not generally possible to just "stop that thread". The threaded routine itself must provide some mechanism to react to some request to terminate. While sleep()ing this can easily be achieved but when it comes, for example, to network IO or database queries it may not be so simple. Commented Mar 6, 2013 at 13:04

1 Answer 1

2

In the simplest case, you could just go with a Thread + an arbitrary Runnable.

If you want to make the call blocking from the perspective of the caller, you can create a "service" class that runs a worker thread and uses Thread.join(long) to wait for the operation to complete or abandon it after the specified timeout (Pay special attention to the proper handling of InterruptedException so things don't get messed up).

Thread.isAlive() will tell you whether the Thread finished or not.

Retrieving the result is a separate concern; I guess you can deal with that...

[EDIT]

Quick-and-dirty example (do not use in production as is!):

/**
 * Actually needs some refactoring
 * Also, did not verify for atomicity - should be redesigned
 */
public V theServiceCall(final T param) {
    final MyResultBuffer<V> buffer = new MyResultBuffer<V>();
    Runnable task = new Runnable() {
        public void run() {
            V result = ejb.process(param);
            buffer.putResult(result);
        }
    }
    Thread t = new Thread(task);
    t.setDaemon(true);
    t.start();
    try {
        t.join(TASK_TIMEOUT_MILLIS);
    } catch (InterruptedException e) {
        // Handle it as needed (current thread is probably asked to terminate)
    }
    return (t.isAlive()) ? null : buffer.getResult();
}

NOTE: Instead of Thread.setDaemon() you can implement a shutdown flag in your Runnable as it would be a better solution.

[/EDIT]

Sign up to request clarification or add additional context in comments.

4 Comments

@Powerslve, when will this InterruptedException be thrown in theServiceCall method, is it going to happen after timeout happens ? I would consider interrupting the called thread at that time and catch InterruptedException from Runnable as well and exit.
@fortm Actually, InterruptedException can also be thrown on internal system events and alike, so it is generally not the best idea to rely on it; unless you have a 100% safe method to determine whether Thread.interrupt() was issued by your code. Also, if you went that way that'd make the caller concerned with the internal workings of the callee resulting in a tightly coupled structure.
@Powerslve, actually, interrupt()-ing a thread, possibly causing an InterruptedException, is the way to go to to tell a thread to terminate a.s.a.p.. As I remarked above, however, this is not always sufficient to actually interrupt a thread, especially if blocking IO or the like is involved, which cannot be interrupted this way. (Not considering java.nio here.)
@HannoBinder Thanks for adding to the above thoughts! BTW, my concern about interruption is that interrupt() could be invoked from outside the codebase, so the developer needs to implement some reliable means of identifying the source of that interruption. Also, as you pointed out too, if the Thread is not blocked by wait(), sleep(), join() or IO, no exception will be thrown (see link) until the next such operation if any. Anyways, using interrupt() does not eliminate the need for a shutdown flag.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.