Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Spring-Retry retry Framework in Spring Boot

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to use Spring-Retry retry framework in Spring Boot". In daily operation, I believe many people have doubts about how to use Spring-Retry retry framework in Spring Boot. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use Spring-Retry retry framework in Spring Boot". Next, please follow the editor to study!

Spring Retry provides the ability to automatically recall failed operations. This is useful in cases where the error may be temporary, such as an instantaneous network failure. Since version 2.2.0, the retry feature has been removed from Spring Batch to become a separate new library: Spring Retry

Maven relies on org.springframework.retry spring-retry org.springframework spring-aspects annotations to enable the Retry function

Use @ EnableRetry annotations in startup classes

Package org.example;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.retry.annotation.EnableRetry;@SpringBootApplication@EnableRetrypublic class RetryApp {public static void main (String [] args) {SpringApplication.run (RetryApp.class, args);}} Note @ Retryable

You need to add the retry annotation @ Retryable to the retried code

Package org.example;import lombok.extern.slf4j.Slf4j;import org.springframework.retry.annotation.Backoff;import org.springframework.retry.annotation.Recover;import org.springframework.retry.annotation.Retryable;import org.springframework.stereotype.Service;import java.time.LocalDateTime;@Service@Slf4jpublic class RetryService {@ Retryable (value = IllegalAccessException.class) public void service1 () throws IllegalAccessException {log.info ("do something... {} ", LocalDateTime.now (); throw new IllegalAccessException (" manual exception ");}}

By default, it will be retried 3 times with an interval of 1 second

We can see it in the annotation @ Retryable

Target ({ElementType.METHOD, ElementType.TYPE}) @ Retention (RetentionPolicy.RUNTIME) @ Documentedpublic @ interface Retryable {/ * Retry interceptor bean name to be applied for retryable method. Is mutually * exclusive with other attributes. * @ return the retry interceptor bean name * / String interceptor () default "; / * Exception types that are retryable. Synonym for includes (). Defaults to empty (and * if excludes is also empty all exceptions are retried). * @ return exception types to retry * / Class

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report