aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes How to do a clean exit? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "How to do a clean exit?" Watch "How to do a clean exit?" New topic
Author

How to do a clean exit?

Jason Ma
Greenhorn

Joined: Jul 10, 2010
Posts: 5
Hi gentlemen,

I'm a newbie who has no Java experience but is lucky enough to pass the SCJP. After trying different approaches to lock the data file and collections, I'm now going for the synchronized public method. Andrew has setDatabaseLocked() method in the data class, which locks the recordNumberLock. I wounder what is the best way to do a clean exit if we don't have any lock?

Thanks for your advice.
Jason
Mxolisi Veco
Ranch Hand

Joined: Jan 14, 2010
Posts: 59
Good day.
I am not sure what it is that you are trying to ask, if you do not have any records locked, why can't you just say System.exit(0) ?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5599
    
  15

I used a shutdown hook to handle a clean exit on shutdown of the standalone client application or the network server


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2268
    
    3

Roel De Nijs wrote:I used a shutdown hook...


Me too! My shutdown hook synchronizes on the Data class object and calls a method called saveRecords() (I'm not sure this is the name of the method as I don't have the code with me right now) which writes the records from the memory cache back to the .db file.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Mxolisi Veco
Ranch Hand

Joined: Jan 14, 2010
Posts: 59
I am sorry about my earlier response Jason.

I added a shutdown hook to my data class so that I can close the database when the application exists. I did not catch the records in the database so I did not need to write any records back to the database file when the application shuts down.
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Roel De Nijs wrote:I used a shutdown hook to handle a clean exit on shutdown of the standalone client application or the network server


I did the same - ShutdownHook for standalone and server mode. Didn't define any for networked client - I have a thin client, so no locks can be acquired directly on the client side.


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
Jason Ma
Greenhorn

Joined: Jul 10, 2010
Posts: 5
Thanks all you guys for replying, and no problem Mxolisi, I haven't thought it through before asking.

My problem is this, for the data class:
- I don't have any lock on the collections, I synchronize all the public methods specified by the provided interface.
- On the other hand, the example in Andrew's great book has recordNumbersLock and does not synchronize its public methods.

Now, Andrew has the CleanExit.run() ultimately locking recordNumbersLock before exit. I wanna do a clean exit too, but my code has no such recordNumbersLock to lock. What should I do?

Reading all your comments, I think the answer is simply to have another public synchronzied method in the Data class, which closes the RandomAccessFile (provided that I didn't cache the records). The shut down hook will just invoke that method through some intermediate method of GuiController.

Am I right?

Jason
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5599
    
  15

Jason Ma wrote:Reading all your comments, I think the answer is simply to have another public synchronzied method in the Data class, which closes the RandomAccessFile (provided that I didn't cache the records). The shut down hook will just invoke that method through some intermediate method of GuiController.

Am I right?

Yes, you are
Colin Duggan
Ranch Hand

Joined: Feb 23, 2008
Posts: 41
when you say
The shut down hook will just invoke that method through some intermediate method of GuiController


How is this possible? If the shutdown hook in Data.java is triggered when the JVM is shutting down how can a method in GUIController call the shutdown hook?
Is it not enough to have the shutdown hook in the Data class constructor which calls saveRecords? This would always be called when the application is shutdown. I'm just trying to understand why we might need an explicit call in the GUIController.thanks
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5599
    
  15

Colin Duggan wrote:Is it not enough to have the shutdown hook in the Data class constructor which calls saveRecords? This would always be called when the application is shutdown.

That's what I did too and should be enough. But the saveRecords-method is a method of the public interface of the Data class (actually my custom interface I created) which is not really needed if you call this method from the shutdown hook. But adding this method to its public interface it could be called for periodic saving (e.g. each 10 minutes) all records in a later version of the application
Colin Duggan
Ranch Hand

Joined: Feb 23, 2008
Posts: 41
I get you. thanks Roel!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to do a clean exit?