Compare commits
4 Commits
v0.0.3
...
27bbfc60d5
| Author | SHA1 | Date | |
|---|---|---|---|
| 27bbfc60d5 | |||
|
|
d643f8f0c8 | ||
| 6fdb02f3c1 | |||
|
|
80fe2f8cb9 |
6
pom.xml
6
pom.xml
@@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>com.imyeyu.java</groupId>
|
||||
<artifactId>timi-java</artifactId>
|
||||
<version>0.0.3</version>
|
||||
<version>0.0.5</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
@@ -50,7 +50,7 @@
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.36</version>
|
||||
<version>1.18.46</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
@@ -92,7 +92,7 @@
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.40</version>
|
||||
<version>1.18.46</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
|
||||
@@ -72,6 +72,30 @@ public interface TimiJava {
|
||||
return !isEmpty(object);
|
||||
}
|
||||
|
||||
static boolean hasEmpty(Object... objects) {
|
||||
for (Object object : objects) {
|
||||
if (isEmpty(object)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static boolean allNotEmpty(Object... objects) {
|
||||
for (Object object : objects) {
|
||||
if (isEmpty(object)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
static <T> T nullIfEmpty(T object) {
|
||||
if (isEmpty(object)) {
|
||||
return null;
|
||||
}
|
||||
return object;
|
||||
}
|
||||
|
||||
/**
|
||||
* 为空时取默认值
|
||||
*
|
||||
|
||||
@@ -3,9 +3,11 @@ package com.imyeyu.java.thread;
|
||||
import com.imyeyu.java.bean.Callback;
|
||||
import com.imyeyu.java.bean.CallbackArg;
|
||||
import com.imyeyu.java.bean.timi.TimiException;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -40,6 +42,41 @@ public class AsyncRetryExecutor {
|
||||
@Getter
|
||||
private volatile boolean isRunning = false;
|
||||
|
||||
/**
|
||||
* 任务
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2025-11-06 23:37
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class Task {
|
||||
|
||||
/** 必填回调方法 */
|
||||
private Callback callback;
|
||||
|
||||
/** 最大重试次数,-1 为无限重试 */
|
||||
@Builder.Default
|
||||
private int maxRetry = DEFAULT_MAX_RETRY;
|
||||
|
||||
/** 重试间隔毫秒数 */
|
||||
@Builder.Default
|
||||
private long retryInterval = DEFAULT_RETRY_INTERVAL;
|
||||
|
||||
/** 线程名前缀 */
|
||||
@Builder.Default
|
||||
private String threadNamePrefix = DEFAULT_THREAD_NAME_PREFIX;
|
||||
|
||||
/** 是否设置为守护线程 */
|
||||
@Builder.Default
|
||||
private boolean daemon = DEFAULT_DAEMON;
|
||||
|
||||
/** 重试耗尽时的异常回调 */
|
||||
private CallbackArg<Exception> onRetryExhausted;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据构建器参数创建异步重试执行器
|
||||
*
|
||||
@@ -62,7 +99,9 @@ public class AsyncRetryExecutor {
|
||||
}
|
||||
if (0 < task.maxRetry && task.maxRetry < retryCount) {
|
||||
// 超过重试次数
|
||||
task.onRetryExhausted.handler(e);
|
||||
if (task.onRetryExhausted != null) {
|
||||
task.onRetryExhausted.handler(e);
|
||||
}
|
||||
break;
|
||||
}
|
||||
// 重试
|
||||
@@ -79,32 +118,13 @@ public class AsyncRetryExecutor {
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造器
|
||||
* 使用默认参数创建执行器
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2025-11-06 23:37
|
||||
* @param task 任务
|
||||
* @return 执行器实例
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
public static class Task {
|
||||
|
||||
/** 必填回调方法 */
|
||||
private Callback callback;
|
||||
|
||||
/** 最大重试次数,-1 为无限重试 */
|
||||
private int maxRetry = DEFAULT_MAX_RETRY;
|
||||
|
||||
/** 重试间隔毫秒数 */
|
||||
private long retryInterval = DEFAULT_RETRY_INTERVAL;
|
||||
|
||||
/** 线程名前缀 */
|
||||
private String threadNamePrefix = DEFAULT_THREAD_NAME_PREFIX;
|
||||
|
||||
/** 是否设置为守护线程 */
|
||||
private boolean daemon = DEFAULT_DAEMON;
|
||||
|
||||
/** 重试耗尽时的异常回调 */
|
||||
private CallbackArg<Exception> onRetryExhausted;
|
||||
public static AsyncRetryExecutor create(Task task) {
|
||||
return new AsyncRetryExecutor(task);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user