Grails - Proper shutdown in dev mode
For my main project I am using Grails for the front-end (I will relate in an upcoming post why I chose this technology in the first place). Grails has this very interesting development mode which allows you to continue working on your application and see the changes right away. To start the application you usually issue the command:
grails run-appor if you use maven
mvn grails:run-appTo shutdown (for restarting for example), you do a
CTRL-C
which terminates the process. Grails uses the Spring framework to bootstrap your application. It also allows you to define your own beans. However, I noticed that when terminating the application, the beans that I had registered with a destroy-method
were not being properly shutdown (the destroy method is simply not called). I tried to find a way to change this behavior by default but did not find anything. I then implemented my own shutdown solution in this manner:
I created a simple class in grails-app/utils/com/mypackage/ShutdownHook.groovy
which registers a VM-wide shutdown hook when it gets called by Spring (ApplicationContextAware
)
package com.mypackage import org.springframework.context.ApplicationContextAware import org.springframework.context.ApplicationContext import org.apache.commons.logging.Log import org.apache.commons.logging.LogFactory public class ShutdownHook implements ApplicationContextAware { public static final Log log = LogFactory.getLog(ShutdownHook.class) public void setApplicationContext(ApplicationContext applicationContext) { Runtime.runtime.addShutdownHook { log.info("Application context shutting down...") applicationContext.close() log.info("Application context shutdown.") } log.info("Shutdown hook setup...") } }Then I added the following block in
grails-app/conf/spring/resources.groovy
which conditionally creates the bean only in development mode (thanks to groovy Spring DSL!).
if(grails.util.GrailsUtil.isDevelopmentEnv()) { myShutdownHook(com.mypackage.ShutdownHook) }It works really well as my beans get properly destroyed when the application terminates. Nonetheless it would be better if it was part of the Grails framework by default. I opened a Jira (GRAILS-4404) ticket for it.
Apr 11 2009 - Posted by Yan in