Compare commits
24 Commits
c7e83f0671
...
v0.0.2
| Author | SHA1 | Date | |
|---|---|---|---|
| 5612d6a24a | |||
| 381d149cd2 | |||
| db530c1fed | |||
| 3b572491d0 | |||
| 1fe9cf7b77 | |||
| 452d525d83 | |||
| 0d21b154ca | |||
| 8153df0b90 | |||
| acba4c11d9 | |||
| af349ca4d6 | |||
| d21fba0fc1 | |||
| 4b4dcc00b8 | |||
| 46f79f8b6b | |||
| 2a948a3c59 | |||
| 534f8eef81 | |||
| 77c53b422e | |||
| 50c9a416a6 | |||
| 90366671d1 | |||
| f1aede1100 | |||
| 8de5f416f8 | |||
| bde63864b1 | |||
| 12bbc62ef0 | |||
| d706574879 | |||
| 687e5c3bf1 |
110
.gitea/workflows/ci.yml
Normal file
110
.gitea/workflows/ci.yml
Normal file
@ -0,0 +1,110 @@
|
||||
name: CI/CD
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
branches:
|
||||
- master
|
||||
types:
|
||||
- closed
|
||||
|
||||
jobs:
|
||||
build-deploy:
|
||||
runs-on: act_runner_java
|
||||
if: ${{ github.event.pull_request.merged == true }}
|
||||
env:
|
||||
JAVA_HOME: /usr/lib/jvm/java-21-openjdk
|
||||
steps:
|
||||
- name: Checkout code
|
||||
run: |
|
||||
git clone ${{ github.server_url }}/${{ github.repository }}.git .
|
||||
git checkout ${{ github.sha }}
|
||||
- name: Set up environment
|
||||
run: |
|
||||
echo "PR #${{ github.event.number }} merged into master"
|
||||
echo "Source branch: ${{ github.event.pull_request.head.ref }}"
|
||||
echo "Target branch: ${{ github.event.pull_request.base.ref }}"
|
||||
- name: Run tests
|
||||
run: |
|
||||
echo "Running test suite..."
|
||||
- name: Build project
|
||||
run: |
|
||||
mvn -B -DskipTests clean package source:jar javadoc:jar
|
||||
- name: Deploy to Nexus
|
||||
if: success()
|
||||
run: |
|
||||
if [ -z "${{ secrets.NEXUS_USERNAME }}" ] || [ -z "${{ secrets.NEXUS_PASSWORD }}" ]; then
|
||||
echo "Missing secrets.NEXUS_USERNAME or secrets.NEXUS_PASSWORD"
|
||||
exit 1
|
||||
fi
|
||||
mkdir -p ~/.m2
|
||||
cat > ~/.m2/settings.xml <<EOF
|
||||
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
|
||||
<servers>
|
||||
<server>
|
||||
<id>timi-nexus</id>
|
||||
<username>${{ secrets.NEXUS_USERNAME }}</username>
|
||||
<password>${{ secrets.NEXUS_PASSWORD }}</password>
|
||||
</server>
|
||||
</servers>
|
||||
</settings>
|
||||
EOF
|
||||
version=$(mvn -q -DforceStdout help:evaluate -Dexpression=project.version)
|
||||
artifact_id=$(mvn -q -DforceStdout help:evaluate -Dexpression=project.artifactId)
|
||||
main_jar="target/${artifact_id}-${version}.jar"
|
||||
sources_jar="target/${artifact_id}-${version}-sources.jar"
|
||||
javadoc_jar="target/${artifact_id}-${version}-javadoc.jar"
|
||||
if [ ! -f "$main_jar" ] || [ ! -f "$sources_jar" ] || [ ! -f "$javadoc_jar" ]; then
|
||||
echo "Missing build artifacts in target"
|
||||
exit 1
|
||||
fi
|
||||
mvn -B deploy:deploy-file \
|
||||
-Dfile="$main_jar" \
|
||||
-Dsources="$sources_jar" \
|
||||
-Djavadoc="$javadoc_jar" \
|
||||
-DpomFile="./pom.xml" \
|
||||
-Durl="https://nexus.imyeyu.com/repository/maven-releases/" \
|
||||
-DrepositoryId="timi-nexus" \
|
||||
-Dhttps.protocols=TLSv1.2 \
|
||||
-Djdk.tls.client.protocols=TLSv1.2
|
||||
- name: Create release
|
||||
if: ${{ success() && startsWith(github.event.pull_request.title, 'v') }}
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.RUNNER_TOKEN }}
|
||||
GITEA_SERVER_URL: ${{ github.server_url }}
|
||||
GITEA_REPOSITORY: ${{ github.repository }}
|
||||
RELEASE_TAG: ${{ github.event.pull_request.title }}
|
||||
RELEASE_TARGET: ${{ github.sha }}
|
||||
run: |
|
||||
if [ -z "$GITEA_TOKEN" ]; then
|
||||
echo "Missing secrets.RUNNER_TOKEN"
|
||||
exit 1
|
||||
fi
|
||||
api_url="$GITEA_SERVER_URL/api/v1/repos/$GITEA_REPOSITORY/releases"
|
||||
payload=$(cat <<EOF
|
||||
{
|
||||
"tag_name": "$RELEASE_TAG",
|
||||
"name": "$RELEASE_TAG",
|
||||
"target_commitish": "$RELEASE_TARGET",
|
||||
"draft": false,
|
||||
"prerelease": false
|
||||
}
|
||||
EOF
|
||||
)
|
||||
response=$(curl -sS -X POST "$api_url" \
|
||||
-H "Authorization: token $GITEA_TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$payload")
|
||||
release_id=$(echo "$response" | sed -n 's/.*"id":[ ]*\\([0-9][0-9]*\\).*/\\1/p' | head -n 1)
|
||||
if [ -z "$release_id" ]; then
|
||||
echo "Create release failed: $response"
|
||||
exit 1
|
||||
fi
|
||||
for asset_path in target/*.jar; do
|
||||
asset_name=$(basename "$asset_path")
|
||||
curl -sS -X POST "$api_url/$release_id/assets?name=$asset_name" \
|
||||
-H "Authorization: token $GITEA_TOKEN" \
|
||||
-H "Content-Type: application/octet-stream" \
|
||||
--data-binary @"$asset_path"
|
||||
done
|
||||
4
.gitignore
vendored
4
.gitignore
vendored
@ -1,3 +1,7 @@
|
||||
/.claude
|
||||
/AGENTS.md
|
||||
/CLAUDE.md
|
||||
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/target/
|
||||
|
||||
20
pom.xml
20
pom.xml
@ -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>
|
||||
@ -27,29 +27,11 @@
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<version>3.3.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-sources</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>jar-no-fork</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>3.11.2</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-javadocs</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
@ -1,6 +1,9 @@
|
||||
package com.imyeyu.java;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@ -10,6 +13,8 @@ import java.util.Map;
|
||||
*/
|
||||
public interface TimiJava {
|
||||
|
||||
String ZERO_UUID = "00000000-0000-0000-0000-000000000000";
|
||||
|
||||
/**
|
||||
* 通用判空
|
||||
* <pre>
|
||||
@ -64,6 +69,14 @@ public interface TimiJava {
|
||||
return !isEmpty(object);
|
||||
}
|
||||
|
||||
static <T> T defaultIfNull(T obj, T defaultObj) {
|
||||
return firstNotNull(obj, defaultObj);
|
||||
}
|
||||
|
||||
static <T> T defaultIfEmpty(T obj, T defaultObj) {
|
||||
return firstNotEmpty(obj, defaultObj);
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
static <T> T firstNotNull(T... objects) {
|
||||
for (int i = 0; i < objects.length; i++) {
|
||||
@ -83,4 +96,15 @@ 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();
|
||||
}
|
||||
|
||||
static <T> Iterable<T> safeIterable(Iterable<T> iterable) {
|
||||
return defaultIfNull(iterable, Collections::emptyIterator);
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,15 +10,12 @@ public class BasePage {
|
||||
protected int index = 0;
|
||||
|
||||
/** 数据量 */
|
||||
protected int size = 16;
|
||||
|
||||
/** 关键字 */
|
||||
protected String keyword;
|
||||
protected long size = 16;
|
||||
|
||||
public BasePage() {
|
||||
}
|
||||
|
||||
public BasePage(int index, int size) {
|
||||
public BasePage(int index, long size) {
|
||||
this.index = index;
|
||||
this.size = size;
|
||||
}
|
||||
@ -39,19 +36,11 @@ public class BasePage {
|
||||
this.index = index;
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
public long getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public void setSize(int size) {
|
||||
public void setSize(long size) {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public String getKeyword() {
|
||||
return keyword;
|
||||
}
|
||||
|
||||
public void setKeyword(String keyword) {
|
||||
this.keyword = keyword;
|
||||
}
|
||||
}
|
||||
|
||||
@ -18,6 +18,24 @@ public class BasePageResult<T> {
|
||||
|
||||
protected List<T> list;
|
||||
|
||||
/**
|
||||
* 结果是否为空
|
||||
*
|
||||
* @return true 为空
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return list.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* 结果是否非空
|
||||
*
|
||||
* @return true 为非空
|
||||
*/
|
||||
public boolean isNotEmpty() {
|
||||
return !isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取总数据量
|
||||
*
|
||||
|
||||
@ -1,47 +1,134 @@
|
||||
package com.imyeyu.java.bean;
|
||||
|
||||
import com.imyeyu.java.ref.Ref;
|
||||
|
||||
/**
|
||||
* 多语言
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2022-02-23 11:25
|
||||
*/
|
||||
public enum Language {
|
||||
|
||||
/** 英语 */
|
||||
en_US("English"),
|
||||
|
||||
/** 简中 */
|
||||
zh_CN("简体中文"),
|
||||
|
||||
/** 繁中 */
|
||||
zh_TW("繁体中文"),
|
||||
|
||||
/** 日语 */
|
||||
ja_JP("日本語"),
|
||||
|
||||
/** 韩语 */
|
||||
ko_KR("한국인"),
|
||||
|
||||
/** 俄语 */
|
||||
ru_RU("русский"),
|
||||
|
||||
/** 德语 */
|
||||
de_DE("Deutsch");
|
||||
|
||||
/** 名称 */
|
||||
final String name;
|
||||
|
||||
Language(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public class Language {
|
||||
|
||||
/**
|
||||
* 获取语言名称
|
||||
*
|
||||
* @return 语言名称
|
||||
* @author 夜雨
|
||||
* @since 2025-12-05 14:31
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
public enum Enum {
|
||||
|
||||
/** 英语 */
|
||||
en_US,
|
||||
|
||||
/** 简中 */
|
||||
zh_CN,
|
||||
|
||||
/** 繁中 */
|
||||
zh_TW,
|
||||
|
||||
/** 日语 */
|
||||
ja_JP,
|
||||
|
||||
/** 韩语 */
|
||||
ko_KR,
|
||||
|
||||
/** 俄语 */
|
||||
ru_RU,
|
||||
|
||||
/** 德语 */
|
||||
de_DE;
|
||||
}
|
||||
|
||||
protected String key;
|
||||
|
||||
protected String zhCN;
|
||||
|
||||
protected String zhTW;
|
||||
|
||||
protected String enUS;
|
||||
|
||||
protected String ruRU;
|
||||
|
||||
protected String koKR;
|
||||
|
||||
protected String jaJP;
|
||||
|
||||
protected String deDE;
|
||||
|
||||
/**
|
||||
* 获取指定语言值
|
||||
*
|
||||
* @param language 指定语言
|
||||
* @return 值
|
||||
*/
|
||||
public String getValue(Enum language) {
|
||||
try {
|
||||
return Ref.getFieldValue(this, language.toString().replace("_", ""), String.class);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public String getZhCN() {
|
||||
return zhCN;
|
||||
}
|
||||
|
||||
public void setZhCN(String zhCN) {
|
||||
this.zhCN = zhCN;
|
||||
}
|
||||
|
||||
public String getZhTW() {
|
||||
return zhTW;
|
||||
}
|
||||
|
||||
public void setZhTW(String zhTW) {
|
||||
this.zhTW = zhTW;
|
||||
}
|
||||
|
||||
public String getEnUS() {
|
||||
return enUS;
|
||||
}
|
||||
|
||||
public void setEnUS(String enUS) {
|
||||
this.enUS = enUS;
|
||||
}
|
||||
|
||||
public String getRuRU() {
|
||||
return ruRU;
|
||||
}
|
||||
|
||||
public void setRuRU(String ruRU) {
|
||||
this.ruRU = ruRU;
|
||||
}
|
||||
|
||||
public String getKoKR() {
|
||||
return koKR;
|
||||
}
|
||||
|
||||
public void setKoKR(String koKR) {
|
||||
this.koKR = koKR;
|
||||
}
|
||||
|
||||
public String getJaJP() {
|
||||
return jaJP;
|
||||
}
|
||||
|
||||
public void setJaJP(String jaJP) {
|
||||
this.jaJP = jaJP;
|
||||
}
|
||||
|
||||
public String getDeDE() {
|
||||
return deDE;
|
||||
}
|
||||
|
||||
public void setDeDE(String deDE) {
|
||||
this.deDE = deDE;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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();
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
148
src/main/java/com/imyeyu/java/thread/AsyncRetryExecutor.java
Normal file
148
src/main/java/com/imyeyu/java/thread/AsyncRetryExecutor.java
Normal 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;
|
||||
}
|
||||
}
|
||||
35
src/test/java/test/TestThread.java
Normal file
35
src/test/java/test/TestThread.java
Normal 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user