Compare commits
7
Commits
v0.0.3
..
02a13377bc
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
02a13377bc | ||
|
|
8e1a1a1673 | ||
|
|
99fb2f62f2 | ||
|
|
e2bba01daf | ||
|
|
c99df7370c | ||
|
|
d643f8f0c8 | ||
|
|
80fe2f8cb9 |
@@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>com.imyeyu.java</groupId>
|
||||
<artifactId>timi-java</artifactId>
|
||||
<version>0.0.3</version>
|
||||
<version>0.0.7</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>
|
||||
|
||||
@@ -5,7 +5,6 @@ import java.io.StringWriter;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 通用工具
|
||||
@@ -72,6 +71,39 @@ public interface TimiJava {
|
||||
return !isEmpty(object);
|
||||
}
|
||||
|
||||
static boolean hasEmpty(Object... objects) {
|
||||
for (Object object : objects) {
|
||||
if (isEmpty(object)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static boolean allEmpty(Object... objects) {
|
||||
for (Object object : objects) {
|
||||
if (isNotEmpty(object)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 为空时取默认值
|
||||
*
|
||||
@@ -81,7 +113,7 @@ public interface TimiJava {
|
||||
* @param <T> 对象类型
|
||||
*/
|
||||
static <T> T defaultIfNull(T obj, T defaultObj) {
|
||||
return Objects.requireNonNullElse(obj, defaultObj);
|
||||
return obj != null ? obj : defaultObj;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -30,4 +30,22 @@ public class BasePage {
|
||||
public void next() {
|
||||
index++;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取偏移量
|
||||
*
|
||||
* @return 偏移量
|
||||
*/
|
||||
public long getOffset() {
|
||||
return (long) index * size;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取限制数量
|
||||
*
|
||||
* @return 限制数量
|
||||
*/
|
||||
public long getLimit() {
|
||||
return size;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,81 +0,0 @@
|
||||
package com.imyeyu.java.bean;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 多语言消息接口
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2024-04-01 10:28
|
||||
*/
|
||||
public interface LanguageMsgMapping<T> {
|
||||
|
||||
/**
|
||||
* 设置消息
|
||||
*
|
||||
* @param msg 消息
|
||||
* @return 原对象
|
||||
*/
|
||||
T msg(String msg);
|
||||
|
||||
/**
|
||||
* 设置消息多语言键
|
||||
*
|
||||
* @param msgKey 键
|
||||
* @return 原对象
|
||||
*/
|
||||
T msgKey(String msgKey);
|
||||
|
||||
/**
|
||||
* 设置消息多语言键并附加插值参数
|
||||
*
|
||||
* @param msgKey 键
|
||||
* @param msgArgs 插值参数
|
||||
* @return 原对象
|
||||
*/
|
||||
T msgKey(String msgKey, Map<String, Object> msgArgs);
|
||||
|
||||
/**
|
||||
* 设置消息
|
||||
*
|
||||
* @param msg 消息
|
||||
*/
|
||||
void setMsg(String msg);
|
||||
|
||||
/**
|
||||
* 获取消息
|
||||
*
|
||||
* @return 消息
|
||||
*/
|
||||
String getMsg();
|
||||
|
||||
/**
|
||||
* 设置消息多语言键
|
||||
*
|
||||
* @param msgKey 键
|
||||
*/
|
||||
void setMsgKey(String msgKey);
|
||||
|
||||
|
||||
/**
|
||||
* 设置消息多语言键并附加插值参数列表
|
||||
*
|
||||
* @param msgKey 键
|
||||
* @param msgArgs 插值参数
|
||||
*/
|
||||
void setMsgKey(String msgKey, Map<String, Object> msgArgs);
|
||||
|
||||
/**
|
||||
* 获取消息多语言键
|
||||
*
|
||||
* @return 键
|
||||
*/
|
||||
String getMsgKey();
|
||||
|
||||
/**
|
||||
* 获取附加插值参数
|
||||
*
|
||||
* @return 附加插值参数列表
|
||||
*/
|
||||
Map<String, Object> getMsgArgs();
|
||||
}
|
||||
@@ -109,7 +109,7 @@ public enum TimiCode {
|
||||
* @return 返回对象
|
||||
*/
|
||||
public TimiResponse<?> toResponse() {
|
||||
return new TimiResponse<>(this).msg(toString());
|
||||
return new TimiResponse<>(this).message(toString());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.imyeyu.java.bean.timi;
|
||||
|
||||
import com.imyeyu.java.bean.LanguageMsgMapping;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Map;
|
||||
@@ -11,81 +10,53 @@ import java.util.Map;
|
||||
* @author 夜雨
|
||||
* @since 2023-04-27 15:55
|
||||
*/
|
||||
public class TimiError extends AssertionError implements LanguageMsgMapping<TimiError> {
|
||||
@Getter
|
||||
public class TimiError extends AssertionError {
|
||||
|
||||
/** 代码 */
|
||||
@Getter
|
||||
protected final TimiCode code;
|
||||
|
||||
protected transient String msgKey;
|
||||
/** 消息代码 */
|
||||
protected final String messageCode;
|
||||
|
||||
protected transient Map<String, Object> msgArgs;
|
||||
/** 消息插值参数 */
|
||||
protected final transient Map<String, Object> messageArgs;
|
||||
|
||||
/** @param code 代码 */
|
||||
public TimiError(TimiCode code) {
|
||||
this.code = code;
|
||||
this(code, code.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造器
|
||||
*
|
||||
* @param code 代码
|
||||
* @param msg 消息
|
||||
* @param messageCode 消息代码
|
||||
*/
|
||||
public TimiError(TimiCode code, String msg) {
|
||||
super(msg);
|
||||
public TimiError(TimiCode code, String messageCode) {
|
||||
this(code, messageCode, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造器
|
||||
*
|
||||
* @param code 代码
|
||||
* @param messageCode 消息代码
|
||||
* @param messageArgs 消息插值参数
|
||||
*/
|
||||
public TimiError(TimiCode code, String messageCode, Map<String, Object> messageArgs) {
|
||||
super(messageCode);
|
||||
this.code = code;
|
||||
this.messageCode = messageCode;
|
||||
this.messageArgs = messageArgs;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转为通用响应
|
||||
*
|
||||
* @return 响应
|
||||
*/
|
||||
public TimiResponse<?> toResponse() {
|
||||
return new TimiResponse<>(code).msg(getMessage()).msgKey(msgKey, msgArgs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TimiError msg(String msg) {
|
||||
throw new UnsupportedOperationException("unsupported to change message");
|
||||
}
|
||||
|
||||
@Override
|
||||
public TimiError msgKey(String msgKey) {
|
||||
setMsgKey(msgKey);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TimiError msgKey(String msgKey, Map<String, Object> msgArgs) {
|
||||
setMsgKey(msgKey, msgArgs);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMsg(String msg) {
|
||||
throw new UnsupportedOperationException("unsupported to change message");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMsg() {
|
||||
return getMessage();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMsgKey(String msgKey) {
|
||||
this.msgKey = msgKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMsgKey() {
|
||||
return msgKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMsgKey(String msgKey, Map<String, Object> msgArgs) {
|
||||
this.msgKey = msgKey;
|
||||
this.msgArgs = msgArgs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getMsgArgs() {
|
||||
return msgArgs;
|
||||
return new TimiResponse<>(code).messageCode(messageCode, messageArgs);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,159 +2,133 @@ package com.imyeyu.java.bean.timi;
|
||||
|
||||
import com.imyeyu.java.TimiJava;
|
||||
import com.imyeyu.java.bean.CallbackReturn;
|
||||
import com.imyeyu.java.bean.LanguageMsgMapping;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 通用运行时异常,附加通用代码
|
||||
* 通用运行时异常,附加通用代码和消息代码
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2021-05-20 15:18
|
||||
*/
|
||||
public class TimiException extends RuntimeException implements LanguageMsgMapping<TimiException> {
|
||||
@Getter
|
||||
public class TimiException extends RuntimeException {
|
||||
|
||||
/** 代码 */
|
||||
@Getter
|
||||
protected final TimiCode code;
|
||||
|
||||
protected transient String msgKey;
|
||||
/** 消息代码 */
|
||||
protected final String messageCode;
|
||||
|
||||
protected transient Map<String, Object> msgArgs;
|
||||
/** 消息插值参数 */
|
||||
protected final transient Map<String, Object> messageArgs;
|
||||
|
||||
/** @param code 代码 */
|
||||
public TimiException(TimiCode code) {
|
||||
this.code = code;
|
||||
this(code, code.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造器
|
||||
*
|
||||
* @param code 代码
|
||||
* @param msg 消息
|
||||
* @param messageCode 消息代码
|
||||
*/
|
||||
public TimiException(TimiCode code, String msg) {
|
||||
super(msg);
|
||||
this.code = code;
|
||||
public TimiException(TimiCode code, String messageCode) {
|
||||
this(code, messageCode, (Map<String, Object>) null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造器
|
||||
*
|
||||
* @param code 代码
|
||||
* @param msg 消息
|
||||
* @param messageCode 消息代码
|
||||
* @param messageArgs 消息插值参数
|
||||
*/
|
||||
public TimiException(TimiCode code, String msg, Throwable e) {
|
||||
super(msg, e);
|
||||
public TimiException(TimiCode code, String messageCode, Map<String, Object> messageArgs) {
|
||||
super(messageCode);
|
||||
this.code = code;
|
||||
this.messageCode = messageCode;
|
||||
this.messageArgs = messageArgs;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造器
|
||||
*
|
||||
* @param code 代码
|
||||
* @param messageCode 消息代码
|
||||
* @param e 异常
|
||||
*/
|
||||
public TimiException(TimiCode code, String messageCode, Throwable e) {
|
||||
super(messageCode, e);
|
||||
this.code = code;
|
||||
this.messageCode = messageCode;
|
||||
this.messageArgs = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转为通用响应
|
||||
*
|
||||
* @return 响应
|
||||
*/
|
||||
public TimiResponse<?> toResponse() {
|
||||
return new TimiResponse<>(code).msg(getMessage()).msgKey(msgKey, msgArgs);
|
||||
return new TimiResponse<>(code).messageCode(messageCode, messageArgs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TimiException msg(String msg) {
|
||||
throw new UnsupportedOperationException("unsupported to change message");
|
||||
}
|
||||
|
||||
@Override
|
||||
public TimiException msgKey(String msgKey) {
|
||||
setMsgKey(msgKey);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TimiException msgKey(String msgKey, Map<String, Object> msgArgs) {
|
||||
setMsgKey(msgKey, msgArgs);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMsg(String msg) {
|
||||
throw new UnsupportedOperationException("unsupported to change message");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMsg() {
|
||||
return getMessage();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMsgKey(String msgKey) {
|
||||
this.msgKey = msgKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMsgKey(String msgKey, Map<String, Object> msgArgs) {
|
||||
this.msgKey = msgKey;
|
||||
this.msgArgs = msgArgs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMsgKey() {
|
||||
return msgKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getMsgArgs() {
|
||||
return msgArgs;
|
||||
}
|
||||
|
||||
public static <T> T required(T t, String message) {
|
||||
public static <T> T required(T t, String messageCode) {
|
||||
if (TimiJava.isEmpty(t)) {
|
||||
throw new TimiException(TimiCode.ARG_MISS, message);
|
||||
throw new TimiException(TimiCode.ARG_MISS, messageCode);
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
public static <T> T required(T t, String message, String msgKey) {
|
||||
public static <T> T required(T t, String messageCode, Map<String, Object> messageArgs) {
|
||||
if (TimiJava.isEmpty(t)) {
|
||||
throw new TimiException(TimiCode.ARG_MISS, message).msgKey(msgKey);
|
||||
throw new TimiException(TimiCode.ARG_MISS, messageCode, messageArgs);
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
public static <T> void requiredNull(T t, String message) throws TimiException {
|
||||
public static <T> void requiredNull(T t, String messageCode) throws TimiException {
|
||||
if (t != null) {
|
||||
throw new TimiException(TimiCode.ERROR, message);
|
||||
throw new TimiException(TimiCode.ERROR, messageCode);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> void requiredNull(T t, String message, String msgKey) throws TimiException {
|
||||
public static <T> void requiredNull(T t, String messageCode, Map<String, Object> messageArgs) throws TimiException {
|
||||
if (t != null) {
|
||||
throw new TimiException(TimiCode.ERROR, message).msgKey(msgKey);
|
||||
throw new TimiException(TimiCode.ERROR, messageCode, messageArgs);
|
||||
}
|
||||
}
|
||||
|
||||
public static void requiredTrue(boolean bool, String message) throws TimiException {
|
||||
public static void requiredTrue(boolean bool, String messageCode) throws TimiException {
|
||||
if (bool) {
|
||||
return;
|
||||
}
|
||||
throw new TimiException(TimiCode.ERROR, message);
|
||||
throw new TimiException(TimiCode.ERROR, messageCode);
|
||||
}
|
||||
|
||||
public static void requiredTrue(boolean bool, String message, String msgKey) throws TimiException {
|
||||
public static void requiredTrue(boolean bool, String messageCode, Map<String, Object> messageArgs) throws TimiException {
|
||||
if (bool) {
|
||||
return;
|
||||
}
|
||||
throw new TimiException(TimiCode.ERROR, message).msgKey(msgKey);
|
||||
throw new TimiException(TimiCode.ERROR, messageCode, messageArgs);
|
||||
}
|
||||
|
||||
public static void requiredTrue(CallbackReturn<Boolean> callback, String message) throws TimiException {
|
||||
public static void requiredTrue(CallbackReturn<Boolean> callback, String messageCode) throws TimiException {
|
||||
TimiException.required(callback, "not found callback");
|
||||
if (callback.handler()) {
|
||||
return;
|
||||
}
|
||||
throw new TimiException(TimiCode.ERROR, message);
|
||||
throw new TimiException(TimiCode.ERROR, messageCode);
|
||||
}
|
||||
|
||||
public static void requiredTrue(CallbackReturn<Boolean> callback, String message, String msgKey) throws TimiException {
|
||||
public static void requiredTrue(CallbackReturn<Boolean> callback, String messageCode, Map<String, Object> messageArgs) throws TimiException {
|
||||
TimiException.required(callback, "not found callback");
|
||||
if (callback.handler()) {
|
||||
return;
|
||||
}
|
||||
throw new TimiException(TimiCode.ERROR, message).msgKey(msgKey);
|
||||
throw new TimiException(TimiCode.ERROR, messageCode, messageArgs);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.imyeyu.java.bean.timi;
|
||||
|
||||
import com.imyeyu.java.bean.LanguageMsgMapping;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
@@ -11,23 +10,33 @@ import java.util.Map;
|
||||
/**
|
||||
* 通用接口返回对象
|
||||
*
|
||||
* @param <T> 数据类型
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2021-07-01 20:18
|
||||
*/
|
||||
@NoArgsConstructor
|
||||
public class TimiResponse<T> implements Serializable, LanguageMsgMapping<TimiResponse<T>> {
|
||||
public class TimiResponse<T> implements Serializable {
|
||||
|
||||
/** 响应代码 */
|
||||
@Setter
|
||||
@Getter
|
||||
protected Integer code;
|
||||
|
||||
/** 消息 */
|
||||
protected String msg;
|
||||
/** 响应消息 */
|
||||
@Getter
|
||||
@Setter
|
||||
protected String message;
|
||||
|
||||
protected transient String msgKey;
|
||||
/** 响应消息代码 */
|
||||
@Getter
|
||||
@Setter
|
||||
protected String messageCode;
|
||||
|
||||
protected transient Map<String, Object> msgArgs;
|
||||
/** 消息插值参数 */
|
||||
@Getter
|
||||
@Setter
|
||||
protected transient Map<String, Object> messageArgs;
|
||||
|
||||
/** 数据体 */
|
||||
@Setter
|
||||
@@ -58,12 +67,12 @@ public class TimiResponse<T> implements Serializable, LanguageMsgMapping<TimiRes
|
||||
*
|
||||
* @param code 代码
|
||||
* @param data 数据体
|
||||
* @param msg 消息
|
||||
* @param message 响应消息
|
||||
*/
|
||||
public TimiResponse(TimiCode code, T data, String msg) {
|
||||
public TimiResponse(TimiCode code, T data, String message) {
|
||||
this.code = code.getValue();
|
||||
this.data = data;
|
||||
this.msg = msg;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
/** @return true 为成功,code 为 20000 段 */
|
||||
@@ -86,71 +95,41 @@ public class TimiResponse<T> implements Serializable, LanguageMsgMapping<TimiRes
|
||||
if (code == null) {
|
||||
throw new NullPointerException("unknow timi code: " + this.code);
|
||||
}
|
||||
return code.toException(msg);
|
||||
return new TimiException(code, messageCode == null ? message : messageCode, messageArgs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 追加消息(避免和泛型字符串冲突)
|
||||
* 设置响应消息
|
||||
*
|
||||
* @param msg 消息
|
||||
* @param message 响应消息
|
||||
* @return 本对象
|
||||
*/
|
||||
@Override
|
||||
public TimiResponse<T> msg(String msg) {
|
||||
this.msg = msg;
|
||||
public TimiResponse<T> message(String message) {
|
||||
this.message = message;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取消息
|
||||
* 设置响应消息代码
|
||||
*
|
||||
* @return 消息
|
||||
* @param messageCode 响应消息代码
|
||||
* @return 本对象
|
||||
*/
|
||||
@Override
|
||||
public String getMsg() {
|
||||
return msg;
|
||||
public TimiResponse<T> messageCode(String messageCode) {
|
||||
this.messageCode = messageCode;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置消息
|
||||
* 设置响应消息代码和插值参数
|
||||
*
|
||||
* @param msg 消息
|
||||
* @param messageCode 响应消息代码
|
||||
* @param messageArgs 插值参数
|
||||
* @return 本对象
|
||||
*/
|
||||
@Override
|
||||
public void setMsg(String msg) {
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TimiResponse<T> msgKey(String msgKey) {
|
||||
this.msgKey = msgKey;
|
||||
public TimiResponse<T> messageCode(String messageCode, Map<String, Object> messageArgs) {
|
||||
this.messageCode = messageCode;
|
||||
this.messageArgs = messageArgs;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TimiResponse<T> msgKey(String msgKey, Map<String, Object> msgArgs) {
|
||||
setMsgKey(msgKey, msgArgs);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMsgKey() {
|
||||
return msgKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMsgKey(String msgKey) {
|
||||
this.msgKey = msgKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMsgKey(String msgKey, Map<String, Object> msgArgs) {
|
||||
this.msgKey = msgKey;
|
||||
this.msgArgs = msgArgs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getMsgArgs() {
|
||||
return msgArgs;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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