This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes NX: (Contractors) server shutdown and file close Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "NX: (Contractors) server shutdown and file close" Watch "NX: (Contractors) server shutdown and file close" New topic

NX: (Contractors) server shutdown and file close

Jeff Wisard
Ranch Hand

Joined: Jan 07, 2002
Posts: 89
Hello everyone,
I have not been able to figure out how to gracefully shut the RMI-based server down. I want to do this so that I can close my database file properly when exiting the server.
What is a good way to shut down an RMI server so that resources can be released? What are the issues here?

Jeff Wisard<br />Sun Certified Java Programmer (Java 2)<br />Sun Certified Web Component Developer
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11778

Hi Jeff,
I have not seen any way of shutting down the RMI Server programatically.
I think the best you can do is derigister your own services, and check that no other services have been registered. They do a System.exit().
Regards, Andrew

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Matt Rea

Joined: Apr 22, 2003
Posts: 28
Hey guys,
I have always been a fan of shutdown hooks to gracefully handle shutdowns in these types of cases. From memory it is something along the lines of:
Runtime localVM = Runtime.getRuntime(); localVM.addShutdownHook(cleanupThread );
cleanupThread is a inner class extending thread. All your cleanup and graceful shutdown code is in the run method. Even if you control-C to shutdown your server it will still run the shutdown hook.
Vitaly Zhuravlyov

Joined: Apr 11, 2003
Posts: 16
I did not think that the Shutdown Hook is not well-known. You can use something like this.

This feature is available since JDK 1.3
Jeff Wisard
Ranch Hand

Joined: Jan 07, 2002
Posts: 89
Wow! Very cool! I didn't know about this.
I agree. Here's the link:
subject: NX: (Contractors) server shutdown and file close
It's not a secret anymore!