欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2021年07月23日 09:15:50   作者:碼農(nóng)熊貓  
Spring Retry 是從 Spring batch 中獨立出來的一個功能,主要實現(xiàn)了重試和熔斷,對于那些重試后不會改變結(jié)果,毫無意義的操作,不建議使用重試,今天通過本文給大家介紹Spring Retry組件詳解,感興趣的朋友一起看看吧

1、概述

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

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

2、Maven依賴

讓我們首先將spring-retry依賴項添加到我們的pom.xml文件中:

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

我們還需要將Spring AOP添加到我們的項目中:

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

可以查看Maven Central來獲取最新版本的spring-retry
spring-aspects 依賴項。

3、開啟Spring Retry

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

@Configuration
@EnableRetry
public class AppConfig { ... }

4、使用Spring Retry

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

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

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

}

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

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

4.2、@Retryable@Recover

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

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

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

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

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

4.3、自定義@Retryable的行為

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

@Service
public 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。

為了演示這一點,我們將看到如何將delaymaxAttempts的值外部化到一個properties文件中。

首先,讓我們在名為retryConfig.properties的文件中定義屬性:

retry.maxAttempts=2
retry.maxDelay=100

然后我們指示@Configuration類加載這個文件:

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

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

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

請注意,我們現(xiàn)在使用的是maxAttemptsExpressiondelayExpression而不是maxAttemptsdelay。

5、RetryTemplate

5.1、RetryOperations

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

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

    ...
}

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

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

5.2、RetryTemplate配置

RetryTemplateRetryOperations的一個實現(xiàn)。

讓我們在@Configuration類中配置一個RetryTemplate的bean:

@Configuration
public 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;
    }
}

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

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

最后,FixedBackOffPolicy會使重試在繼續(xù)之前暫停一段固定的時間。

5.3、使用RetryTemplate

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

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

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

retryTemplate.execute(arg0 -> {
    myService.templateRetryService();
    return null;
});

6、監(jiān)聽器

監(jiān)聽器在重試時提供另外的回調(diào)。我們可以用這些來關(guā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);
    }
}

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

6.2、注冊監(jiān)聽器

接下來,我們將我們的監(jiān)聽器(DefaultListenerSupport)注冊到我們的RetryTemplate bean:

@Configuration
public class AppConfig {
    ...

    @Bean
    public RetryTemplate retryTemplate() {
        RetryTemplate retryTemplate = new RetryTemplate();
        ...
        retryTemplate.registerListener(new DefaultListenerSupport());
        return retryTemplate;
    }
}

7、測試結(jié)果

為了完成我們的示例,讓我們驗證一下結(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;
        });
    }
}

從測試日志中可以看出,我們已經(jīng)正確配置了RetryTemplateRetryListener

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)聽器來使用Spring Retry。

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

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

相關(guān)文章

  • java使用歸并刪除法刪除二叉樹中節(jié)點的方法

    java使用歸并刪除法刪除二叉樹中節(jié)點的方法

    這篇文章主要介紹了java使用歸并刪除法刪除二叉樹中節(jié)點的方法,實例分析了java二叉樹算法的相關(guān)操作技巧,需要的朋友可以參考下
    2015-05-05
  • 詳解eclipse項目中的.classpath文件原理

    詳解eclipse項目中的.classpath文件原理

    這篇文章介紹了eclipse項目中的.classpath文件的原理,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-12-12
  • RabbitMQ,RocketMQ,Kafka?事務(wù)性,消息丟失,消息順序性和消息重復(fù)發(fā)送的處理策略問題

    RabbitMQ,RocketMQ,Kafka?事務(wù)性,消息丟失,消息順序性和消息重復(fù)發(fā)送的處理策略問題

    這篇文章主要介紹了RabbitMQ,RocketMQ,Kafka?事務(wù)性,消息丟失,消息順序性和消息重復(fù)發(fā)送的處理策略,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • Java中如何將list轉(zhuǎn)為樹形結(jié)構(gòu)

    Java中如何將list轉(zhuǎn)為樹形結(jié)構(gòu)

    這篇文章主要介紹了Java中如何將list轉(zhuǎn)為樹形結(jié)構(gòu),本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-09-09
  • java中@JsonValue和@JsonCreator使用

    java中@JsonValue和@JsonCreator使用

    本文主要介紹了java中@JsonValue和@JsonCreator使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-06-06
  • Spring?Security過濾器鏈體系的實例詳解

    Spring?Security過濾器鏈體系的實例詳解

    這篇文章主要介紹了Spring?Security過濾器鏈體系,通過思維導(dǎo)圖可以很好的幫助大家理解配置類的相關(guān)知識,結(jié)合實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-02-02
  • java agent 使用及實現(xiàn)代碼

    java agent 使用及實現(xiàn)代碼

    java agent的作用可以在字節(jié)碼這個層面對類和方法進(jìn)行修改的技術(shù),能夠在不影響編譯的情況下,修改字節(jié)碼。本文主要給大家講解java agent 使用及實現(xiàn)代碼,感興趣的朋友一起看看吧
    2018-07-07
  • Jmeter?BlazeMeter實現(xiàn)web錄制過程

    Jmeter?BlazeMeter實現(xiàn)web錄制過程

    BlazeMeter是一款與Apache JMeter兼容的chrome插件,采用BlazeMeter可以方便的進(jìn)行流量錄制和腳本生成,作為接口測試腳本編寫的一個基礎(chǔ),這篇文章主要介紹了Jmeter?BlazeMeter實現(xiàn)web錄制,需要的朋友可以參考下
    2021-12-12
  • Lombok注解之@SuperBuilder--解決無法builder父類屬性問題

    Lombok注解之@SuperBuilder--解決無法builder父類屬性問題

    這篇文章主要介紹了Lombok注解之@SuperBuilder--解決無法builder父類屬性問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • 淺談SpringBoot中的Bean初始化方法?@PostConstruct

    淺談SpringBoot中的Bean初始化方法?@PostConstruct

    這篇文章主要介紹了SpringBoot中的Bean初始化方法?@PostConstruct,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11

最新評論