This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes How to stop rmi server? 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 » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "How to stop rmi server?" Watch "How to stop rmi server?" New topic
Author

How to stop rmi server?

Peter Yunguang Qiu
Ranch Hand

Joined: Nov 22, 2003
Posts: 99
How to stop rmi server programmatically?
Is it very neccessary to close the RandomAccessFile before shut down(System.exit(0) ?
Peter
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi Peter,
Originally posted by Peter Yunguang Qiu:
How to stop rmi server programmatically?

You cant. You can stop your service on the registry, but you cannot stop the registry itself.
Originally posted by Peter Yunguang Qiu:
Is it very neccessary to close the RandomAccessFile before shut down(System.exit(0) ?

You would be wise to. I think that Sun do take care to close open file descriptors cleanly when shutting down, but as far as I know it is not guaranteed. So you could risk corrupting your file if you do not close it yourself. Plus it is good for the junior programmer reviewing your code: they get to see where you open the file and where you close it, rather than it being automagically closed.
Regards, Andrew
[ December 30, 2003: Message edited by: Andrew Monkhouse ]

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

Joined: Nov 07, 2003
Posts: 4982
Hi.

Is it very neccessary to close the RandomAccessFile before shut down(System.exit(0)?

I guess it should be no harm for doing this.
In addition, you may override the protected function finalize() in order to release any resources held by the system.
But, whether the resource is released is NOT assured.
Nick.


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Peter Yunguang Qiu
Ranch Hand

Joined: Nov 22, 2003
Posts: 99
Thanks Andrew and Nicholas.
You cant. You can stop your service on the registry, but you cannot stop the registry itself.
Right now I shut down the server by press Ctrl - C. Is it the same as
System.exit(0)? How to shut down programmatically? How to stop RMI service? By using Naming.unbind(String name)? Is rmitegistry always running on the machine and we cannot stop it? should I stop server by a server GUI?
Peter
George Marinkovich
Ranch Hand

Joined: Apr 15, 2003
Posts: 619
Hi Peter,
Originally posted by Peter Yunguang Qiu:

1. Right now I shut down the server by press Ctrl - C. Is it the same as
System.exit(0)?
2. How to shut down programmatically?
3. How to stop RMI service? By using Naming.unbind(String name)?
4. Is rmitegistry always running on the machine and we cannot stop it? 5, should I stop server by a server GUI?

1. Yes. Do you allow the user to select the database file on the server? Or allow the user to pick the rmiregistry port? If so, then you probably have some sort of GUI for the server. You could therefore implement a shutdown button for the server. Or maybe just closing the server GUI causes the server to shutdown.
How do you make sure the database file is closed properly?
2. System.exit(0); will shutdown your server programmatically. You should do cleanup (if there is any) prior to that, of course.
3. I'm not sure it's necessary to stop the RMI service (by using Naming.unbind). If you shutdown your server, clients will get an exception (don't remember exactly which one) when they try to make a call on the server. The client needs to be able to handle this exception. If you did an explicit Naming.unbind() on the server-side, I guess the client would get a different exception which you would then still need to handle on the client-side. I don't see much advantage in doing the unbind in that case.
4. rmiregistry may or may not be running on the server. That's why when you start the server you do a LocateRegistry.createRegistry (assume the rmiregistry has not been started), and if that throws an exception then do a LocateRegistry.getRegistry (since we know the rmiregistry must have been started already). Of course, you can make the opposite assumption just as easily: assume the rmiregistry is already started and create it if that's not true. I'm not sure it matters which way you do this.
In any case, I don't know how you would stop the rmiregistry even if you wanted to. The point being that your code should work under either circumstance: the rmiregistry is already running, or it's not.
5. Same as answer to 1.
Hope that helps and Happy New Year,
George
[ December 31, 2003: Message edited by: George Marinkovich ]

Regards, George
SCJP, SCJD, SCWCD, SCBCD
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi George,

Originally posted by Peter Yunguang Qiu:
Right now I shut down the server by press Ctrl - C
Originally posted by George Marinkovich:
How do you make sure the database file is closed properly?

You could use Runtime.addShutdownHook to add code which runs when the user shuts down by pressing Ctrl-C. This can ensure that there are no current writes to the database and close any open files.
Regards, Andrew
Peter Yunguang Qiu
Ranch Hand

Joined: Nov 22, 2003
Posts: 99
Hi, George:
Happy New Year to you!
Thank you for your detail explanation.
Re: Answer 1:
I have not implemented the server GUI, I will do that. I am going to reuse the config window from client GUI and let server and client share one suncertify.properties file. Is that OK? To close database file, I just do this:
randomAccessFileObject.close();
Is it close database file properly?
Re: Answer 2:
As for cleanup, I just do this:
randomAccessFileObject.close();
Is it enough?
Re: Answer 3:
I will hadle that exception. Thank you for your prompt.
Re: Answer 4:
I have a question:
The method
public static Registry getRegistry(String host, int port)
throws RemoteException
Can get registry from other machine which is not where the server running?
I mean, the server and registry can reside in 2 machines? and the client at the third machine?
Peter
-------------------------------
Hi, Andrew:
Happy New year to you!
Thanks for you help. I just read Java API doc about "Runtime.addShutdownHook " It is the first time I read this method. I do not quite understand it and don't know how to use it and what is the benifits to use it and in what situation to use it.
Peter
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi Peter,
Happy New Year to you too.

The method
public static Registry getRegistry(String host, int port)
throws RemoteException
Can get registry from other machine which is not where the server running?

You can - when you call this method, all the JVM knows is that you are requesting a reference to the Registry. It does not know whether you want it in order to lookup a service or bind a service. So of course you can get a reference to a registry on another machine.

I mean, the server and registry can reside in 2 machines? and the client at the third machine?

Here is the kicker. Both the API and the RMI Specifications allow you to set up such an implementation. However the reference implementation of the RMI Registry (the one that we get in Sun's JDK / Java runtime) does not allow this for security reasons. Potentially an RMI Registry from another supplier may allow this.
End result: you do not have to try and code for or test the scenario where the RMI Registry is on a separate machine to the service you are running.

Thanks for you help. I just read Java API doc about "Runtime.addShutdownHook " It is the first time I read this method. I do not quite understand it and don't know how to use it and what is the benifits to use it and in what situation to use it.

This may be irrelevant now that you are using a GUI to start and stop your server.
However you would use this hook anytime you want to run cleanup routines when the user hits Ctrl-C. So instead of closing the file inside the event handler when the user hits the shutdown button, you could have the event handler just call System.exit(). Then have the code to close the file inside your cleanup code which you registered with the ShutdownHook.
How to use it is fairly simple. Try this code:

See how simple that was?
Now some people would be thinking that this is pointless: you have a GUI with a shutdown button. But the problem with that is that you need to have someone physically at the terminal to hit the shutdown button.
By using the shutdown hook, you can have the server application respond to an external signal telling it to shutdown. For example, most uninterruptable power supplies (UPS) will send a signal to the operating system if the main power is off and the battery is getting low. The operating system normally sends a friendly shutdown message (usually the same signal as if the user had pressed Ctrl-C) to each application to allow them to shut down cleanly, then after a few minutes the operating system kills any application which did not respond to the friendly request to shutdown. If you have a shutdown hook your application can handle the potential power outage. If you don't have a shutdown hook your application will crash (potentially in the middle of an update).
Note that to use this effectively you would also need some way of ensuring that current writes are completed and that new writes do not start. The Fly By Night Services assignment covered this by having a method which would lock the entire database, however the new assignments do not require such a locking facility.
Also big note: this is getting a bit beyond the requirements of the assignments you have been given. Shutting down cleanly is something you really should do, and I offered this as a suggestion if you did have your users hitting Ctrl-C to exit the server. However if you have a GUI with an exit button, then you can save yourself a lot of work
Regards, Andrew
[ January 01, 2004: Message edited by: Andrew Monkhouse ]
Peter Yunguang Qiu
Ranch Hand

Joined: Nov 22, 2003
Posts: 99
Thank you very much Andrew.
Peter Yunguang Qiu
Ranch Hand

Joined: Nov 22, 2003
Posts: 99
If I don't use server GUI, are there any good way to shut down server? I think I should use BufferedReader for waiting a key strike and then system.exit(0) after close the file. Is that right?
Peter
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to stop rmi server?
 
Similar Threads
about the stop rmi
JBoss -5.0.0 installation fails
B&S Passed - 381
Strange RMI - Error
Server UI