retryAttempt);
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/retry-api/src/main/java/com/poldroc/retry/api/support/stop/RetryStop.java:
--------------------------------------------------------------------------------
1 | package com.poldroc.retry.api.support.stop;
2 |
3 | import com.poldroc.retry.api.model.RetryAttempt;
4 | /**
5 | * 结束的条件
6 | * @author Poldroc
7 | * @since 2024/7/11
8 | */
9 |
10 | public interface RetryStop {
11 |
12 | /**
13 | * 停止执行重试
14 | * @param attempt 执行信息
15 | * @return 是否停止
16 | */
17 | boolean stop(final RetryAttempt attempt);
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/retry-api/src/main/java/com/poldroc/retry/api/support/wait/RetryWait.java:
--------------------------------------------------------------------------------
1 | package com.poldroc.retry.api.support.wait;
2 |
3 | import com.poldroc.retry.api.context.RetryWaitContext;
4 | import com.poldroc.retry.api.model.WaitTime;
5 |
6 | import java.lang.annotation.Annotation;
7 |
8 | /**
9 | * 重试等待策略
10 | * 1. 所有的实现必须要有无参构造器,因为会基于反射处理类信息(newInstance)。
11 | * 2. 尽可能的保证为线程安全的,比如 stateless。
12 | * @author Poldroc
13 | * @since 2024/7/11
14 | */
15 |
16 | public interface RetryWait{
17 |
18 | /**
19 | * 计算等待时间
20 | * @param retryWaitContext 上下文信息
21 | * @return 等待时间的结果信息
22 | */
23 | WaitTime waitTime(final RetryWaitContext retryWaitContext);
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/retry-common/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | io.github.poldroc
8 | roc-retry
9 | 1.1
10 |
11 |
12 | retry-common
13 |
14 |
15 | 8
16 | 8
17 | UTF-8
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/retry-common/src/main/java/com/poldroc/retry/common/annotation/NotThreadSafe.java:
--------------------------------------------------------------------------------
1 | package com.poldroc.retry.common.annotation;
2 |
3 | import java.lang.annotation.*;
4 | /**
5 | * 线程不安全安全注解
6 | * @author Poldroc
7 | * @since 2024/7/11
8 | */
9 |
10 | @Documented
11 | @Inherited
12 | @Target({ElementType.TYPE, ElementType.METHOD})
13 | @Retention(RetentionPolicy.RUNTIME)
14 | public @interface NotThreadSafe {
15 | }
16 |
--------------------------------------------------------------------------------
/retry-common/src/main/java/com/poldroc/retry/common/annotation/ThreadSafe.java:
--------------------------------------------------------------------------------
1 | package com.poldroc.retry.common.annotation;
2 |
3 | import java.lang.annotation.*;
4 | /**
5 | * 线程安全注解
6 | * 放在类上,标识当前类为线程安全的。
7 | * 放在方法上,标识方法是线程安全的。
8 | *
9 | * 注意:目前此注解仅供内部使用,用来标识类是否线程安全。(表示作者的预期) 真正效果需要验证。
10 | *
11 | * 后期用途:可能会直接基于 class 进行反射创建,要求有些类需要显示指定这个注解。
12 | * @author Poldroc
13 | * @since 2024/7/11
14 | */
15 |
16 | @Documented
17 | @Inherited
18 | @Target({ElementType.TYPE, ElementType.METHOD})
19 | @Retention(RetentionPolicy.RUNTIME)
20 | public @interface ThreadSafe {
21 | }
22 |
--------------------------------------------------------------------------------
/retry-common/src/main/java/com/poldroc/retry/common/constant/enums/ProxyTypeEnum.java:
--------------------------------------------------------------------------------
1 | package com.poldroc.retry.common.constant.enums;
2 |
3 | public enum ProxyTypeEnum {
4 | NONE,
5 | DYNAMIC,
6 | CGLIB;
7 |
8 | private ProxyTypeEnum() {
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/retry-common/src/main/java/com/poldroc/retry/common/support/instance/Instance.java:
--------------------------------------------------------------------------------
1 | package com.poldroc.retry.common.support.instance;
2 |
3 | /**
4 | * 实例化对象的接口
5 | * 1. 使用此类的 class 必须有无参构造器
6 | * 2. 当前类出于测试阶段。
7 | * @author Poldroc
8 | * @since 2024/7/11
9 | */
10 |
11 | public interface Instance {
12 |
13 | /**
14 | * 获取对象的单例对象
15 | * 1. 需要保证对象的线程安全性。
16 | * 2. 只有在同一个分组返回的对象才会是单例,否则返回 newInstance()
17 | * @param tClass class 类型
18 | * @param groupName 分组名称
19 | * @param 泛型
20 | * @return 实例化对象
21 | */
22 | T singleton(final Class tClass,
23 | final String groupName);
24 |
25 | /**
26 | * 获取对象的单例对象
27 | * 1. 需要保证对象的线程安全性。
28 | * @param tClass class 类型
29 | * @param 泛型
30 | * @return 实例化对象
31 | */
32 | T singleton(final Class tClass);
33 |
34 | /**
35 | * 获取每个线程内唯一的实例化对象
36 | * 注意:可能会内存泄漏的场景。
37 | * (1) 只要这个线程对象被gc回收,就不会出现内存泄露,但在threadLocal设为null和线程结束这段时间不会被回收的,就发生了我们认为的内存泄露。
38 | * 最要命的是线程对象不被回收的情况,这就发生了真正意义上的内存泄露。比如使用线程池的时候,线程结束是不会销毁的,会再次使用的。就可能出现内存泄露。
39 | * 参考资料:https://www.cnblogs.com/onlywujun/p/3524675.html
40 | * @param tClass class 类型
41 | * @param 泛型
42 | * @return 实例化对象
43 | * @see java.lang.ref.WeakReference 弱引用
44 | */
45 | T threadLocal(final Class tClass);
46 |
47 | /**
48 | * 多例对象,每次都是全新的创建
49 | * @param tClass class 类型
50 | * @param 泛型
51 | * @return 实例化对象
52 | */
53 | T multiple(final Class tClass);
54 |
55 | /**
56 | * 线程安全对象
57 | * 1. 判断当前类是否拥有 {@link com.poldroc.retry.common.annotation.ThreadSafe} 注解,
58 | * 如果有,则直接创建单例对象。如果不是,则创建多例对象。
59 | * @param tClass class 类型
60 | * @param 泛型
61 | * @return 实例化对象
62 | */
63 | T threadSafe(final Class tClass);
64 |
65 | }
66 |
--------------------------------------------------------------------------------
/retry-common/src/main/java/com/poldroc/retry/common/support/instance/impl/InstanceFactory.java:
--------------------------------------------------------------------------------
1 | package com.poldroc.retry.common.support.instance.impl;
2 |
3 | import com.poldroc.retry.common.annotation.ThreadSafe;
4 | import com.poldroc.retry.common.support.instance.Instance;
5 | import com.poldroc.retry.common.util.ArgUtil;
6 |
7 | import java.util.Map;
8 | import java.util.concurrent.ConcurrentHashMap;
9 |
10 | /**
11 | * 实例化工厂类
12 | *
13 | * @author Poldroc
14 | * @since 2024/7/11
15 | */
16 | @ThreadSafe
17 | public class InstanceFactory implements Instance {
18 |
19 | private InstanceFactory() {
20 | }
21 |
22 | /**
23 | * 单例 map 对象
24 | * key 是 class 的全称
25 | */
26 | private final Map singletonMap = new ConcurrentHashMap<>();
27 |
28 | /**
29 | * 线程内的 map 对象
30 | */
31 | private ThreadLocal