自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

更好的 Java 重試框架 Sisyphus 配置的兩種方式介紹

開發(fā) 后端
這一節(jié)讓我們一起學習下 sisyphus 基于函數(shù)式的配置和注解式的配置。為了滿足更加方便的配置,Retryer 類提供了許多可以配置的信息。

這一節(jié)讓我們一起學習下 sisyphus 基于函數(shù)式的配置和注解式的配置。

函數(shù)式配置概覽

為了滿足更加方便的配置,Retryer 類提供了許多可以配置的信息。

默認配置

  1. /** 
  2.  * 默認配置測試 
  3.  */ 
  4. public void defaultConfigTest() { 
  5.     Retryer.<String>newInstance() 
  6.             .condition(RetryConditions.hasExceptionCause()) 
  7.             .retryWaitContext(RetryWaiter.<String>retryWait(NoRetryWait.class).context()) 
  8.             .maxAttempt(3) 
  9.             .listen(RetryListens.noListen()) 
  10.             .recover(Recovers.noRecover()) 
  11.             .callable(new Callable<String>() { 
  12.                 @Override 
  13.                 public String call() throws Exception { 
  14.                     System.out.println("called..."); 
  15.                     throw new RuntimeException(); 
  16.                 } 
  17.             }).retryCall(); 

 和下面的代碼是等價的:

  1. public void helloTest() { 
  2.     Retryer.<String>newInstance() 
  3.             .callable(new Callable<String>() { 
  4.                 @Override 
  5.                 public String call() throws Exception { 
  6.                     System.out.println("called..."); 
  7.                     throw new RuntimeException(); 
  8.                 } 
  9.             }).retryCall(); 

方法說明

condition

重試觸發(fā)的條件,可以指定多個條件。

默認為拋出異常。

retryWaitContext

重試等待的策略,可以指定多個。

默認為不做任何等待。

maxAttempt

指定最大重試次數(shù),包括第一次執(zhí)行。

默認值:3 次。

listen

指定重試的監(jiān)聽實現(xiàn),默認為不做監(jiān)聽。

recover

當重試完成之后,依然滿足重試條件,則可以指定恢復的策略。

默認不做恢復。

callable

待重試執(zhí)行的方法。

retryCall

觸發(fā)重試執(zhí)行。

接口的詳細介紹

接口及其實現(xiàn)

所有的接口,都可以直接查看對應的子類實例。

用戶自定義

基于替換的靈活性,用戶可以實現(xiàn)接口,定義更符合自己業(yè)務的實現(xiàn)。

sisyphus 注解

配置具有很高的靈活性,但是對于開發(fā)人員的使用,就沒有注解那樣簡單靈活。

所以本框架也實現(xiàn)了基于注解的重試。

設計的規(guī)范

保證接口和注解二者的統(tǒng)一性。

maven 引入

  1. <dependency> 
  2.     <groupId>${project.groupId}</groupId> 
  3.     <artifactId>sisyphus-annotation</artifactId> 
  4.     <version>${project.version}</version> 
  5. </dependency> 

注解

核心注解主要有兩個。

Retry

用于指定重試的相關配置。

  1. /** 
  2.  * 重試注解 
  3.  * 1. 實際需要,只允許放在方法上。 
  4.  * 2. 如果放在接口上,是否所有的子類都生效?為了簡單明確,不提供這種實現(xiàn)。 
  5.  * 3. 保持注解和接口的一致性。{@link com.github.houbb.sisyphus.api.core.Retry} 接口 
  6.  * @author binbin.hou 
  7.  * @since 0.0.3 
  8.  */ 
  9. @Documented 
  10. @Inherited 
  11. @Target(ElementType.METHOD) 
  12. @Retention(RetentionPolicy.RUNTIME) 
  13. @RetryAble(DefaultRetryAbleHandler.class) 
  14. public @interface Retry { 
  15.  
  16.     /** 
  17.      * 重試類實現(xiàn) 
  18.      * @return 重試 
  19.      * @since 0.0.5 
  20.      */ 
  21.     Class<? extends com.github.houbb.sisyphus.api.core.Retry> retry() default DefaultRetry.class; 
  22.  
  23.     /** 
  24.      * 最大嘗試次數(shù) 
  25.      * 1. 包含方法第一次正常執(zhí)行的次數(shù) 
  26.      * @return 次數(shù) 
  27.      */ 
  28.     int maxAttempt() default 3; 
  29.  
  30.     /** 
  31.      * 重試觸發(fā)的場景 
  32.      * @return 重試觸發(fā)的場景 
  33.      */ 
  34.     Class<? extends RetryCondition> condition() default ExceptionCauseRetryCondition.class; 
  35.  
  36.     /** 
  37.      * 監(jiān)聽器 
  38.      * 1. 默認不進行監(jiān)聽 
  39.      * @return 監(jiān)聽器 
  40.      */ 
  41.     Class<? extends RetryListen> listen() default NoRetryListen.class; 
  42.  
  43.     /** 
  44.      * 恢復操作 
  45.      * 1. 默認不進行任何恢復操作 
  46.      * @return 恢復操作對應的類 
  47.      */ 
  48.     Class<? extends Recover> recover() default NoRecover.class; 
  49.  
  50.     /** 
  51.      * 等待策略 
  52.      * 1. 支持指定多個,如果不指定,則不進行任何等待, 
  53.      * @return 等待策略 
  54.      */ 
  55.     RetryWait[] waits() default {}; 
  56.  

RetryWait

用于指定重試的等待策略。

  1. package com.github.houbb.sisyphus.annotation.annotation; 
  2.  
  3. import com.github.houbb.sisyphus.annotation.annotation.metadata.RetryWaitAble; 
  4. import com.github.houbb.sisyphus.annotation.handler.impl.DefaultRetryWaitAbleHandler; 
  5. import com.github.houbb.sisyphus.core.constant.RetryWaitConst; 
  6. import com.github.houbb.sisyphus.core.support.wait.NoRetryWait; 
  7.  
  8. import java.lang.annotation.*; 
  9.  
  10. /** 
  11.  * 重試等待策略 
  12.  * 1. 為了對應重試策略,所有的內置注解應該實現(xiàn)當前的注解。 
  13.  * 2. 是否允許自定義注解? 
  14.  * 
  15.  * 當注解+對象同時出現(xiàn)的時候,視為組合。 
  16.  * 
  17.  * @author binbin.hou 
  18.  * @since 0.0.3 
  19.  */ 
  20. @Retention(RetentionPolicy.RUNTIME) 
  21. @Inherited 
  22. @Documented 
  23. @Target(ElementType.ANNOTATION_TYPE) 
  24. @RetryWaitAble(DefaultRetryWaitAbleHandler.class) 
  25. public @interface RetryWait { 
  26.  
  27.     /** 
  28.      * 默認值 
  29.      * 1. fixed 模式,則對應固定等待時間 
  30.      * 2. 遞增 
  31.      * @return 默認值 
  32.      */ 
  33.     long value() default RetryWaitConst.VALUE_MILLS; 
  34.  
  35.     /** 
  36.      * 最小值 
  37.      * @return 最小值 
  38.      */ 
  39.     long min() default RetryWaitConst.MIN_MILLS; 
  40.  
  41.     /** 
  42.      * 最大值 
  43.      * @return 最大值 
  44.      */ 
  45.     long max() default RetryWaitConst.MAX_MILLS; 
  46.  
  47.     /** 
  48.      * 影響因數(shù) 
  49.      * 1. 遞增重試,默認為 {@link RetryWaitConst#INCREASE_MILLS_FACTOR} 
  50.      * 2. 指數(shù)模式。默認為 {@link RetryWaitConst#MULTIPLY_FACTOR} 
  51.      * @return 影響因數(shù) 
  52.      */ 
  53.     double factor() default Double.MIN_VALUE; 
  54.  
  55.     /** 
  56.      * 指定重試的等待時間 class 信息 
  57.      * @return 重試等待時間 class 
  58.      */ 
  59.     Class<? extends com.github.houbb.sisyphus.api.support.wait.RetryWait> retryWait() default NoRetryWait.class; 
  60.  

注解的使用

定義好了注解,肯定要有注解的相關使用。

關于注解的使用,主要有兩種方式。

Proxy+CGLIB

基于代理模式和字節(jié)碼增強。

如果是項目中沒有使用 spring,直接使用這種方式比較方便。

Spring-AOP

可以和 spring 直接整合。

使用方式和 spring-retry 是一樣的。

這些內容將放在下一節(jié)進行詳細講解。

小結

靈活的配置才能更加符合實際生產使用中的各種需求。

一般實際使用推薦使用注解的配置方式,非常的簡單方便。

 

責任編輯:姜華 來源: 今日頭條
相關推薦

2015-05-06 10:05:22

javajava框架spring aop

2009-06-25 13:43:00

Buffalo AJA

2011-04-02 09:48:38

深拷貝

2011-06-16 10:02:08

JAVA靜態(tài)載入

2009-12-07 13:42:24

WCF框架

2021-05-27 10:57:01

TCP定時器網(wǎng)絡協(xié)議

2021-06-30 07:19:34

SpringBoot定時任務

2010-03-29 18:31:09

Nginx配置

2010-08-06 09:38:11

Flex讀取XML

2023-03-29 13:06:36

2010-10-21 16:24:18

sql server升

2010-03-16 15:23:32

java動態(tài)載入

2011-03-03 10:26:04

Pureftpd

2016-11-07 09:02:02

Malloc內存syscall

2010-07-15 14:38:55

Perl eval函數(shù)

2009-09-08 15:22:20

Spring依賴注入

2010-08-03 13:27:04

FlexBuilder

2024-02-04 09:24:45

MyBatisSQL語句Spring

2010-10-20 15:48:56

SQL Server許

2021-12-08 10:47:35

RabbitMQ 實現(xiàn)延遲
點贊
收藏

51CTO技術棧公眾號