色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

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

瀏覽:147日期:2023-08-13 14:11:26

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的坑,就是方法內部調用,會使aspect增強失效,那么retry當然也會失效。

例如

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

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

補充知識:Springboot整合Spring Retry實現重試機制

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

Springboot可以通過整合Spring Retry框架實現重試。

下面講一下在之前新建的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')@SpringBootApplicationpublic class BatisApplication { public static void main(String[] args) { SpringApplication.run(BatisApplication.class, args); }}

3、新建重試接口RetryService和實現類RetryServiceImpl

重試接口:

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

接口實現類:

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;@Servicepublic 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('回調方法執行?。?!'); }}

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

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

maxAttempts:重試次數(包括第一次調用)

delay:重試的間隔時間

multiplier:delay時間的間隔倍數

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

@Recover:標記方法為回調方法,傳參與@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

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

可以看到,轉賬操作一共執行了3次,最后執行了回調方法。

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

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

以上這篇淺談spring的重試機制無效@Retryable@EnableRetry就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 黄色三级视频在线播放 | 国产欧美精品区一区二区三区 | 狠狠色丁香婷婷久久综合不卡 | 亚洲国产精品自在现线让你爽 | 亚洲精品色一区二区三区 | 视频精品一区 | 久久一区二区三区99 | 国产91第一页 | 欧美亚洲欧美区 | 手机看片国产 | 波多野结衣免费观看视频 | 久草在线资源视频 | 国产日韩久久 | 成人黄18免费网站 | 国模午夜写真福利视频在线 | 欧美日a | 九一国产精品视频 | 国产在线91区精品 | 一区二三国产 | 一二三区在线观看 | 色偷偷亚洲男人天堂 | 国产99视频精品免视看9 | 日韩一级a毛片欧美一级 | 国产黄色一级网站 | 日韩在线观看一区二区三区 | 免费观看一级一片 | 亚洲国产精品日韩在线观看 | 国产精品亚洲精品 | 国产婷婷成人久久av免费高清 | 中文字幕日韩欧美一区二区三区 | 老司机精品影院一区二区三区 | 精品国产免费人成高清 | 色三级大全高清视频在线观看 | 亚洲99在线的 | 中文在线观看视频 | 日本韩国一级片 | 奇米色88欧美一区二区 | 亚州免费一级毛片 | 日产国产精品久久久久久 | 黄色美女网站视频 | 特级淫片日本高清视频 |