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 Timeout functionality needed for process Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Timeout functionality needed for process" Watch "Timeout functionality needed for process" New topic
Author

Timeout functionality needed for process

James Daniel
Ranch Hand

Joined: Sep 24, 2004
Posts: 79
I have written an application that executes an Ant script. Unfortunately, sometimes the Ant scripts exits unexpectedly if something is incorrect in the Ant script. My program doesn't pick this exit up and hangs forever. I was thinking of putting a inactivity timeout value for the process or InputStream. Does anyone know how to implement this or any ideas to handle this situation? Any suggestions would be greatly appreciated. Thanks.
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
My best advice would be that you should get rid of those "incorrectness" that suddenly happen in your Ant scripts and that make your application hang.

That would be curing the disease, not simply soothing the pain.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I agree it would be better to cure the disease than put a bandaid on it (mixed metaphor warning!) but if you cannot ...

That will cause your main thread to throw an exception if the real work doesn't finish soon. It won't kill the real work thread. You can try to do that separately or just let it hang for a while.
[ May 31, 2006: Message edited by: Stan James ]

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
James Daniel
Ranch Hand

Joined: Sep 24, 2004
Posts: 79
Thanks for the input. I absolutely agree that the Ant code needs to be working but I am trying to build in safeguards in case there is a problem with the Ant script that it doesn't make the thread hang indefinitely. Does the join method detect the underlaying process status? The thread continues to run and does not detect the process exit because the process does not issue an exit value. I was thinking that maybe I can monitor the InputStream. If it stops for a given amount of time, then it would destoy the thread. Is there a good way to monitor the InputStream to see if it stops writing?
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Well, the problem with the simple InputStream is that it won't interrupt if the thread gets blocked by a reading or writing operation, even if you explicitly interrupt the hanging thread by calling its interrupt() method.

Then, instead of using a simple InputStream, maybe, you should use a FileChannel:

See java.nio.channels.FileChannel

Since FileChannels implement InterruptibleChannel, your thread may be interrupted even while it is blocked by an IO operation like a read or a write.

Then, as Stan suggested, you could ask your main thread to join your "IO thread" for a period of time that you consider reasonable, if when the join operation ends, the thread doing the IO operation is not yet terminated,that's to say if...

!theThread.getState()==Thread.State.TEMINATED

...then you can ask the hanging thread to interrupt:

theThread.inturrpt()

Be sure to handle ClosedByInterruptException in the run() method of your "IO thread" so that you can do some cleanup when the IO operation is interrupted.

Does this sound useful?
[ May 31, 2006: Message edited by: Edwin Dalorzo ]
James Daniel
Ranch Hand

Joined: Sep 24, 2004
Posts: 79
It sounds very useful. Thanks so much replying. I will give it a try. Thanks..
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
I mean somewhat like this:



Now, if you use join(timeout) it may imply that the main thread would have to wait some time even after the IO thread has finished. To improve that you may put your main thread to sleep for a smaller period and check the IO threads state, if after a number of checks the IO thread has not succeed it, interrupt it.


[ May 31, 2006: Message edited by: Edwin Dalorzo ]
James Daniel
Ranch Hand

Joined: Sep 24, 2004
Posts: 79
Edwin, Let me thank you again for taking the time to reply. I see that this method works under the premis that one knows how long an Ant build, or any spawned process, is to take. If it takes longer than a given time, it "times out" and interupts the thread. I could probably do this since I know how long a typical build is to take. I was initally thinking of timing inactivity in the IO. Say there is a minute or two of total inactivity then it would call the interupt. That way it is universal no matter how long the process would normally last. I don't know if that is possible and I may be wanting too much.
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Set MAX_SECONDS to the maximum time you would like to wait for every IO operation, and before every read or write operation reset the current counter to 0.

That way the main thread will interrupt the IO thread only if a single IO operation exceeds the timeout, and not if the whole file processing exceeds the timeout.

I have to admit that it sounds like something I would do just to patch the code, and I wouldn't feel very proud of it. But the whole question, all along, has been about patching a piece of code, hasn't it?

What do you think?
[ May 31, 2006: Message edited by: Edwin Dalorzo ]
James Daniel
Ranch Hand

Joined: Sep 24, 2004
Posts: 79
That sounds like it would work. I will do it as you say as a patch but will try to think of a more eloquent solution down the road. Thanks again for the input..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Timeout functionality needed for process
 
Similar Threads
Running WebLogic sh script that sets environment variables from ANT
Help Needed regarding the automation of the build process
Error code _from_ ANT
Error code _from_ ANT
What command to give in ant to pause/wait for some seconds?