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

Springboot Retry組件@Recover失效問題解決方法

 更新時(shí)間:2021年11月30日 15:46:45   作者:劍客阿良_ALiang  
在使用springboot的retry模塊時(shí),你是否出現(xiàn)過@Recover注解失效的問題呢?不用擔(dān)心,這篇文章就來告訴你解決@Recover失效的辦法,需要的小伙伴可以參考一下

背景

在使用springboot的retry模塊時(shí),你是否出現(xiàn)過@Recover注解失效的問題呢?下面我會(huì)對(duì)該問題進(jìn)行復(fù)現(xiàn),并且簡(jiǎn)要的說下解決方法。

問題復(fù)現(xiàn)

首先我們引入maven

        <dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.6</version>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

主類配置EnableRetry注解

package ai.guiji.csdn;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.retry.annotation.EnableRetry;
 
@EnableRetry
@SpringBootApplication
public class CsdnApplication {
 
  public static void main(String[] args) {
    SpringApplication.run(CsdnApplication.class, args);
  }
}

準(zhǔn)備測(cè)試的Retry組件類代碼

package ai.guiji.csdn.component;
 
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.Component;
 
import java.util.function.Supplier;
 
/** @Author 劍客阿良_ALiang @Date 2021/4/22 16:07 @Description: 重試工具 */
@Slf4j
@Component
public class RetryUtil {
  @Retryable(
      value = Exception.class,
      maxAttempts = 3,
      backoff = @Backoff(delay = 5000, multiplier = 1.5))
  public String retry(Supplier<String> supplier) throws Exception {
    String result = null;
    try {
      result = supplier.get();
    } catch (Exception exception) {
      log.error("異常報(bào)錯(cuò):{}", exception.getMessage());
      throw exception;
    }
    return result;
  }
 
  @Recover
  public void recover(Exception e) {
    log.error("調(diào)用超過3次異常");
  }
}

代碼說明

1、我們可以看到retry方法會(huì)重試supplier的get結(jié)果,捕獲異常并拋出異常。這里拋出后會(huì)被retry捕獲并且重試。

2、maxAttempts參數(shù)為重試的最大次數(shù)。

3、backoff中的delay為兩次重試之間的延遲,multiplier為重試阻尼,可以這么理解,每次重試間隔時(shí)間為上一次重試間隔時(shí)間的倍數(shù)。

4、如果3次重試均拋出異常,則進(jìn)入recover方法。

編寫測(cè)試代碼

package ai.guiji.csdn.component;
 
import cn.hutool.core.convert.Convert;
import cn.hutool.http.HttpUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
 
/** @Author 劍客阿良_ALaing @Date 2021/11/30 13:08 @Description: */
@SpringBootTest
class RetryUtilTest {
  @Autowired private RetryUtil retryUtil;
 
  @Test
  void retry() {
    try {
      System.out.println(Convert.toStr(retryUtil.retry(() -> HttpUtil.post("xxxx", "")), "haha"));
    } catch (Exception exception) {
      exception.printStackTrace();
    }
  }
}

執(zhí)行測(cè)試結(jié)果

2021-11-30 13:37:44.012 ERROR 13600 --- [           main] ai.guiji.csdn.component.RetryUtil        : 異常報(bào)錯(cuò):UnknownHostException: xxxx
2021-11-30 13:37:49.019 ERROR 13600 --- [           main] ai.guiji.csdn.component.RetryUtil        : 異常報(bào)錯(cuò):UnknownHostException: xxxx
2021-11-30 13:37:58.787 ERROR 13600 --- [           main] ai.guiji.csdn.component.RetryUtil        : 異常報(bào)錯(cuò):UnknownHostException: xxxx
org.springframework.retry.ExhaustedRetryException: Cannot locate recovery method; nested exception is cn.hutool.core.io.IORuntimeException: UnknownHostException: xxxx
	at org.springframework.retry.annotation.RecoverAnnotationRecoveryHandler.recover(RecoverAnnotationRecoveryHandler.java:70)
	at org.springframework.retry.interceptor.RetryOperationsInterceptor$ItemRecovererCallback.recover(RetryOperationsInterceptor.java:142)
	at org.springframework.retry.support.RetryTemplate.handleRetryExhausted(RetryTemplate.java:539)
	at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:387)
	at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:225)
	at org.springframework.retry.interceptor.RetryOperationsInterceptor.invoke(RetryOperationsInterceptor.java:116)
	at org.springframework.retry.annotation.AnnotationAwareRetryOperationsInterceptor.invoke(AnnotationAwareRetryOperationsInterceptor.java:163)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750)
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:692)
	at ai.guiji.csdn.component.RetryUtil$$EnhancerBySpringCGLIB$$d209cbc6.retry(<generated>)
	at ai.guiji.csdn.component.RetryUtilTest.retry(RetryUtilTest.java:17)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:688)
	at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
	at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
	at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
	at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
	at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:210)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:206)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:131)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:65)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
	at java.util.ArrayList.forEach(ArrayList.java:1257)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
	at java.util.ArrayList.forEach(ArrayList.java:1257)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
	at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
	at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:108)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:96)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:75)
	at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: cn.hutool.core.io.IORuntimeException: UnknownHostException: xxxx
	at cn.hutool.http.HttpRequest.send(HttpRequest.java:1153)
	at cn.hutool.http.HttpRequest.execute(HttpRequest.java:969)
	at cn.hutool.http.HttpRequest.execute(HttpRequest.java:940)
	at cn.hutool.http.HttpUtil.post(HttpUtil.java:216)
	at cn.hutool.http.HttpUtil.post(HttpUtil.java:197)
	at ai.guiji.csdn.component.RetryUtilTest.lambda$retry$0(RetryUtilTest.java:17)
	at ai.guiji.csdn.component.RetryUtil.retry(RetryUtil.java:22)
	at ai.guiji.csdn.component.RetryUtil$$FastClassBySpringCGLIB$$a565f63f.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:779)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750)
	at org.springframework.retry.interceptor.RetryOperationsInterceptor$1.doWithRetry(RetryOperationsInterceptor.java:93)
	at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:329)
	... 73 more
Caused by: java.net.UnknownHostException: xxxx
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
	at java.net.Socket.connect(Socket.java:589)
	at java.net.Socket.connect(Socket.java:538)
	at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
	at sun.net.www.http.HttpClient.openServer(HttpClient.java:463)
	at sun.net.www.http.HttpClient.openServer(HttpClient.java:558)
	at sun.net.www.http.HttpClient.<init>(HttpClient.java:242)
	at sun.net.www.http.HttpClient.New(HttpClient.java:339)
	at sun.net.www.http.HttpClient.New(HttpClient.java:357)
	at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1220)
	at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1156)
	at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1050)
	at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:984)
	at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1334)
	at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1309)
	at cn.hutool.http.HttpConnection.getOutputStream(HttpConnection.java:451)
	at cn.hutool.http.HttpRequest.sendFormUrlEncoded(HttpRequest.java:1176)
	at cn.hutool.http.HttpRequest.send(HttpRequest.java:1145)
	... 86 more
 
 
Process finished with exit code 0

并沒有進(jìn)入recover方法,注解未觸發(fā)。

問題解決

這里有個(gè)很容易忽視的點(diǎn),就是retry方法是有返回值的,所以recover方法也必須是相同類型帶返回值的方法。所以要把recover方法改一下。

package ai.guiji.csdn.component;
 
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.Component;
 
import java.util.function.Supplier;
 
/** @Author 劍客阿良_ALiang @Date 2021/4/22 16:07 @Description: 重試工具 */
@Slf4j
@Component
public class RetryUtil {
  @Retryable(
      value = Exception.class,
      maxAttempts = 3,
      backoff = @Backoff(delay = 5000, multiplier = 1.5))
  public String retry(Supplier<String> supplier) throws Exception {
    String result = null;
    try {
      result = supplier.get();
    } catch (Exception exception) {
      log.error("異常報(bào)錯(cuò):{}", exception.getMessage());
      throw exception;
    }
    return result;
  }
 
  @Recover
  public String recover(Exception e) {
    log.error("調(diào)用超過3次異常");
    return "調(diào)用超過3次異常";
  }
}

重新執(zhí)行測(cè)試看下結(jié)果

以上就是Springboot Retry組件@Recover失效問題解決方法的詳細(xì)內(nèi)容,更多關(guān)于Springboot Retry 解決@Recover失效的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java編程Commons lang組件簡(jiǎn)介

    Java編程Commons lang組件簡(jiǎn)介

    這篇文章主要介紹了Java編程Commons lang組件的相關(guān)內(nèi)容,十分具有參考意義,需要的朋友可以了解下。
    2017-09-09
  • Spring Boot 整合 Shiro+Thymeleaf過程解析

    Spring Boot 整合 Shiro+Thymeleaf過程解析

    這篇文章主要介紹了Spring Boot 整合 Shiro+Thymeleaf過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • java有界類型參數(shù)的實(shí)例用法

    java有界類型參數(shù)的實(shí)例用法

    小編給大家整理了一篇關(guān)于java有界類型參數(shù)的使用的相關(guān)文章及擴(kuò)展實(shí)例內(nèi)容,有需要的朋友們可以學(xué)習(xí)參考下。
    2021-07-07
  • App登陸java后臺(tái)處理和用戶權(quán)限驗(yàn)證

    App登陸java后臺(tái)處理和用戶權(quán)限驗(yàn)證

    這篇文章主要為大家詳細(xì)介紹了App登陸java后臺(tái)處理和用戶權(quán)限驗(yàn)證,感興趣的朋友可以參考一下
    2016-06-06
  • Java8(291)之后禁用了TLS1.1使JDBC無法用SSL連接SqlServer2008的解決方法

    Java8(291)之后禁用了TLS1.1使JDBC無法用SSL連接SqlServer2008的解決方法

    這篇文章主要介紹了Java8(291)之后禁用了TLS1.1使JDBC無法用SSL連接SqlServer2008的解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-03-03
  • IDEA中scala生成變量后自動(dòng)顯示變量類型問題

    IDEA中scala生成變量后自動(dòng)顯示變量類型問題

    這篇文章主要介紹了IDEA中scala生成變量后自動(dòng)顯示變量類型問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • HTTP中g(shù)et和post的區(qū)別詳解

    HTTP中g(shù)et和post的區(qū)別詳解

    這篇文章主要為大家詳細(xì)介紹了HTTP中g(shù)et和post的區(qū)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • java開發(fā)https請(qǐng)求ssl不受信任問題解決方法

    java開發(fā)https請(qǐng)求ssl不受信任問題解決方法

    這篇文章主要介紹了java開發(fā)https請(qǐng)求ssl不受信任問題解決方法,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • idea2019導(dǎo)入maven項(xiàng)目中的某些問題及解決方法

    idea2019導(dǎo)入maven項(xiàng)目中的某些問題及解決方法

    這篇文章主要介紹了idea2019導(dǎo)入maven項(xiàng)目中的某些問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • Java操作壓縮包解壓過程詳解

    Java操作壓縮包解壓過程詳解

    這篇文章主要介紹了Java操作壓縮包解壓過程,項(xiàng)目開發(fā)中,總會(huì)遇到解壓縮文件的時(shí)候,比如用戶下載多個(gè)文件時(shí),服務(wù)端可以將多個(gè)文件壓縮成一個(gè)文件,用戶上傳資料時(shí),允許上傳壓縮文件,服務(wù)端進(jìn)行解壓讀取每一個(gè)文件,使用場(chǎng)景是很多的,下面來詳細(xì)講解,需要的朋友可以參考下
    2024-10-10

最新評(píng)論

得荣县| 平和县| 锦州市| 女性| 漠河县| 西峡县| 体育| 苏尼特右旗| 化州市| 沙田区| 象州县| 吴旗县| 札达县| 元朗区| 苏尼特右旗| 墨江| 五大连池市| 海安县| 阿荣旗| 通许县| 东乌珠穆沁旗| 陵水| 正安县| 海宁市| 江津市| 乌苏市| 小金县| 洛扎县| 道真| 怀柔区| 桦南县| 长丰县| 东乡县| 新干县| 周口市| 定西市| 松桃| 利川市| 个旧市| 晋州市| 卫辉市|