GithubHelp home page GithubHelp logo

springbootretrypattern's Introduction

SpringBootRetryPattern

Example of Retry Pattern based on Spring Boot

Main dependencies:

<dependencies>  
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
   <dependency>
      <groupId>org.springframework.retry</groupId>
      <artifactId>spring-retry</artifactId>
  </dependency>
  <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-aspects</artifactId>
  </dependency>
 </dependencies>

Enabling Retry:

@SpringBootApplication
@EnableRetry
public class Main {

  public static void main(String[] args) {
    SpringApplication.run(Main.class, args);
  }
}

Defining controller class:

  @RestController
  public class Controller {

    @GetMapping(value = "/")
    @Retryable(
            value = {Exception.class}, 
            maxAttempts = 1,
            backoff = @Backoff(1000))//defining retry configuration
    public String home() {

      RestTemplate restTemplate = new RestTemplate(); //creating RestTemplate instance

      ResponseEntity<String> request_body = restTemplate.exchange("https://jsonplaceholder.typicode.com/posts/1", 
          HttpMethod.GET, null, String.class );//making a request to jsonplacholder api

      String answer = request_body.getBody();

      return answer;
    }

    @Recover//defining recover method to SQLExceptions errors
    public String recover(SQLException e) {

      return "from recover method";
    }
    @Recover//defining recover method to Exceptions errors
    public String recover1(Exception e) {

      return "from recover method2";
    }	
  }

Configuring retryable in method using @Retryable parameters:

  • value = {Exception.class}//if any exception occurs the recover methos is triggered, you can define any exception class for example SQLException
  • maxAttempts = 1, //attempts alloweds if any exception occurs before trigger the recover method
  • backoff = @Backoff(1000)// setting the time for attempts
    •     @GetMapping(value = "/")
          @Retryable(
                  value = {Exception.class}, 
                  maxAttempts = 1,
                  backoff = @Backoff(1000))//defining retry configuration
          public String home() {
      
            RestTemplate restTemplate = new RestTemplate(); //creating RestTemplate instance
      
            ResponseEntity<String> request_body = restTemplate.exchange("https://jsonplaceholder.typicode.com/posts/1", 
                HttpMethod.GET, null, String.class );//making a request to jsonplacholder api
      
            String answer = request_body.getBody();
      
            return answer;
          }
      

      Defining recover method for Exception class errors in home method:

          @Recover//defining recover method to Exceptions errors
          public String recover1(Exception e) {
      
            return "from recover method1";
          }
      

      Behavior:

      If any error occurs in the home method call, the recover method recover 1 is triggered, you can make a test setting wrong the call to jsonplaceholder api, for example writing the api endpoint wrong, and the answer will be

      "from recover method1"

springbootretrypattern's People

Contributors

itqpleyva avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.