欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺談spring的重試機制無效@Retryable@EnableRetry

 更新時間:2020年09月18日 10:28:43   作者:Singlerr  
這篇文章主要介紹了淺談spring的重試機制無效@Retryable@EnableRetry,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

spring-retry模塊支持方法和類、接口、枚舉級別的重試

方式很簡單,引入pom包

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>lastest</version>
</parent>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.retry/spring-retry -->
<dependency>
  <groupId>org.springframework.retry</groupId>
  <artifactId>spring-retry</artifactId>
  <version>1.1.2.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.8.6</version>
</dependency>

然后在@Configuration注解的類中添加@EnableRetry

最后在想要重試的方法上添加@Retryable(Exception.class)

由于retry用到了aspect增強,所有會有aspect的坑,就是方法內(nèi)部調(diào)用,會使aspect增強失效,那么retry當然也會失效。

例如

public class demo {
  public void A() {
    B();
  }

  @Retryable(Exception.class)
  public void B() {
    throw new RuntimeException("retry...");
  }
}

這種情況B()不會重試。

補充知識:Springboot整合Spring Retry實現(xiàn)重試機制

在項目開發(fā)過程中,經(jīng)常會有這樣的情況:第一次執(zhí)行一個操作不成功,考慮到可能是網(wǎng)絡原因造成,就多執(zhí)行幾次操作,直到得到想要的結果為止,這就是重試機制。

Springboot可以通過整合Spring Retry框架實現(xiàn)重試。

下面講一下在之前新建的ibatis項目基礎上整合Spring Retry框架的步驟:

1、首先要在pom.xml配置中加入spring-retry的依賴:

<dependency>
  <groupId>org.springframework.retry</groupId>
  <artifactId>spring-retry</artifactId>
</dependency>
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
</dependency>

2、在啟動類中加入重試注解@EnableRetry。

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.retry.annotation.EnableRetry;

@EnableRetry //重試注解
@MapperScan("com.batis.mapper")
@SpringBootApplication
public class BatisApplication {
  public static void main(String[] args) {
    SpringApplication.run(BatisApplication.class, args);
  }
}

3、新建重試接口RetryService和實現(xiàn)類RetryServiceImpl

重試接口:

public interface RetryService {
  void retryTransferAccounts(int fromAccountId, int toAccountId, float money) throws Exception;
}

接口實現(xiàn)類:

import com.batis.mapper.AccountMapper;
import com.batis.model.Account;
import com.batis.service.RetryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class RetryServiceImpl implements RetryService {
  @Autowired
  private AccountMapper accountMapper;

  @Transactional
  @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 3000, multiplier = 1, maxDelay = 10000))
  @Override
  public void retryTransferAccounts(int fromAccountId, int toAccountId, float money) throws Exception {
    Account fromAccount = accountMapper.findOne(fromAccountId);
    fromAccount.setBalance(fromAccount.getBalance() - money);
    accountMapper.update(fromAccount);

    int a = 2 / 0;
    Account toAccount = accountMapper.findOne(toAccountId);
    toAccount.setBalance(toAccount.getBalance() + money);
    accountMapper.update(toAccount);
    throw new Exception();
  }

  @Recover
  public void recover(Exception e) {
    System.out.println("回調(diào)方法執(zhí)行?。?!");
  }
}

@Retryable:標記當前方法會使用重試機制

value:重試的觸發(fā)機制,當遇到Exception異常的時候,會觸發(fā)重試

maxAttempts:重試次數(shù)(包括第一次調(diào)用)

delay:重試的間隔時間

multiplier:delay時間的間隔倍數(shù)

maxDelay:重試次數(shù)之間的最大時間間隔,默認為0,如果小于delay的設置,則默認為30000L

@Recover:標記方法為回調(diào)方法,傳參與@Retryable的value值需一致

4、新建重試控制器類RetryController

