grails.org Website

Proper shutdown in dev mode

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Critical Critical
  • Resolution: Fixed
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None
  • Patch Submitted:
    Yes

Description

I posted a message about this 'issue' in the mailing list (http://www.nabble.com/Proper-shutdown-in-dev-mode-td22877346.html) but in the meantime I wanted to track the improvement down.

I was looking for a way to do proper shutdown in development mode (meaning that the spring application context gets properly closed when hitting CTRL-C). I don't know if there is a standard way to do this, but it was pretty important in my case that even in dev mode, shutdown happens correctly. Since I could not find a way, I implemented it in the following manner. Please let me know if there is a better way (would be great to be 'standard'...)

I created a simple class in grails-app/utils/com/mypackage/ShutdownHook.groovy

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...")
  }
}

and in grails-app/conf/spring/resources.groovy I have the following (conditional) block:

if(grails.util.GrailsUtil.isDevelopmentEnv())
  {
    myShutdownHook(com.mypackage.ShutdownHook)
  }

I demonstrated that it works really well. It would be really sweet if that could be part of the normal Grails distribution (and it would even be ok if by default it is off...) but there should be a 'standard' way to be able to have shutdown on CTRL-C in dev mode behaves properly.

Activity

Hide
Graeme Rocher added a comment -

patch included, moving up the priority list

Show
Graeme Rocher added a comment - patch included, moving up the priority list
Hide
Graeme Rocher added a comment -

Yup works great. Thanks

Show
Graeme Rocher added a comment - Yup works great. Thanks

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: