• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Aaron John
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Aaron John
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you should not create any thread yourself.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic