aspose file tools*
The moose likes Threads and Synchronization and the fly likes How to construct a Thread that will check whether a service is up. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How to construct a Thread that will check whether a service is up." Watch "How to construct a Thread that will check whether a service is up." New topic
Author

How to construct a Thread that will check whether a service is up.

Aaron John
Ranch Hand

Joined: May 30, 2005
Posts: 74
My thread knowledge is a bit weak, so I'll try to explain this as best I can. I have a program that relies on an external service that does some document conversion. Occasionally this service falls over, which hangs the main program. What I want to do is to periodically check that the service is up and running, so I have a Thread class that will handle this. Every 30 seconds, I check whether the service is running, e.g.



I'm not sure how the main program will know if the service is not running. Do I need to use a wait() notify() mechanism? How to ensure the program recovers after the exception.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18745
    
  40

I'm not sure how the main program will know if the service is not running. Do I need to use a wait() notify() mechanism? How to ensure the program recovers after the exception.


Throwing an exception will not work -- as the exception thrown by one thread will not be caught by another thread.

You can however, setup some sort of communication between the two threads. It could be as simple as setting a flag, that is returned using a synchronized method -- which will be call right before the service is used. Or you can make it more notification driven, by using wait() and notify().

I recommend that you get a little deeper into threads -- if you want to use threads. The Sun Thread Tutorial is a good place to start.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
You can do by this. Child thread is responsible for checking the connection with the service and if the service is available then it will connect with that service.




Naseem
[ June 26, 2006: Message edited by: Naseem Khan ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Aaron John
Ranch Hand

Joined: May 30, 2005
Posts: 74
Thanks for your answer. Another question, my main program runs in an application server (JBoss). Is it safe to use threads here, as I think the app server should handle threads itself.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Then you should not create any thread yourself.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Look into an EJB compatible scheduler. I haven't used it but the Quartz home page says it's cool with EJBs. They mention Flux as a commercial alternative; my project uses a very old version of Flux and I can't say much about the newer ones. A full blown scheduler is pretty serious overkill for one repeating task but maybe it will be interesting enough that you'll find other uses for it.

Otherwise, if you have a web tier you can start your own threads in the Servlet container, just not in the EJB container. I'd likely use Timer in the JDK with a callback to some other class who can take action on the results.


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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to construct a Thread that will check whether a service is up.