Handling the shutdown sequence in a Griffon application - No Fluff Just Stuff

Handling the shutdown sequence in a Griffon application

Posted by: Andres Almiray on May 23, 2010

You might have heard that Griffon 0.3.1 has just been released, and with it comes a new feature: ShutdownHandlers.

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 valid
Application 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.
And there you have it, several options to run code during the shutdown of a Griffon application, each one with pros and cons.

You will find more info on these features and other at the Griffon Guide.

Keep on Groovying!
Andres Almiray

About Andres Almiray

Andres is a Java/Groovy developer and a Java Champion with more than 20 years of experience in software design and development. He has been involved in web and desktop application development since the early days of Java. Andres is a true believer in open source and has participated on popular projects like Groovy, Griffon, and DbUnit, as well as starting his own projects (Json-lib, EZMorph, GraphicsBuilder, JideBuilder). Founding member of the Griffon framework and Hackergarten community event. https://ch.linkedin.com/in/aalmiray

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »