File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Method call timeout and return value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Method call timeout and return value" Watch "Method call timeout and return value" New topic
Author

Method call timeout and return value

Paulo Carvalho
Ranch Hand

Joined: Nov 12, 2008
Posts: 57
Hello

I am trying to implement some kind of timeout in my java application.
I have a method which cannot be modified, because it is on an external JAR and which returns a value.
I want to call this method and, after X seconds without response from the method call, throw a TimeOutException. If the X seconds are not reached, than I want to be able of access the response of the method call.

Simple algorithm code explaining the general goal:



How can I do that?

Thanks

Regards
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

You could make use of the ExecutorService and its timeout facilities. The idea is to execute the method you want to call in a different thread, and let the ExecutorService cancel it after a specific time. Here is a simple example, using two fixed threads. You'd have to adapt this to your needs.


[My Blog]
All roads lead to JavaRanch
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Method call timeout and return value
 
Similar Threads
Pending session bean
Alert and setTimeout methods behaving differently in IE and Safari
Transaction Timeouts
JUnit and DAO
SoapConnection timeout