Compare commits

...

3 Commits

Author SHA1 Message Date
12bbc62ef0 fix TimiX multilingual 2025-11-07 16:54:56 +08:00
d706574879 add AsyncRetryExecutor 2025-11-07 11:08:19 +08:00
687e5c3bf1 multilingual args use Map<String, Object> 2025-11-06 17:41:54 +08:00
9 changed files with 290 additions and 13 deletions

View File

@ -6,7 +6,7 @@
<groupId>com.imyeyu.java</groupId>
<artifactId>timi-java</artifactId>
<version>0.0.1</version>
<version>0.0.2</version>
<packaging>jar</packaging>
<properties>

View File

@ -1,5 +1,7 @@
package com.imyeyu.java;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Collection;
import java.util.Map;
@ -83,4 +85,11 @@ public interface TimiJava {
}
return null;
}
static String toString(Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
return sw.toString();
}
}

View File

@ -1,5 +1,7 @@
package com.imyeyu.java.bean;
import java.util.Map;
/**
* @author 夜雨
* @since 2024-04-03 11:27
@ -14,5 +16,5 @@ public interface LanguageMapping {
String text(String key, String def);
String textArgs(String key, Object... args);
String textArgs(String key, Map<String, Object> argsMap);
}

View File

@ -1,5 +1,7 @@
package com.imyeyu.java.bean;
import java.util.Map;
/**
* @author 夜雨
* @since 2024-04-01 10:28
@ -10,11 +12,17 @@ public interface LanguageMsgMapping<T> {
T msgKey(String msgKey);
T msgKey(String msgKey, Map<String, Object> msgArgs);
void setMsg(String msg);
String getMsg();
void setMsgKey(String msgKey);
void setMsgKey(String msgKey, Map<String, Object> msgArgs);
String getMsgKey();
Map<String, Object> getMsgArgs();
}

View File

@ -2,6 +2,8 @@ package com.imyeyu.java.bean.timi;
import com.imyeyu.java.bean.LanguageMsgMapping;
import java.util.Map;
/**
* 致命错误
*
@ -13,7 +15,9 @@ public class TimiError extends AssertionError implements LanguageMsgMapping<Timi
/** 代码 */
protected final TimiCode code;
protected String msgKey;
protected transient String msgKey;
protected transient Map<String, Object> msgArgs;
/** @param code 代码 */
public TimiError(TimiCode code) {
@ -41,7 +45,7 @@ public class TimiError extends AssertionError implements LanguageMsgMapping<Timi
}
public TimiResponse<?> toResponse() {
return new TimiResponse<>(code).msg(getMessage()).msgKey(msgKey);
return new TimiResponse<>(code).msg(getMessage()).msgKey(msgKey, msgArgs);
}
@Override
@ -55,6 +59,12 @@ public class TimiError extends AssertionError implements LanguageMsgMapping<Timi
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");
@ -74,4 +84,15 @@ public class TimiError extends AssertionError implements LanguageMsgMapping<Timi
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;
}
}

View File

