Grails Config: Bootstrap.groovy

@drse / updated July 06, 2015

Share:  

Note: as of Grails 2.4.x Confirmed with grails 3.1.0.RC1

Checking the environment

Bootstrap.groovy:

import grails.util.Environment

class BootStrap {
  def init = { servletContext ->
    println "Initializing..."
    if (Environment.current == Environment.DEVELOPMENT) {
      println "Development"
    } else if (Environment.current == Environment.TEST) {
      println "Test"
    } else if (Environment.current == Environment.PRODUCTION) {
      println "Production"
    }
  }

  /**
   * Note: this is not guaranteed to be called by the servlet container
   */
  def destroy = {
    printn "De-initializing..."
    if (Environment.current == Environment.DEVELOPMENT) {
    } else if (Environment.current == Environment.TEST) {
    } else if (Environment.current == Environment.PRODUCTION) {
    }
  }
}

Using a Service

(or accessing other Spring beans)

Bootstrap.groovy:

import org.springframework.web.context.support.WebApplicationContextUtils

class BootStrap {
  def init = { servletContext ->
    def springContext = WebApplicationContextUtils.getWebApplicationContext(servletContext)
    println "Initializing..."
    def service = springContext.getBean("notificationService")
    service.sendAdminNotification("[UP] started")
  }

  /**
   * Note: this is not guaranteed to be called by the servlet container
   */
  def destroy = { servletContext ->
    printn "De-initializing..."
    def springContext = WebApplicationContextUtils.getWebApplicationContext(servletContext)
    if (Environment.current == Environment.DEVELOPMENT) {
    } else if (Environment.current == Environment.TEST) {
    } else if (Environment.current == Environment.PRODUCTION) {
      def service = springContext.getBean("notificationService")
      service.sendAdminNotification("[DOWN] destroyed")
    }
  }
}

Checking database connections

  • DataSource
  • MongoDB
  • spring-cloud connector

Bootstrap.groovy:

import org.springframework.web.context.support.WebApplicationContextUtils
import org.springframework.cloud.CloudFactory
import org.springframework.cloud.CloudException

class BootStrap {
  def grailsApplication

  def init = { servletContext ->
    def springContext = WebApplicationContextUtils.getWebApplicationContext(servletContext)
    println "Initializing..."
    println "Datasource: " + grailsApplication.config.dataSource
    println "Mongo: " + grailsApplication.config.grails.mongo
    // Get bean ref
    def mongo = springContext.getBean("mongo")
    // Get cloud info
    def cloud = null
    def dbName = "mydb"
    try {
      cloud = new CloudFactory().cloud
      // Get database name for bound service
      def mongoInfo = cloud?.getServiceInfo('myservice-db')
      dbName  = mongoInfo?.database
      println "Got dbName from cloud info: " + dbName
    } catch (CloudException e) {
      println "ERROR getting cloud: " + e
    }
    def db = mongo.getDB(dbName)
    println "Got db: " + db
  }
}