Saturday April 11, 2009

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-app
or if you use maven
mvn grails:run-app
To 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.

April 11, 2009 by Yan Pujante

Posted in grails | 2 Comments »

Comments:

Hi, even if I found your post very informative I have to say that for me is very helpful the grails default shutdown mechanism, because it "kind of force" me/us to correctly develop my/our application(s) by not trusting in shutdown mechanisms for correct application working.

Posted by davidecr on April 13, 2009 at 08:02 PM PST #

David, I totally understand your point and I agree that the application logic should not rely on trusting the shutdown mechanism (since the power can go down, etc...). Nonetheless, I believe that my application should be 'courteous' and not, for example, abruptly end connections when it is a 'normal' shutdown. I can think of many use cases where it is beneficial: for example, when you shutdown, you inform some other services that you are not available anymore... of course the underlying implementation needs to be robust and handle the case when you abruptly disappear (with hearbeats or whatever)... but during development it would be nice if the behavior was preserved and not have to rely on other means which are harder to control (asynchronous detection...). I am totally open to both views in any case and I believe it should be an option in Grails to do clean shutdown or fast shutdown.

Posted by Yan Pujante on April 14, 2009 at 04:05 PM PST #

Post a Comment:
  • HTML Syntax: Allowed