@ -4,6 +4,8 @@ import com.imyeyu.java.TimiJava;
import com.imyeyu.java.bean.CallbackReturn;
import com.imyeyu.java.bean.LanguageMsgMapping;
import java.util.Map;
/**
* 通用运行时异常,附加通用代码
*
@ -15,7 +17,9 @@ public class TimiException extends RuntimeException implements LanguageMsgMappin
/** 代码 */
protected final TimiCode code;
protected String msgKey;
protected transient String msgKey;
protected transient Map<String, Object> msgArgs;
/** @param code 代码 */
public TimiException(TimiCode code) {
@ -54,7 +58,7 @@ public class TimiException extends RuntimeException implements LanguageMsgMappin
}
public TimiResponse<?> toResponse() {
return new TimiResponse<>(code).msg(getMessage()).msgKey(msgKey);
return new TimiResponse<>(code).msg(getMessage()).msgKey(msgKey, msgArgs);
}
@Override
@ -68,6 +72,12 @@ public class TimiException extends RuntimeException implements LanguageMsgMappin
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");
@ -83,11 +93,22 @@ public class TimiException extends RuntimeException implements LanguageMsgMappin
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) {
if (TimiJava.isEmpty(t)) {
throw new TimiException(TimiCode.ARG_MISS, message);
@ -95,12 +116,25 @@ public class TimiException extends RuntimeException implements LanguageMsgMappin
return t;
}
public static <T> T required(T t, String message, String msgKey) {
if (TimiJava.isEmpty(t)) {
throw new TimiException(TimiCode.ARG_MISS, message).msgKey(msgKey);
}
return t;
}
public static <T> void requiredNull(T t, String message) throws TimiException {
if (t != null) {
throw new TimiException(TimiCode.ERROR, message);
}
}
public static <T> void requiredNull(T t, String message, String msgKey) throws TimiException {
if (t != null) {
throw new TimiException(TimiCode.ERROR, message).msgKey(msgKey);
}
}
public static void requiredTrue(boolean bool, String message) throws TimiException {
if (bool) {
return;
@ -108,11 +142,11 @@ public class TimiException extends RuntimeException implements LanguageMsgMappin
throw new TimiException(TimiCode.ERROR, message);
}
public static void requiredFalse(boolean bool, String message) throws TimiException {
if (!bool) {
public static void requiredTrue(boolean bool, String message, String msgKey) throws TimiException {
if (bool) {
return;
}
throw new TimiException(TimiCode.ERROR, message);
throw new TimiException(TimiCode.ERROR, message).msgKey(msgKey);
}
public static void requiredTrue(CallbackReturn<Boolean> callback, String message) throws TimiException {
@ -123,11 +157,11 @@ public class TimiException extends RuntimeException implements LanguageMsgMappin
throw new TimiException(TimiCode.ERROR, message);
}
public static void requiredFalse(CallbackReturn<Boolean> callback, String message) throws TimiException {
public static void requiredTrue(CallbackReturn<Boolean> callback, String message, String msgKey) throws TimiException {
TimiException.required(callback, "not found callback");
if (!callback.handler()) {
if (callback.handler()) {
return;
}
throw new TimiException(TimiCode.ERROR, message);
throw new TimiException(TimiCode.ERROR, message).msgKey(msgKey);
}
}

View File

@ -3,6 +3,7 @@ package com.imyeyu.java.bean.timi;
import com.imyeyu.java.bean.LanguageMsgMapping;
import java.io.Serializable;
import java.util.Map;
/**
* 通用接口返回对象
@ -18,7 +19,9 @@ public class TimiResponse<T> implements Serializable, LanguageMsgMapping<TimiRes
/** 消息 */
protected String msg;
protected String msgKey;
protected transient String msgKey;
protected transient Map<String, Object> msgArgs;
/** 数据体 */
protected T data;
@ -156,6 +159,12 @@ public class TimiResponse<T> implements Serializable, LanguageMsgMapping<TimiRes
return this;
}
@Override
public TimiResponse<T> msgKey(String msgKey, Map<String, Object> msgArgs) {
setMsgKey(msgKey, msgArgs);
return this;
}
@Override
public String getMsgKey() {
return msgKey;
@ -165,4 +174,15 @@ public class TimiResponse<T> implements Serializable, LanguageMsgMapping<TimiRes
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;
}
}

View File

@ -0,0 +1,148 @@
package com.imyeyu.java.thread;
import com.imyeyu.java.bean.Callback;
import com.imyeyu.java.bean.CallbackArg;
import java.util.concurrent.TimeUnit;
/**
*
* @author 夜雨
* @since 2025-11-06 17:45
*/
public class AsyncRetryExecutor {
/** 默认重试次数,-1 表无限 */
private static final int DEFAULT_MAX_RETRY = -1;
/** 默认重试间隔(毫秒) */
private static final long DEFAULT_RETRY_INTERVAL = -1;
/** 线程名前缀 */
private static final String DEFAULT_THREAD_NAME_PREFIX = "AsyncRetryThread-";
/** true 为守护进程 */
private static final boolean DEFAULT_DAEMON = false;
private final Thread thread;
private volatile boolean isSuccess = false;
private volatile boolean isRunning = false;
private AsyncRetryExecutor(Builder builder) {
thread = new Thread(() -> {
int retryCount = 0;
while (isRunning && !isSuccess && (builder.maxRetry < 0 || retryCount <= builder.maxRetry)) {
try {
builder.callback.handler();
isSuccess = true;
} catch (Exception e) {
retryCount++;
if (!isRunning) {
// 中断
break;
}
if (0 < builder.maxRetry && builder.maxRetry < retryCount) {
// 超过重试次数
builder.onRetryExhausted.handler(e);
break;
}
// 重试
try {
TimeUnit.MILLISECONDS.sleep(builder.retryInterval);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
break;
}
}
}
}, builder.threadNamePrefix + Thread.currentThread().threadId());
thread.setDaemon(builder.daemon);
}
/**
* 构造器
*
* @author 夜雨
* @since 2025-11-06 23:37
*/
public static class Builder {
// 必需参数
private final Callback callback;
// 可选参数(带默认值)
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 Builder(Callback callback) {
this.callback = callback;
}
public Builder maxRetry(int maxRetry) {
this.maxRetry = maxRetry;
return this;
}
public Builder retryInterval(long retryInterval) {
this.retryInterval = retryInterval;
return this;
}
public Builder threadNamePrefix(String prefix) {
this.threadNamePrefix = prefix;
return this;
}
public Builder daemon(boolean daemon) {
this.daemon = daemon;
return this;
}
public Builder onRetryExhausted(CallbackArg<Exception> onRetryExhausted) {
this.onRetryExhausted = onRetryExhausted;
return this;
}
public AsyncRetryExecutor build() {
return new AsyncRetryExecutor(this);
}
}
public static AsyncRetryExecutor create(Callback callback) {
return new Builder(callback).build();
}
public static AsyncRetryExecutor create(Callback callback, int maxRetry) {
return new Builder(callback).maxRetry(maxRetry).build();
}
public static AsyncRetryExecutor create(Callback callback, long interval) {
return new Builder(callback).retryInterval(interval).build();
}
public void start() {
if (!isRunning) {
isRunning = true;
isSuccess = false;
thread.start();
}
}
public void interrupt() {
isRunning = false;
if (thread.isAlive() && !thread.isInterrupted()) {
thread.interrupt();
}
}
public boolean isSuccess() {
return isSuccess;
}
public boolean isRunning() {
return isRunning;
}
}

View File

@ -0,0 +1,35 @@
package test;
import com.imyeyu.java.thread.AsyncRetryExecutor;
import org.junit.jupiter.api.Test;
/**
* @author 夜雨
* @since 2025-11-06 23:59
*/
public class TestThread {
@Test
public void testAsyncRetryExecutor() throws InterruptedException {
Object lock = new Object();
AsyncRetryExecutor executor = AsyncRetryExecutor.create(() -> {
double value = Math.random();
if (.9 < value) {
System.out.println("success " + value);
synchronized (lock) {
lock.notifyAll();
}
} else {
System.out.println("fail " + value);
throw new RuntimeException("fail");
}
});
executor.start();
synchronized (lock) {
lock.wait();
}
assert executor.isSuccess();
}
}