av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Spring框架中一個(gè)有用的小組件之Spring Retry組件詳解

瀏覽:3日期:2023-06-30 09:02:33
1、概述

Spring Retry 是Spring框架中的一個(gè)組件,它提供了自動(dòng)重新調(diào)用失敗操作的能力。這在錯(cuò)誤可能是暫時(shí)發(fā)生的(如瞬時(shí)網(wǎng)絡(luò)故障)的情況下很有幫助。

在本文中,我們將看到使用Spring Retry的各種方式:注解、RetryTemplate以及回調(diào)。

2、Maven依賴(lài)

讓我們首先將spring-retry依賴(lài)項(xiàng)添加到我們的pom.xml文件中:

<dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId> <version>1.2.5.RELEASE</version></dependency>

我們還需要將Spring AOP添加到我們的項(xiàng)目中:

<dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.2.8.RELEASE</version></dependency>

可以查看Maven Central來(lái)獲取最新版本的spring-retry和spring-aspects 依賴(lài)項(xiàng)。

3、開(kāi)啟Spring Retry

要在應(yīng)用程序中啟用Spring Retry,我們需要將@EnableRetry注釋添加到我們的@Configuration類(lèi):

@Configuration@EnableRetrypublic class AppConfig { ... }4、使用Spring Retry

4.1、@Retryable而不用恢復(fù)

我們可以使用@Retryable注解為方法添加重試功能:

@Servicepublic interface MyService { @Retryable(value = RuntimeException.class) void retryService(String sql);}

在這里,當(dāng)拋出RuntimeException時(shí)嘗試重試。

根據(jù)@Retryable的默認(rèn)行為,重試最多可能發(fā)生3次,重試之間有1秒的延遲。

4.2、@Retryable和@Recover

現(xiàn)在讓我們使用@Recover注解添加一個(gè)恢復(fù)方法:

@Servicepublic interface MyService { @Retryable(value = SQLException.class) void retryServiceWithRecovery(String sql) throws SQLException; @Recover void recover(SQLException e, String sql);}

這里,當(dāng)拋出SQLException時(shí)重試會(huì)嘗試運(yùn)行。 當(dāng)@Retryable方法因指定異常而失敗時(shí),@Recover注解定義了一個(gè)單獨(dú)的恢復(fù)方法。

因此,如果retryServiceWithRecovery方法在三次嘗試之后還是拋出了SQLException,那么recover()方法將被調(diào)用。

恢復(fù)處理程序的第一個(gè)參數(shù)應(yīng)該是Throwable類(lèi)型(可選)和相同的返回類(lèi)型。其余的參數(shù)按相同順序從失敗方法的參數(shù)列表中填充。

4.3、自定義@Retryable的行為

為了自定義重試的行為,我們可以使用參數(shù)maxAttempts和backoff:

@Servicepublic interface MyService { @Retryable( value = SQLException.class, maxAttempts = 2, backoff = @Backoff(delay = 100)) void retryServiceWithCustomization(String sql) throws SQLException;}

這樣最多將有兩次嘗試和100毫秒的延遲。

4.4、使用Spring Properties

我們還可以在@Retryable注解中使用properties。

為了演示這一點(diǎn),我們將看到如何將delay和maxAttempts的值外部化到一個(gè)properties文件中。

首先,讓我們?cè)诿麨閞etryConfig.properties的文件中定義屬性:

retry.maxAttempts=2retry.maxDelay=100

然后我們指示@Configuration類(lèi)加載這個(gè)文件:

@PropertySource('classpath:retryConfig.properties')public class AppConfig { ... }// ...

最后,我們可以在@Retryable的定義中注入retry.maxAttempts和retry.maxDelay的值:

@Service public interface MyService { @Retryable( value = SQLException.class, maxAttemptsExpression = '${retry.maxAttempts}', backoff = @Backoff(delayExpression = '${retry.maxDelay}')) void retryServiceWithExternalizedConfiguration(String sql) throws SQLException; }

請(qǐng)注意,我們現(xiàn)在使用的是maxAttemptsExpression和delayExpression而不是maxAttempts和delay。

5、RetryTemplate

5.1、RetryOperations

Spring Retry提供了RetryOperations接口,它提供了一組execute()方法:

public interface RetryOperations { <T> T execute(RetryCallback<T> retryCallback) throws Exception; ...}

execute()方法的參數(shù)RetryCallback,是一個(gè)接口,可以插入需要在失敗時(shí)重試的業(yè)務(wù)邏輯:

public interface RetryCallback<T> { T doWithRetry(RetryContext context) throws Throwable;}

5.2、RetryTemplate配置

RetryTemplate是RetryOperations的一個(gè)實(shí)現(xiàn)。

讓我們?cè)贎Configuration類(lèi)中配置一個(gè)RetryTemplate的bean:

@Configurationpublic class AppConfig { //... @Bean public RetryTemplate retryTemplate() {RetryTemplate retryTemplate = new RetryTemplate();FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();fixedBackOffPolicy.setBackOffPeriod(2000l);retryTemplate.setBackOffPolicy(fixedBackOffPolicy);SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();retryPolicy.setMaxAttempts(2);retryTemplate.setRetryPolicy(retryPolicy);return retryTemplate; }}

這個(gè)RetryPolicy確定了何時(shí)應(yīng)該重試操作。

其中SimpleRetryPolicy定義了重試的固定次數(shù),另一方面,BackOffPolicy用于控制重試嘗試之間的回退。

最后,F(xiàn)ixedBackOffPolicy會(huì)使重試在繼續(xù)之前暫停一段固定的時(shí)間。

5.3、使用RetryTemplate

要使用重試處理來(lái)運(yùn)行代碼,我們可以調(diào)用retryTemplate.execute()方法:

retryTemplate.execute(new RetryCallback<Void, RuntimeException>() { @Override public Void doWithRetry(RetryContext arg0) {myService.templateRetryService();... }});

我們可以使用lambda表達(dá)式代替匿名類(lèi):

retryTemplate.execute(arg0 -> { myService.templateRetryService(); return null;});6、監(jiān)聽(tīng)器

監(jiān)聽(tīng)器在重試時(shí)提供另外的回調(diào)。我們可以用這些來(lái)關(guān)注跨不同重試的各個(gè)橫切點(diǎn)。

6.1、添加回調(diào)

回調(diào)在RetryListener接口中提供:

public class DefaultListenerSupport extends RetryListenerSupport { @Override public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {logger.info('onClose');...super.close(context, callback, throwable); } @Override public <T, E extends Throwable> void onError(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {logger.info('onError'); ...super.onError(context, callback, throwable); } @Override public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) {logger.info('onOpen');...return super.open(context, callback); }}

open和close的回調(diào)在整個(gè)重試之前和之后執(zhí)行,而onError應(yīng)用于單個(gè)RetryCallback調(diào)用。

6.2、注冊(cè)監(jiān)聽(tīng)器

接下來(lái),我們將我們的監(jiān)聽(tīng)器(DefaultListenerSupport)注冊(cè)到我們的RetryTemplate bean:

@Configurationpublic class AppConfig { ... @Bean public RetryTemplate retryTemplate() {RetryTemplate retryTemplate = new RetryTemplate();...retryTemplate.registerListener(new DefaultListenerSupport());return retryTemplate; }}7、測(cè)試結(jié)果

為了完成我們的示例,讓我們驗(yàn)證一下結(jié)果:

@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration( classes = AppConfig.class, loader = AnnotationConfigContextLoader.class)public class SpringRetryIntegrationTest { @Autowired private MyService myService; @Autowired private RetryTemplate retryTemplate; @Test(expected = RuntimeException.class) public void givenTemplateRetryService_whenCallWithException_thenRetry() {retryTemplate.execute(arg0 -> { myService.templateRetryService(); return null;}); }}

從測(cè)試日志中可以看出,我們已經(jīng)正確配置了RetryTemplate和RetryListener:

2020-01-09 20:04:10 [main] INFO c.p.s.DefaultListenerSupport - onOpen 2020-01-09 20:04:10 [main] INFO c.pinmost.springretry.MyServiceImpl - throw RuntimeException in method templateRetryService() 2020-01-09 20:04:10 [main] INFO c.p.s.DefaultListenerSupport - onError 2020-01-09 20:04:12 [main] INFO c.pinmost.springretry.MyServiceImpl - throw RuntimeException in method templateRetryService() 2020-01-09 20:04:12 [main] INFO c.p.s.DefaultListenerSupport - onError 2020-01-09 20:04:12 [main] INFO c.p.s.DefaultListenerSupport - onClose

8、結(jié)論

在本文中,我們看到了如何使用注解、RetryTemplate和回調(diào)監(jiān)聽(tīng)器來(lái)使用Spring Retry。

原文地址:https://www.baeldung.com/spring-retry

到此這篇關(guān)于Spring框架中一個(gè)有用的小組件:Spring Retry的文章就介紹到這了,更多相關(guān)Spring Spring Retry組件內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 全免费a级毛片免费看视频免 | 99精品99久久久久久宅男 | 最近日韩中文字幕 | 国产精品视频久久 | 亚洲第1页 | 美女视频网站久久 | 日本不卡一区二区三区在线观看 | 影音先锋男 | 欧美精品在线观看 | 天天操 夜夜操 | 亚洲第一免费播放区 | 国精品一区二区 | 春色av| 国内av在线| 狠狠色狠狠色综合日日92 | 人人干在线视频 | 男女羞羞网站 | 国产观看 | 91在线一区| 亚洲一区二区在线视频 | 另类 综合 日韩 欧美 亚洲 | 在线观看中文字幕 | 中文字幕欧美在线观看 | 日本中文字幕在线视频 | 亚洲av毛片成人精品 | 国产日韩精品一区二区 | 在线播放国产一区二区三区 | 亚洲成人综合社区 | 国产精品国产三级国产aⅴ原创 | 精品不卡 | 国产一区二区三区久久久久久久久 | 999久久久久久久久6666 | 淫片一级国产 | 久久精品久久久久久 | 一区二区精品 | 久久国产精99精产国高潮 | 欧美一级做a爰片免费视频 国产美女特级嫩嫩嫩bbb片 | 亚洲在线一区二区三区 | 亚洲热在线视频 | 欧美精品乱码久久久久久按摩 | 国产激情视频网站 |