import com.batis.service.RetryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/retry")
public class RetryController {
  @Autowired
  private RetryService retryService;

  @RequestMapping(value = "/transfer", method = RequestMethod.GET)
  public String transferAccounts() {
    try {
      retryService.retryTransferAccounts(1, 2, 200);
      return "ok";
    } catch (Exception e) {
      return "no";
    }
  }
}

5、啟動ibatis項目進行測試,在瀏覽器地址欄輸入:http://localhost:8080/retry/transfer

可以看到,轉賬操作一共執(zhí)行了3次,最后執(zhí)行了回調(diào)方法。

至此Springboot整合Spring Retry的步驟已經(jīng)完成,測試也非常成功!

有可以改進的地方希望諸位同學不要吝惜筆墨,加以指正,萬分感謝!

以上這篇淺談spring的重試機制無效@Retryable@EnableRetry就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • maven 刪除下載失敗的包的方法

    maven 刪除下載失敗的包的方法

    本文介紹了當Maven包報紅時,使用刪除相關文件的方法來解決該問題,具有一定的參考價值,感興趣的可以了解一下
    2023-09-09
  • springboot項目編譯提示無效的源發(fā)行版17解決辦法

    springboot項目編譯提示無效的源發(fā)行版17解決辦法

    這篇文章主要給大家介紹了關于springboot項目編譯提示無效的源發(fā)行版17解決辦法,這個錯誤意味著你的Spring Boot項目正在使用Java 17這個版本,但是你的項目中未配置正確的Java版本,需要的朋友可以參考下
    2023-06-06
  • Servlet機制Pushlet原理及用法詳解

    Servlet機制Pushlet原理及用法詳解

    這篇文章主要介紹了Servlet機制Pushlet原理及用法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-10-10
  • Java包裝類的概述與應用

    Java包裝類的概述與應用

    包裝類使用起來非常方便,但是沒有對應的方法來操作這些基本數(shù)據(jù)類型,可以使用一個類,把基本類型的數(shù)據(jù)裝起來,在類中定義一些方法,我們可以使用類中的方法來操作這些基本類型的數(shù)據(jù),這篇文章主要給大家介紹了關于Java包裝類的相關資料,需要的朋友可以參考下
    2022-04-04
  • 淺談System.getenv()和System.getProperty()的區(qū)別

    淺談System.getenv()和System.getProperty()的區(qū)別

    這篇文章主要介紹了System.getenv()和System.getProperty()的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • 從面試中的問題分析ThreadLocal

    從面試中的問題分析ThreadLocal

    這篇文章主要介紹了從面試中的問題分析ThreadLocal,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,下面我們來一起學習一下吧
    2019-06-06
  • Apache?Commons?Config管理配置文件核心功能使用

    Apache?Commons?Config管理配置文件核心功能使用

    這篇文章主要為大家介紹了Apache?Commons?Config管理和使用配置文件核心深入探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-12-12
  • Java實現(xiàn)高效隨機數(shù)算法的示例代碼

    Java實現(xiàn)高效隨機數(shù)算法的示例代碼

    這篇文章主要介紹了Java實現(xiàn)高效隨機數(shù)算法的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-02-02
  • 詳解java內(nèi)部類的訪問格式和規(guī)則

    詳解java內(nèi)部類的訪問格式和規(guī)則

    在本文里我們給大家詳細分享了關于java內(nèi)部類的訪問格式和規(guī)則知識點內(nèi)容,有興趣的朋友們學習下。
    2018-10-10
  • Java 實現(xiàn)棧的三種方式

    Java 實現(xiàn)棧的三種方式

    這篇文章主要介紹了棧:LIFO(后進先出),自己實現(xiàn)一個棧,要求這個棧具有push()、pop()(返回棧頂元素并出棧)、peek() (返回棧頂元素不出棧)、isEmpty()這些基本的方法,需要的朋友可以參考下
    2020-12-12

最新評論