Handling the shutdown sequence in a Griffon application
I know what you're thinking, executing code at application shutdown is nothing new, as a matter of fact there were 3 other ways to do it before this feature was added to Griffon. Let's review all of them in complexity order.
Shutdown life cycle script
This is the first option that was added to Griffon back in release 0.0 (September 10th 2008 for those keeping count). This script will be called as part of the application's life cycle (inspired by JSR 296 Swing Application Framework), and it's always guaranteed to be executed inside the UI Thread. This script is a single point of entry (good) but it does not allow other artifacts to execute code at shutdown (bad) unless you call them explicitly on that script.Application event handlers
Then came application events at a later release (0.1 to be precise); they revolutionized how artifacts interact with each other. An application will send a ShutdownStart event when the shutdown sequence is initiated. You have the option to register an application event handler at any time; as a matter of fact almost of any type too (supported types are: Script, Map, Closure and beans). Controllers happen to be application event listeners by default, meaning that the following code is validApplication event handlers are guaranteed to be called outside of the UI thread always.
GriffonApplication subclass
I almost decided to not write about this option as it is not for the faint of heart. Actually it is not that bad as it sounds, it's just that creating a GriffonApplication subclass is rarely needed. But if you do, then you have the option to override the shutdown() method.All these options allow you to execute code while the shutdown sequence is in process, however none of them let you abort the sequence (unless you go with the subclass option, but why complicate yourself?).
Enter the ShutdownHandler interface.
ShutdownHandler
This interface defines two handy methods- boolean canShutdown(GriffonApplication app) - informs the application that the shutdown sequence must be aborted if the return value is false.
- void onShutdown(GriffonApplication app) - executes the code as part of the shutdown sequence.
You will find more info on these features and other at the Griffon Guide.
Keep on Groovying!
About Andres Almiray
Andres is a Java/Groovy developer and Java Champion, with more than 11 years of experience in software design and development. He has been involved in web and desktop application developments since the early days of Java. He has also been teacher of computer science courses in the most prestigious education institute in Mexico. His current interests include Groovy and Swing. He is a true believer of open source and has participated in popular projects like Groovy, Griffon, JMatter and DbUnit, as well as starting his own projects (Json-lib, EZMorph, GraphicsBuilder, JideBuilder). Founding member and current project lead of the Griffon framework. He blogs periodically at http://jroller.com/aalmiray. You can find him on twitter too as @aalmiray. He likes to spend time with his beloved wife, Ixchel, when not hacking around.
More About Andres »NFJS, the Magazine
December Issue Now AvailableBDD and REST
by Brian SlettenMocks and Stubs in Groovy Tests
by Kenneth KousenAlgorithms for Better Text Search Results
by John GriffinKnowns and Unknowns of Scrum and Agile
by Brian Tarbox