🏠Spring BootFailure Analyzers in Spring Boot and How they Work

Failure Analyzers in Spring Boot and How they Work

In this post, We will take a look at how Failure Analyzers in Spring Boot work and how to implement them to handle startup failures in spring boot.

Introduction to Failure analyzers

Failure Analyzers is a diagnostics feature from Spring Boot to deal with startup failures. If your application fails to start, These FailureAnalyzers would try to provide a detailed message to solve the issue. For instance, You may see the following error when port 8080 is already in use.

Note that, This error message is from PortInUseFailureAnalyzer which deals with PortInUseException.

You can find a list of FailureAnalyzers under org.springframework.boot.diagnostics.analyzer package.

Next, Let’s learn to create a simple failure analyzer.

Identify a Failure Scenario

Let’s say one of the bean will fail to create and throw a MyCustomApplicationException. Let’s try and see how error message looks like without failure analyzers.

public class MyCustomApplicationException extends RuntimeException {
    public MyCustomApplicationException(){
        super("I Like to Break Things!");
    }
}Code language: PHP (php)

Next, We have here the bean that will cause a failure at startup.

@Service
public class HelloService {

    public HelloService() {
        throw new MyCustomApplicationException();  //This will throw error on startup
    }

    public String hello() {
        return "Hello There...!";
    }
}Code language: PHP (php)

With the above setup, if we start the application the following exception will occur.

Default behaviour without Failure Analyzers

As we know how the default behaviour looks like, Let’s create a failure analyzer.

Create a custom Failure Analyzer

All failure analyzers must implement the FailureAnalyzer interface. And, The easier way to do this by extending the AbstractFailureAnalyzer class. When registered, Each of these analyzers will get a chance to diagnose the issue and provide a FailureAnalysis. So let’s write one for MyCustomApplicationException.

public class MyCustomApplicationFailureAnalyzer extends AbstractFailureAnalyzer<MyCustomApplicationException> {
    @Override
    protected FailureAnalysis analyze(Throwable rootFailure, MyCustomApplicationException cause) {
        return new FailureAnalysis("Yeah, He likes to break Things...!",
                        "Ummm... Don't use 'MyCustomApplicationException'...! Duh..!?🤦",
                        cause);
    }
}Code language: Java (java)

Note that, We are returning a failure analysis with the appropriate cause and action you should take. Here, you may further evaluate the cause object and further customize the responses. Now, you need to register this analyzer so Spring Boot is aware of its existence.

Register the Custom Analyzer in Spring Boot Factories

First, you need to create a spring.factories file under src/main/resources/META-INF directory. If you don’t have this directory then create it. Add the following content to register our analyzer.

org.springframework.boot.diagnostics.FailureAnalyzer=com.springhow.examples.springbootfailureanalyzers.MyCustomApplicationFailureAnalyzerCode language: Properties (properties)

Note that, Spring Boot needs exception analyzers to be in place even before the application startup, We have no option than to load it as factory objects.

Test the Custom Spring Boot Failure Analyzer

Simply run the application and you will get the following output.

Even though the messages are comical, I believe they are self explanatory.

Summary

To summarize, We learned how Failure analyzers work and how to implement them in Spring boot. If you liked this article, the following articles may interest you as well.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *