# Spring Boot Actuator

Get familiar with the Spring Boot Actuator and its features.

Time: 20 minutes.

## Code Snippet Manager Actuator

You will continue with the Code Snippet Manager code. For this lab you will need the snippet-code-manager-jpa-rest project code. You will reuse the code from previous labs.

1. Open a browser and hit the url: http://start.spring.io

2. Click the Switch to the full version link.

3. Fill out the Code Snippet Manager Actuator Project metadata with (See Figure 1.0):

   Table 1. Code Snippet Manager Actuator - metadata
   Property | Value
   --- | ---
   Group: | io.pivotal.workshop
   Artifact: | code-snippet-manager-actuator
   Name: | code-snippet-manager-actuator
   Package Name: | io.pivotal.workshop.snippet
   Dependencies: | Web, DevTools, Groovy Templates, JPA, Rest Repositories, H2, MySQL, Actuator, HATEOAS
   Spring Boot: | 2.0.0.M7

   Figure 1.0: Spring Initializr - http://start.spring.io/

   Tip | You can choose either Maven or Gradle project types.

4. Type Web, DevTools, Groovy Templates, JPA, Rest Repositories, H2, MySQL, Actuator, HATEOAS in the Dependencies field and press Enter.

5. Click the Generate Project button.

6. Unzip the file in any directory you want.

7. Import your project in any IDE you want.

8. Copy all the packages (with code) into the new project.

9. The packages: domain, repository and config will remain with no change.

### Spring Boot Actuator: Review Endpoints

1. In the src/main/resources/application.properties:

   - Enable all the endpoints

   - Change the default path /actuator, instead use the path /admin

     application.properties

         management.endpoints.web.base-path=/admin
         management.endpoints.web.expose=*

2. Run the application and make sure all the endpoints are enable. Review all of them.

### Spring Boot Actuator: HealthIndicator

Using the same code, imagine that we are going to use a FileSystem to save some information. A specific path, by using the snippet.path property. We need to know:

- If the path doesn’t exists, report out of service.

- If the path exists but doesn’t have writing permissions, report system down.

- If the path exists and it has write permissions, report up.

1. Let’s create a properties holder class. Add the SnippetProperties class in the io.pivotal.workshop.snippet.config package:

   io.pivotal.workshop.snippet.config.SnippetProperties.java

       package io.pivotal.workshop.snippet.config;

       import org.springframework.boot.context.properties.ConfigurationProperties;

       @ConfigurationProperties(prefix = "snippet")
       public class SnippetProperties {

               private String path;

               public String getPath() {
                       return path;
               }

               public void setPath(String path) {
                       this.path = path;
               }

       }

   The above code will hold the information about the path with the property: snippet.path.

2. Create the SnippetHealthCheck class in the io.pivotal.workshop.snippet.actuator package:

   io.pivotal.workshop.snippet.actuator.SnippetHealthCheck.java

       package io.pivotal.workshop.snippet.actuator;

       import java.io.File;

       import org.springframework.beans.factory.annotation.Value;
       import org.springframework.boot.actuate.health.Health;
       import org.springframework.boot.actuate.health.HealthIndicator;
       import org.springframework.stereotype.Component;

       @Component
       public class SnippetHealthCheck implements HealthIndicator {

               private String path;

               public SnippetHealthCheck(@Value("${snippet.path:/tmp}")String path){
                       this.path = path;
               }

               @Override
               public Health health() {

                       try {

                               File file = new File(path);
                               if(file.exists()){

                                       if(file.canWrite())
                                               return Health.up().build();

                                       return Health.down().build();

                               }else{
                                       return Health.outOfService().build();
                               }
                       }catch(Exception ex) {
                               return Health.down(ex).build();
                       }
               }

       }

   Take a look at the constructor. If the snippet.path is not found, it will use the /tmp as default path.

3. In the src/main/resources/application.properties file add the following property:

       ## Snippet
       snippet.path=/tmp/snippets

   Tip | Do not create this folder yet, we will make sure our custom health indicator works.

   Tip | If you are using Windows try to use the C:/tmp/snippets or C:\\tmp\\snippets format.

4. If you run the application and go to the http://localhost:8080/admin/health, you should have something like the following Figure 3.0:

   Figure 3.0: Spring Boot Actuator - http://localhost:8080/admin/health

   Here you can see the SnippetHealthCheck value: OUT_OF_SERVICE.

5. Create the /tmp/snippets (or C:\tmp\snippets for Windows) path but add/modify only read access, and then you can refresh the page, you should see the same as Figure 4.0:

   Figure 4.0: Spring Boot Actuator - http://localhost:8080/admin/health

   Here you can see the SnippetHealthCheck value: DOWN.

6. Modify the path to write access and refresh, you should have the following, see Figure 5.0.

   Figure 5.0: Spring Boot Actuator - http://localhost:8080/admin/health

   Here you can see the SnippetHealthCheck value: UP.

### CHALLENGES

1. Add a graceful shutdown to the application by executing the following command:

       curl -X POST localhost:8080/admin/shutdown

   Tip | If you are a Windows user, you can use the POSTMAN (https://www.getpostman.com/) application to execute the POST.

### HOMEWORK

Review the Micrometer technology (http://micrometer.io) and apply it to the Code Snippet Manager.