Merge pull request 'v0.0.4' (#9) from dev into master
Reviewed-on: #9
This commit was merged in pull request #9.
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
<groupId>com.imyeyu.java</groupId>
|
<groupId>com.imyeyu.java</groupId>
|
||||||
<artifactId>timi-java</artifactId>
|
<artifactId>timi-java</artifactId>
|
||||||
<version>0.0.3</version>
|
<version>0.0.4</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
|||||||
@@ -3,9 +3,11 @@ package com.imyeyu.java.thread;
|
|||||||
import com.imyeyu.java.bean.Callback;
|
import com.imyeyu.java.bean.Callback;
|
||||||
import com.imyeyu.java.bean.CallbackArg;
|
import com.imyeyu.java.bean.CallbackArg;
|
||||||
import com.imyeyu.java.bean.timi.TimiException;
|
import com.imyeyu.java.bean.timi.TimiException;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
@@ -40,6 +42,41 @@ public class AsyncRetryExecutor {
|
|||||||
@Getter
|
@Getter
|
||||||
private volatile boolean isRunning = false;
|
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) {
|
if (0 < task.maxRetry && task.maxRetry < retryCount) {
|
||||||
// 超过重试次数
|
// 超过重试次数
|
||||||
task.onRetryExhausted.handler(e);
|
if (task.onRetryExhausted != null) {
|
||||||
|
task.onRetryExhausted.handler(e);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// 重试
|
// 重试
|
||||||
@@ -79,32 +118,13 @@ public class AsyncRetryExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造器
|
* 使用默认参数创建执行器
|
||||||
*
|
*
|
||||||
* @author 夜雨
|
* @param task 任务
|
||||||
* @since 2025-11-06 23:37
|
* @return 执行器实例
|
||||||
*/
|
*/
|
||||||
@Data
|
public static AsyncRetryExecutor create(Task task) {
|
||||||
@Builder
|
return new AsyncRetryExecutor(task);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user