aspose file tools
The moose likes Beginning Java and the fly likes How to time out calling a function from an application Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "How to time out calling a function from an application" Watch "How to time out calling a function from an application" New topic
Author

How to time out calling a function from an application

Mullin Yu
Greenhorn

Joined: Feb 24, 2005
Posts: 14
inside my program, it will call a function of 3rd party java class, but sometimes it may hang and no error/exception thrown. then, my application doesn't work properly.

i want to know how to make the following thrown an exception after a period of time to my main program, some timeout exception? then, my application can continue processing.

try {
objLog.info("before convert");
jpegConverted = objConvertService.convert(_objAttachment.getData(),sExtension,"JPEG",((docconv.service.ImageProfile) (objObject)));
objLog.info("after convert");
} catch(IllegalFileTypeException _objIlegalfiletypeexception) {
objLog.err(sContentName + ": Illegal File Type Exception returned by transcoder.",_objIlegalfiletypeexception);
convertSuccess = false;
} catch(ConvertException _objConvertexception) {
objLog.err(sContentName + ": Convert Exception returned by transcoder.",_objConvertexception);
convertSuccess = false;
} catch(Exception _objException) {
objLog.err(sContentName + ": Others Exception returned by transcoder.",_objException);
convertSuccess = false;
}
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
want to know how to make the following thrown an exception after a period of time to my main program, some timeout exception?


Use Threads for this.
Sample code is here .
But i feel this is not the right way of exception handling.
[ April 06, 2005: Message edited by: Srinivasa Raghavan ]

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You can put the work to be done in another thread and join() that thread with a timeout.

Look at the doc for Thread.join() to see what might happen. And let me know if this really works for you.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Mullin Yu
Greenhorn

Joined: Feb 24, 2005
Posts: 14
i got compilation error:
[javac] C:\opt\SVGT\Development\src\java\hk\com\smartone\svgtx\converter\Con
verter.java:104: local variable jpegConverted is accessed from within inner clas
s; needs to be declared final
[javac] jpegConverted = objConvertService.convert(_objAttachment.getData(),sExtension,"JPEG",((docconv.service.ImageProfile) (objObj
ect)));

as jpegConverted, _objAttachment, sExtension, objObject are outer class variables and some functions of outer class may need to get and set their values and can't assign those variables to final.

any methods to solve this?

try {
/* original code
objLog.info("before convert");
jpegConverted = objConvertService.convert(_objAttachment.getData(),sExtension,"JPEG",((docconv.service.ImageProfile) (objObject)));
objLog.info("after convert");
*/
//
Thread th = new Thread()
{
public void run()
{
objLog.info("before convert");
jpegConverted = objConvertService.convert(_objAttachment.getData(),sExtension,"JPEG",((docconv.service.ImageProfile) (objObject)));
objLog.info("after convert");
}
};
th.start();
th.join(100 * 1000);
if(th.isAlive())
{
th.interrupt();
throw new Exception("Timeout");
}
//
} catch(Exception _objException) {
objLog.err(sContentName + ": Others Exception returned by transcoder.",_objException);
convertSuccess = false;
}
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I'd probably make the converter service a regular class - not a nested class - with a constructor or setters to load up the things it needs to work with. That's my lazy way to avoid these scoping problems without really figuring them out.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
If you read the error message, it tells you one way to fix the problem:

"local variable jpegConverted is accessed from within inner clas
s; needs to be declared final"

Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How to time out calling a function from an application
 
Similar Threads
Runtime exception
Need Java program to convert SVG to PDF format
Why finally block is not executed completely?
data structures
Exam Objectives Flow Control.