review
We learned earlier
better java retry framework sisyphus Introduction to getting started
better java retry framework The story behind
In this section, let us learn about sisyphus based on functional configuration and annotation configuration.
Functional configuration overview
In order to meet more convenient configuration, the Retryer class provides a lot of information that can be configured.
default allocation
/**
* 默认配置测试
*/
public void defaultConfigTest() {
Retryer.<String>newInstance()
.condition(RetryConditions.hasExceptionCause())
.retryWaitContext(RetryWaiter.<String>retryWait(NoRetryWait.class).context())
.maxAttempt(3)
.listen(RetryListens.noListen())
.recover(Recovers.noRecover())
.callable(new Callable<String>() {
@Override
public String call() throws Exception {
System.out.println("called...");
throw new RuntimeException();
}
}).retryCall();
}
It is equivalent to the following code:
public void helloTest() {
Retryer.<String>newInstance()
.callable(new Callable<String>() {
@Override
public String call() throws Exception {
System.out.println("called...");
throw new RuntimeException();
}
}).retryCall();
}
Method description
condition
Retry trigger conditions, multiple conditions can be specified.
The default is to throw an exception.
retryWaitContext
You can specify multiple strategies for retry waiting.
The default is not to do any waiting.
maxAttempt
Specify the maximum number of retries, including the first execution.
Default value: 3 times.
listen
Specify the monitoring implementation for retrying, and the default is not to do the monitoring.
recover
After the retry is completed, the retry conditions are still met, and the recovery strategy can be specified.
No recovery is done by default.
callable
The method to be retryed.
retryCall
Trigger retry execution.
Detailed introduction of the interface
Interface and its implementation
For all interfaces, you can directly view the corresponding subclass instances.
Custom
Based on the flexibility of replacement, users can implement interfaces and define implementations that are more in line with their own business.
sisyphus annotation
The configuration is highly flexible, but for developers' use, it is not as simple and flexible as annotations.
So this framework also implements annotation-based retry.
Design specifications
Ensure the unity of the interface and annotations.
Introduced by maven
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>sisyphus-annotation</artifactId>
<version>${project.version}</version>
</dependency>
annotation
There are two main core annotations.
Retry
Used to specify the relevant configuration for retrying.
/**
* 重试注解
* 1. 实际需要,只允许放在方法上。
* 2. 如果放在接口上,是否所有的子类都生效?为了简单明确,不提供这种实现。
* 3. 保持注解和接口的一致性。{@link com.github.houbb.sisyphus.api.core.Retry} 接口
* @author binbin.hou
* @since 0.0.3
*/
@Documented
@Inherited
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@RetryAble(DefaultRetryAbleHandler.class)
public @interface Retry {
/**
* 重试类实现
* @return 重试
* @since 0.0.5
*/
Class<? extends com.github.houbb.sisyphus.api.core.Retry> retry() default DefaultRetry.class;
/**
* 最大尝试次数
* 1. 包含方法第一次正常执行的次数
* @return 次数
*/
int maxAttempt() default 3;
/**
* 重试触发的场景
* @return 重试触发的场景
*/
Class<? extends RetryCondition> condition() default ExceptionCauseRetryCondition.class;
/**
* 监听器
* 1. 默认不进行监听
* @return 监听器
*/
Class<? extends RetryListen> listen() default NoRetryListen.class;
/**
* 恢复操作
* 1. 默认不进行任何恢复操作
* @return 恢复操作对应的类
*/
Class<? extends Recover> recover() default NoRecover.class;
/**
* 等待策略
* 1. 支持指定多个,如果不指定,则不进行任何等待,
* @return 等待策略
*/
RetryWait[] waits() default {};
}
RetryWait
Used to specify the waiting strategy for retries.
package com.github.houbb.sisyphus.annotation.annotation;
import com.github.houbb.sisyphus.annotation.annotation.metadata.RetryWaitAble;
import com.github.houbb.sisyphus.annotation.handler.impl.DefaultRetryWaitAbleHandler;
import com.github.houbb.sisyphus.core.constant.RetryWaitConst;
import com.github.houbb.sisyphus.core.support.wait.NoRetryWait;
import java.lang.annotation.*;
/**
* 重试等待策略
* 1. 为了对应重试策略,所有的内置注解应该实现当前的注解。
* 2. 是否允许自定义注解?
*
* 当注解+对象同时出现的时候,视为组合。
*
* @author binbin.hou
* @since 0.0.3
*/
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@Target(ElementType.ANNOTATION_TYPE)
@RetryWaitAble(DefaultRetryWaitAbleHandler.class)
public @interface RetryWait {
/**
* 默认值
* 1. fixed 模式,则对应固定等待时间
* 2. 递增
* @return 默认值
*/
long value() default RetryWaitConst.VALUE_MILLS;
/**
* 最小值
* @return 最小值
*/
long min() default RetryWaitConst.MIN_MILLS;
/**
* 最大值
* @return 最大值
*/
long max() default RetryWaitConst.MAX_MILLS;
/**
* 影响因数
* 1. 递增重试,默认为 {@link RetryWaitConst#INCREASE_MILLS_FACTOR}
* 2. 指数模式。默认为 {@link RetryWaitConst#MULTIPLY_FACTOR}
* @return 影响因数
*/
double factor() default Double.MIN_VALUE;
/**
* 指定重试的等待时间 class 信息
* @return 重试等待时间 class
*/
Class<? extends com.github.houbb.sisyphus.api.support.wait.RetryWait> retryWait() default NoRetryWait.class;
}
Use of annotations
Once the annotation is defined, there must be related usage of the annotation.
Regarding the use of annotations, there are mainly two ways.
Proxy+CGLIB
Based on proxy mode and bytecode enhancement.
If spring is not used in the project, it is more convenient to use this method directly.
Spring-AOP
It can be directly integrated with spring.
The usage is the same as spring-retry.
These contents will be explained in detail in the next section.
summary
Flexible configuration can be more in line with various needs in actual production and use.
Generally, it is recommended to use the annotation configuration method in actual use, which is very simple and convenient.
java retry framework sisyphus open source address
I hope this article is helpful to you. If you like it, please like, collect and forward a wave.
I am an old horse, and I look forward to seeing you again next time.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。