This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to time out calling a function from an application  RSS feed

 
Mullin Yu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mullin Yu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!