Quarkus - Application Initialization and Termination

You often need to execute custom actions when the application starts and clean up everything when the application stops.This guide explains how to:

  • be notified when the application starts

  • be notified when the application stops

Prerequisites

To complete this guide, you need:

  • less than 10 minutes

  • an IDE

  • JDK 1.8+ installed with JAVA_HOME configured appropriately

  • Apache Maven 3.5.3+

Solution

We recommend that you follow the instructions in the next sections and create the application step by step.However, you can go right to the completed example.

Clone the Git repository: git clone https://github.com/quarkusio/quarkus-quickstarts.git, or download an archive.

The solution is located in the lifecycle-quickstart directory.

Creating the Maven project

First, we need a new project. Create a new project with the following command:

  1. mvn io.quarkus:quarkus-maven-plugin:1.0.0.CR1:create \
  2. -DprojectGroupId=org.acme \
  3. -DprojectArtifactId=lifecycle-quickstart \
  4. -DclassName="org.acme.events.GreetingResource" \
  5. -Dpath="/hello"
  6. cd lifecycle-quickstart

It generates:

  • the Maven structure

  • a landing page accessible on http://localhost:8080

  • example Dockerfile files for both native and jvm modes

  • the application configuration file

  • an org.acme.events.GreetingResource resource

  • an associated test

Listening for startup and shutdown events

Create a new class named AppLifecycleBean (or pick another name) in the org.acme.events package, and copy thefollowing content:

  1. package org.acme.events;
  2. import javax.enterprise.context.ApplicationScoped;
  3. import javax.enterprise.event.Observes;
  4. import io.quarkus.runtime.ShutdownEvent;
  5. import io.quarkus.runtime.StartupEvent;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. @ApplicationScoped
  9. public class AppLifecycleBean {
  10. private static final Logger LOGGER = LoggerFactory.getLogger("ListenerBean");
  11. void onStart(@Observes StartupEvent ev) { (1)
  12. LOGGER.info("The application is starting...");
  13. }
  14. void onStop(@Observes ShutdownEvent ev) { (2)
  15. LOGGER.info("The application is stopping...");
  16. }
  17. }
  • Method called when the application is starting

  • Method called when the application is terminating

The events are also called in dev mode between each redeployment.
The methods can access injected beans. Check the AppLifecycleBean.java class for details.

What is the difference from @Initialized(ApplicationScoped.class) and @Destroyed(ApplicationScoped.class)

In the JVM mode, there is no real difference, except that StartupEvent is always fired after @Initialized(ApplicationScoped.class) and ShutdownEvent is fired before @Destroyed(ApplicationScoped.class).For a native executable build, however, @Initialized(ApplicationScoped.class) is fired as part of the native build process, whereas StartupEvent is fired when the native image is executed.See Three Phases of Bootstrap and Quarkus Philosophy for more details.

In CDI applications, an event with qualifier @Initialized(ApplicationScoped.class) is fired when the application context is initialized. See the spec for more info.

Package and run the application

Run the application with: ./mvnw compile quarkus:dev, the logged message is printed.When the application is stopped, the second log message is printed.

As usual, the application can be packaged using ./mvnw clean package and executed using the -runner.jar file.You can also generate the native executable using ./mvnw clean package -Pnative.