Loading...

December 24, 2015

Ratpacked: Execute Code On Start and Stop Application Lifecycle Events

Ratpack has the ratpack.server.Service interface with the methods onStart and onStop. If we write an implementation class for the Service interface and register it with the Ratpack registry, then Ratpack will invoke the onStart method when the application starts and the onStop method when the application stops. The methods take an event object as argument and we can use the event object to access the registry if we need to. Writing an implementation for the Service interface can be useful for example to bootstrap the application with initial data or do other things.

In the following example implementation we log when the application starts and stops. In the onStart method we also display a Ratpack banner on the console.

// File: src/main/groovy/com/mrhaki/ratpack/Banner.groovy
package com.mrhaki.ratpack

import com.google.common.io.Resources
import groovy.util.logging.Slf4j
import ratpack.server.Service
import ratpack.server.StartEvent
import ratpack.server.StopEvent
import ratpack.util.RatpackVersion

@Slf4j
class Banner implements Service {

    @Override
    void onStart(StartEvent event) throws Exception {
        log.info('Starting Ratpack application')
        showBanner()        
    }
    
    private void showBanner() {
        // Load banner.txt from the classpath.
        final URL bannerResource = Resources.getResource('banner.txt')
        final String banner = bannerResource.text
        
        // Print banner with version info about Ratpack and JVM.
        println banner
        println ":: Ratpack version : ${RatpackVersion.version}"
        println ":: Java version    : ${System.getProperty('java.version')}"
    }

    @Override
    void onStop(StopEvent event) throws Exception {
        log.info('Stopping Ratpack application')
    }
}

In our Ratpack application we use the Banner class and add it to the registry:

// File: src/ratpack/ratpack.groovy
import com.mrhaki.ratpack.Banner

import static ratpack.groovy.Groovy.ratpack

ratpack {
    bindings {
        // Register Service implementation in the
        // registry. Ratpack picks it up and executes
        // the onStart and onStop methods.
        bind Banner
    }

    handlers {
        get {
            render 'Ratpack rocks!'
        }
    }
}

We also create a file banner.txt in the src/ratpack directory:

    ____        __                   __  
   / __ \____ _/ /_____  ____ ______/ /__
  / /_/ / __ `/ __/ __ \/ __ `/ ___/ //_/
 / _, _/ /_/ / /_/ /_/ / /_/ / /__/ ,<   
/_/ |_|\__,_/\__/ .___/\__,_/\___/_/|_|  
               /_/                       

When we run the application we see the logging messages and our banner:

$ gradle run
:compileJava UP-TO-DATE
:compileGroovy UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:configureRun
:run
[main] INFO ratpack.server.RatpackServer - Starting server...
[main] INFO ratpack.server.RatpackServer - Building registry...
[main] INFO ratpack.server.RatpackServer - Initializing 1 services...
[ratpack-compute-1-1] INFO com.mrhaki.ratpack.Banner - Starting Ratpack application
    ____        __                   __  
   / __ \____ _/ /_____  ____ ______/ /__
  / /_/ / __ `/ __/ __ \/ __ `/ ___/ //_/
 / _, _/ /_/ / /_/ /_/ / /_/ / /__/ ,<   
/_/ |_|\__,_/\__/ .___/\__,_/\___/_/|_|  
               /_/                       

:: Ratpack version : 1.1.1
:: Java version    : 1.8.0_66
[main] INFO ratpack.server.RatpackServer - Ratpack started (development) for http://localhost:5050
> Building 83% > :run

Written with Ratpack 1.1.1.