• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to do a clean exit?

 
Jason Ma
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9934
113
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used a shutdown hook to handle a clean exit on shutdown of the standalone client application or the network server
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mxolisi Veco
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 611
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jason Ma
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9934
113
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9934
113
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get you. thanks Roel!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic