這篇文章將為大家詳細(xì)講解有關(guān)Spring重試支持Spring Retry的示例分析,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
我們提供的服務(wù)有:成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、永清ssl等。為上千企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的永清網(wǎng)站制作公司第一步、引入maven依賴
org.springframework.boot spring-boot-starter-parent 1.5.3.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.retry spring-retry 1.1.2.RELEASE org.aspectj aspectjweaver 1.8.6
第二步、添加@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)沒(méi)有
@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)用異常
關(guān)于“Spring重試支持Spring Retry的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。