This week's book giveaway is in the HTML/CSS/JavaScript forum.
We're giving away four copies of Practical SVG and have Chris Coyier on-line!
See this thread for details.
Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to Monitor a window Service and Restart Programatically

 
chetan raj
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I have a reqirement to monitor a window service through a java program
and restart the service programaticaly if the service stops. Is this possible through a java program. A sample example on this will be really helpful.
Thanks in Advance,

Chetan
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't believe this question has anything to do with threads -- so... off it goes to "other api".

Henry
 
Ajay Saxena
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your requirement is just to restart a windows service in the event it stops /fails,why not use Windows Service Control Manager (SCM)to achieve the same. That's a default service monitor built into Windows.

I don't see any reason for you to programmatically monitor a windows service UNLESS you need to do some sort of detailed monitoring such as fetching performance and usage stats for the corresponding process(es) of the service.

If you still want to do programmatic monitoring,you have a whole bunch of options

1> you may want to use WMI (Windows Management Instrumentation),for any arbitrary service.I'm not sure if a Java wrapper on WMI exists.So you might have to use JNI in that case.

2> Otherwise,if the service is actually a java program,you would have to spawn a monitor agent thread in the program that would listen to heartbeats from your Monitor program (the watchdog that you plan to design).The monitor program and the agent thread in the monitored program can communicate over RMI. The restart part could be achieved through Runtime.exec()

3> Alternatively if the service spawns an HTTP listener thread, your monitor Program can issue HTTP heartbeat requests to detect if the service is running. However, be cautioned that the HTTP listener could get stopped even when the service is up and running. So you might have to investigate further.

To conclude, the approach to be taken would be influenced by the level of monitoring you plan to do,and of course the semantics of service failure

[ August 28, 2008: Message edited by: Ajay Saxena ]

[ August 28, 2008: Message edited by: Ajay Saxena ]
[ August 29, 2008: Message edited by: Ajay Saxena ]
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also use the "sc" command. Find out the exact command ("sc /?" will help you), then run that using ProcessBuilder or Runtime.exec.

JNI has my preference though, since a) you don't have to parse the output, and b) it will most likely be faster.
 
chetan raj
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI All,
Thanks for the prompt reply. I need to run my java program
as window service and write another java program to monitor
this service and restart the service in case of service failure.
So anybody give a suitable link or sample program to achive this
will be greatly helpful.

thanks in advance,

Chetan
 
Ajay Saxena
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since both the programs are implemented in Java,consider using RMI. Let's call the monitor program M and the monitored program A.

Implement a monitor agent component(a runnable that could be spawned as a daemon thread)on A as an RMI service with a remote method heartbeat().

Implement M as an RMI client that would periodically invoke this remote method heartbeat() on A.The method could be made to return some string or boolean.If the method throws some exception, you may want to decide that A has failed. But then deciding on the failure of A just on the basis of an exception on the remote call heartbeat() might not be right. You may want to come up with more sophisticated methods of failure detection.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!