Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Ratpacked: Execute Code On Start and Stop Application Lifecycle Events

DZone's Guide to

Ratpacked: Execute Code On Start and Stop Application Lifecycle Events

Learn how to make Ratpack invoke the onStart method when the application starts and the onStop method when the application stops.

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

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:

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:
ratpack ,java ,groovy

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}