In this post, we’ll configure and utilize the Spring Boot Actuator shutdown and startup endpoints. The shutdown endpoint is employed to terminate the Spring Boot application, while the startup endpoint is not responsible for launching the application. Instead, the startup endpoint monitors all loaded components at the application’s start. Let’s explore how to configure these actuator endpoints for startup and shutdown in the Spring Boot application.
Spring Boot Actuator Configuration
To load the Actuator endpoints, add the Spring Boot Starter Actuator dependencies to the pom.xml
. To enable the endpoints in the REST web service, include the Spring Web dependencies along with the Spring Boot Starter Actuator. The Spring Boot Actuator exposes the health endpoint by default. Ensure that the Spring Boot application is configured to include startup and shutdown endpoints. Below is an example pom.xml
file containing the necessary dependencies:
pom.xml
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
Spring Boot Actuator Startup Endpoint Code
The Spring Boot application does not inherently store information about startup beans in memory. To capture startup bean information and make it accessible via the startup endpoint, you need to store this information in memory. This can be achieved using the BufferingApplicationStartup
class. To store startup information, include this class in the Spring Boot main class. The following code example illustrates how to utilize the BufferingApplicationStartup
class for storing startup information.
SpringBootActuatorApplication.java
package com.test; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.metrics.buffering.BufferingApplicationStartup; @SpringBootApplication public class SpringBootActuatorApplication { public static void main(String[] args) { // SpringApplication.run(SpringBootActuatorApplication.class, args); SpringApplication app = new SpringApplication(SpringBootActuatorApplication.class); app.setApplicationStartup(new BufferingApplicationStartup(1000)); app.run(args); } }
By using the BufferingApplicationStartup
class with an appropriate buffer size, you can capture and store startup bean information in memory for retrieval via the startup endpoint. Adjust the buffer size as needed based on your application requirements.
Spring Boot Actuator Startup and Shutdown Configuration
By default, the Spring Boot Actuator does not expose the startup and shutdown endpoints. To enable and configure them, you need to include specific settings in the application.properties
file. Ensure that the startup and shutdown endpoints are added to the list of management endpoints and are enabled. The following configurations should be added to the application.properties
file.
application.properties
management.endpoints.web.exposure.include=health,startup,shutdown management.endpoint.startup.enabled=true management.endpoint.shutdown.enabled=true
Expose the Startup and Shutdown in Actuator endpoints
To view the endpoints, start the Spring Boot application. In the list of endpoints, the Spring Boot Actuator should display the startup and shutdown endpoints. To confirm that the startup and shutdown endpoints are correctly loaded, make a request to the Actuator endpoint URL. The Actuator endpoint list will appear as illustrated below
Ensure that the startup and shutdown endpoints are visible in the Actuator endpoint list, and you can access them via their respective URLs. Adjust the URL and port according to your specific setup.
Spring Boot Actuator Startup Endpoint
Accessing the startup endpoint in the Spring Boot Actuator requires the use of HTTP POST requests. The HTTP GET method is not applicable for startup endpoints, and attempting to access it through a browser won’t work. Instead, you should invoke it using tools like curl
or Postman. Below is an example of calling the startup endpoint using Postman, where all the application startup loading information will be included in the response:
To access the Spring Boot Actuator startup endpoint using the curl
command line tool, use the following command:
curl --location --request POST 'http://localhost:8080/actuator/startup'
Execute this command in your terminal to make a POST request to the startup endpoint. Adjust the URL and port as necessary based on your specific application configuration.
Spring Boot Actuator Shutdown Endpoint
The shutdown endpoint can be accessed through the same URL as the startup endpoint. It exclusively accepts HTTP POST requests and is not accessible through a web browser. To interact with the shutdown endpoint, it should be invoked using tools such as the curl
command or Postman. The provided example illustrates how to make a call to the Spring Boot Actuator shutdown endpoint URL and showcases the response received. Upon invoking this endpoint, the Spring Boot application will initiate an automatic shutdown.
To access the Spring Boot Actuator shutdown endpoint using the curl
command, utilize the following command in your terminal:
curl --location --request POST 'http://localhost:8080/actuator/shutdown'
Execute this command in your terminal to initiate a POST request to the shutdown endpoint. Adjust the URL and port accordingly based on your specific application configuration.