最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring重試支持Spring Retry的方法

 更新時(shí)間:2018年04月06日 11:18:17   作者:人生的旅客  
本篇文章主要介紹了Spring重試支持Spring Retry的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

本文介紹了Spring重試支持Spring Retry的方法,分享給大家,具體如下:

第一步、引入maven依賴

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.3.RELEASE</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>

第二步、添加@Retryable和@Recover注解

package hello;

import org.springframework.remoting.RemoteAccessException;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

@Service
public class RemoteService {
@Retryable(value= {RemoteAccessException.class},maxAttempts = 3,backoff = @Backoff(delay = 5000l,multiplier = 1))
public void call() throws Exception {
    System.out.println("do something...");
    throw new RemoteAccessException("RPC調(diào)用異常");
}
@Recover
public void recover(RemoteAccessException e) {
    System.out.println(e.getMessage());
}
}

@Retryable注解
被注解的方法發(fā)生異常時(shí)會(huì)重試
value:指定發(fā)生的異常進(jìn)行重試
include:和value一樣,默認(rèn)空,當(dāng)exclude也為空時(shí),所有異常都重試
exclude:指定異常不重試,默認(rèn)空,當(dāng)include也為空時(shí),所有異常都重試
maxAttemps:重試次數(shù),默認(rèn)3
backoff:重試補(bǔ)償機(jī)制,默認(rèn)沒有

@Backoff注解
delay:指定延遲后重試
multiplier:指定延遲的倍數(shù),比如delay=5000l,multiplier=2時(shí),第一次重試為5秒后,第二次為10秒,第三次為20秒

@Recover
當(dāng)重試到達(dá)指定次數(shù)時(shí),被注解的方法將被回調(diào),可以在該方法中進(jìn)行日志處理。需要注意的是發(fā)生的異常和入?yún)㈩愋鸵恢聲r(shí)才會(huì)回調(diào)

第三步、SpringBoot方式啟動(dòng)容器、測(cè)試

添加@EnableRetry注解,啟用重試功能

package hello;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.retry.annotation.EnableRetry;

@SpringBootApplication
@EnableRetry
public class Application {

  public static void main(String[] args) throws Exception {
    ApplicationContext annotationContext = new AnnotationConfigApplicationContext("hello");
    RemoteService remoteService = annotationContext.getBean("remoteService", RemoteService.class);
    remoteService.call();
  }
}

運(yùn)行結(jié)果:

16:50:51.012 [main] DEBUG org.springframework.retry.support.RetryTemplate - Retry: count=0
do something…
16:50:51.025 [main] DEBUG org.springframework.retry.backoff.ExponentialBackOffPolicy - Sleeping for 5000
16:50:56.026 [main] DEBUG org.springframework.retry.support.RetryTemplate - Checking for rethrow: count=1
16:50:56.026 [main] DEBUG org.springframework.retry.support.RetryTemplate - Retry: count=1
do something…
16:50:56.026 [main] DEBUG org.springframework.retry.backoff.ExponentialBackOffPolicy - Sleeping for 5000
16:51:01.026 [main] DEBUG org.springframework.retry.support.RetryTemplate - Checking for rethrow: count=2
16:51:01.027 [main] DEBUG org.springframework.retry.support.RetryTemplate - Retry: count=2
do something…
16:51:01.027 [main] DEBUG org.springframework.retry.support.RetryTemplate - Checking for rethrow: count=3
16:51:01.027 [main] DEBUG org.springframework.retry.support.RetryTemplate - Retry failed last attempt: count=3
RPC調(diào)用異常

參考 :https://github.com/spring-projects/spring-retry

補(bǔ)充

對(duì)于非冪等的請(qǐng)求(比如新增,更新操作),千萬不要使用重試,對(duì)數(shù)據(jù)一致性會(huì)造成很大影響。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java HtmlParse提取標(biāo)簽中的值操作

    Java HtmlParse提取標(biāo)簽中的值操作

    這篇文章主要介紹了Java HtmlParse提取標(biāo)簽中的值操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • springboot項(xiàng)目完整后端請(qǐng)求Controller層優(yōu)雅處理

    springboot項(xiàng)目完整后端請(qǐng)求Controller層優(yōu)雅處理

    這篇文章主要為大家介紹了springboot項(xiàng)目Controller層代碼的優(yōu)雅處理實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • springboot接口服務(wù),防刷、防止請(qǐng)求攻擊,AOP實(shí)現(xiàn)方式

    springboot接口服務(wù),防刷、防止請(qǐng)求攻擊,AOP實(shí)現(xiàn)方式

    本文介紹了如何使用AOP防止Spring?Boot接口服務(wù)被網(wǎng)絡(luò)攻擊,通過在pom.xml中加入AOP依賴,創(chuàng)建自定義注解類和AOP切面,以及在業(yè)務(wù)類中使用這些注解,可以有效地對(duì)接口進(jìn)行保護(hù),測(cè)試表明,這種方法有效地防止了網(wǎng)絡(luò)攻擊
    2024-11-11
  • IDEA的Project無法正常顯示的問題解決

    IDEA的Project無法正常顯示的問題解決

    本文主要介紹了IDEA的Project無法正常顯示的問題解決,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-02-02
  • Java后臺(tái)接收數(shù)據(jù)的三種方式(url、form-data與application/json)

    Java后臺(tái)接收數(shù)據(jù)的三種方式(url、form-data與application/json)

    本文主要介紹了Java后臺(tái)接收數(shù)據(jù)的三種方式(url、form-data與application/json),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • maven中profile的使用

    maven中profile的使用

    本文主要介紹了maven中profile的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • Java?Spring的使用注解開發(fā)詳解

    Java?Spring的使用注解開發(fā)詳解

    這篇文章主要為大家介紹了Java?Spring注解開發(fā),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • Java并發(fā)編程之原子操作類詳情

    Java并發(fā)編程之原子操作類詳情

    這篇文章主要介紹了Java并發(fā)編程之原子操作類詳情,文章基于Java并發(fā)編程展開相關(guān)內(nèi)容,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-04-04
  • Java合并區(qū)間的實(shí)現(xiàn)

    Java合并區(qū)間的實(shí)現(xiàn)

    本文主要介紹了Java合并區(qū)間的實(shí)現(xiàn),通過合理使用集合類和排序算法,可以有效地解決合并區(qū)間問題,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-08-08
  • Java將數(shù)組轉(zhuǎn)換成字符串的四種方法總結(jié)

    Java將數(shù)組轉(zhuǎn)換成字符串的四種方法總結(jié)

    這篇文章主要給大家介紹了關(guān)于Java將數(shù)組轉(zhuǎn)換成字符串的四種方法,每種方法都有其適用的場(chǎng)景和優(yōu)缺點(diǎn),文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-12-12

最新評(píng)論

鄂托克旗| 遵化市| 鱼台县| 佛冈县| 浮山县| 泾源县| 门头沟区| 八宿县| 太康县| 温州市| 霍州市| 九龙城区| 罗江县| 广昌县| 正蓝旗| 定西市| 台安县| 新邵县| 永兴县| 兰溪市| 布拖县| 龙井市| 江门市| 五大连池市| 枝江市| 蒲城县| 田林县| 平定县| 扎兰屯市| 信阳市| 晋宁县| 光泽县| 镇平县| 田东县| 遂宁市| 治多县| 凤庆县| 崇文区| 正镶白旗| 东丰县| 阿鲁科尔沁旗|