Spring Exception retry Framework Spring Retry

Source: Internet
Author: User

Spring retry support is integrated into the spring or Spring boot project, and it supports AOP aspect injection notation, so you must introduce the Aspectjweaver.jar package when you introduce it.

Quick-Integrate code samples:

@Configuration@EnableRetry  Public classApplication {@Bean PublicService Service () {return NewService (); }} @ServiceclassService {  @Retryable (remoteaccessexception.  Class) PublicService () {//... do something    }}

The following is an integration step based on the Spring Boot project:

POM:

        <!--Spring Retry -        <Dependency>            <groupId>Org.springframework.retry</groupId>            <Artifactid>Spring-retry</Artifactid>        </Dependency>        <Dependency>            <groupId>Org.aspectj</groupId>            <Artifactid>Aspectjweaver</Artifactid>        </Dependency>

Service:

 PackageCom.jsoft.springboottest.springboottest1;ImportJava.time.LocalTime;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;Importorg.springframework.remoting.RemoteAccessException;ImportOrg.springframework.retry.annotation.Backoff;ImportOrg.springframework.retry.annotation.Recover;Importorg.springframework.retry.annotation.Retryable;ImportOrg.springframework.stereotype.Service; @Service  Public classRemoteservice {Private Final StaticLogger Logger = Loggerfactory.getlogger (remoteservice.class); @Retryable (Value = {Remoteaccessexception.class}, maxattempts = 3, Backoff = @Backoff (delay = 5000l, multiplier = 1)) Public voidCall ()throwsException {logger.info (Localtime.now ()+ "Do something ..."); Throw NewRemoteaccessexception ("RPC Call exception"); } @Recover  Public voidRecover (remoteaccessexception e) {logger.info (E.getmessage ()); }}

@Retryable annotations

The annotated method retries when an exception occurs

    • Value: Specifies the exception that occurred to retry
    • Include: As with value, the default is null, and when exclude is also empty, all exceptions are retried
    • Exclude: Specifies that exceptions are not retried, default NULL, and when include is empty, all exceptions are retried
    • Maxattemps: Retry count, default 3
    • Backoff: Retry compensation mechanism, default No

@Backoff annotations

    • Delay: Specify retry after delay
    • Multiplier: Specifies a multiple of the delay, such as delay=5000l,multiplier=2, when the first retry is 5 seconds, the second time is 10 seconds, and the third time is 20 seconds

@Recover

When the retry arrives at a specified number of times, the annotated method is called back, and the method can be used for log processing. It is important to note that the exception that occurs is the same as the type of the incoming parameter.

Controller:

 PackageCom.jsoft.springboottest.springboottest1.controller;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RestController;ImportCom.jsoft.springboottest.springboottest1.RemoteService; @RestController Public classTestController {@AutowiredPrivateRemoteservice Remoteservice; @RequestMapping ("/show")     PublicString Show () {Try{remoteservice.call (); } Catch(Exception e) {//TODO auto-generated Catch block//e.printstacktrace ();        }        return"Hello World"; }}

APP:

 PackageCom.jsoft.springboottest.springboottest1;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;ImportOrg.springframework.retry.annotation.EnableRetry;/*** Hello world! **/@SpringBootApplication@EnableRetry  Public classApp { Public Static voidMain (string[] args) {Springapplication.run (App.class, args); }}

Effect:

Description

1. The method using @retryable cannot be called in this class, otherwise the retry mechanism will not take effect. That is, to mark as @service, and then use @autowired injection or @bean to the instance in other classes to take effect.

2, to trigger the @recover method, then the @retryable method can not have a return value, can only be void to take effect.

3, the use of @retryable method inside can not use Try...catch package, to be issued on the exception, or will not trigger.

4, during the retry period this method is synchronous, if using similar to Spring cloud as a framework of the fusing mechanism, you can combine the retry mechanism to retry after the return results.

5, Spring retry can not only inject the way to achieve, but also through the way of API implementation, similar to the mechanism of fusing processing based on the implementation of the API will be more relaxed.

Example code: HTTPS://GITHUB.COM/EASONJIM/5_JAVA_EXAMPLE/TREE/MASTER/SPRINGBOOTTEST/SPRINGBOOTTEST3

Reference:

http://blog.csdn.net/u014513883/article/details/52371198 (The above sections are transferred from this article)

Https://github.com/spring-projects/spring-retry (official website)

http://www.jianshu.com/p/314059943f1c

http://www.broadview.com.cn/article/233 (Fuse retry)

Spring Exception retry Framework Spring Retry

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.