File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Saving properties in shutdownhook 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 "Saving properties in shutdownhook" Watch "Saving properties in shutdownhook" New topic
Author

Saving properties in shutdownhook

Ronald Wouters
Ranch Hand

Joined: Apr 28, 2005
Posts: 190
Hi all,

do you think if a Runtime shutdownhook is a good place to save/store the suncerity.properties file ?

Regards,
Ronald Wouters



Sun Certified Developer for the Java 2 Platform
Sun Certified Enterprise Architect for the Java Platform, Enterprise Edition 5
Eiji Seki
Ranch Hand

Joined: Feb 15, 2006
Posts: 88
I think finalize methods or shutdown hooks are something always to avoid, if possible. =D You cannot be sure that they will execute, right?
What about saving it everytime configuration changes?


SCJD URLyBird (WIP)<br />SCJP 1.5
Alan Mc Kernan
Ranch Hand

Joined: Oct 13, 2005
Posts: 59
Thats was my plan of attack.

The config file is onnly going to be touched once right, on load up ?

So save it then, and continue
Aruneesh Salhotra
Ranch Hand

Joined: May 12, 2004
Posts: 167
One should have the ability of change the configs anytime. Just have a menuBar and when you have any event related to config file, save it.

Ofcouse for application finalize method, you should do clean up of things.
Ronald Wouters
Ranch Hand

Joined: Apr 28, 2005
Posts: 190
Whenever an event occurs that requires updating the configuration a planned to simple update the given property in the java.util.Properties instance.
The actual writing out the Properties instance to disk, as the suncertify.properties file, is something I planned to do in a shutdown hook.
Indeed most configuration properties for URLyBird are not changed "on the fly" and writing it out to disk whenever a property changes seems a bit overkill.
Anyway, most applications I know only "persist" new configuration changes when they exit normally. If an application crashes, and you start it up again, the old configuration is usually still active. According to the API docs for the addShutdownHook method, the thread that acts as the hook always gets started when the application exits normally. That was my reason for asking about the shutdown hook in the first place.

Alan, you made a good point about the properties being touched once. That is certainly the case for URLyBird. However, most other applications allow you to change options as often as you want, usually using an Edit Options menu item or something similar. Would you also write out the properties to disk in such a case.

Does this all make sense or am I just loosing it ? I need to get some sleep ...

Regards,
Ronald Wouters
[ February 17, 2006: Message edited by: Ronald Wouters ]
Eiji Seki
Ranch Hand

Joined: Feb 15, 2006
Posts: 88
I think it depends on the kind of configuration we are talking about.
In case of an Options windows, I see no problem in persisting it when the confirmation button is pressed. The cost of it is not relevant since we are not writing it often and it is not a big file (right? ).

Other properties that may be persisted like search history, window positions and anything else outside of the options window should be persisted when shutting down. The hook you mentioned looks like a nice way of doing it.
Aruneesh Salhotra
Ranch Hand

Joined: May 12, 2004
Posts: 167
do u think storing the search history (even though) not in specs might score points.
Ronald Wouters
Ranch Hand

Joined: Apr 28, 2005
Posts: 190
My instructions say
You will not receive extra credit points for work beyond the requirements of the specification.


So I am not planning on saving search history.
Extra work but no extra credit ... waste of time and effort.

Regards,
Ronald Wouters
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Saving properties in shutdownhook
 
Similar Threads
Killing process spawned by Runtime.exec()
runtime permission
does gc still have a chance to do its job if i kill the java process
In Data class, how to close RandomAccessFile file?
stopping a java service from a shell script