Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
YZG
yzg-util
Commits
a9074143
Commit
a9074143
authored
May 07, 2022
by
yanzg
Browse files
Options
Browse Files
Download
Plain Diff
Merge remote-tracking branch 'origin/master'
parents
90538b7f
787abdb3
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
229 additions
and
45 deletions
+229
-45
YzgErrorData.java
...base/src/main/java/com/yanzuoguang/util/YzgErrorData.java
+29
-0
CodeTargetException.java
...a/com/yanzuoguang/util/exception/CodeTargetException.java
+158
-0
ExceptionHelper.java
.../java/com/yanzuoguang/util/exception/ExceptionHelper.java
+4
-2
DbExecuteImpl.java
.../src/main/java/com/yanzuoguang/db/impl/DbExecuteImpl.java
+0
-1
CacheLock.java
...-redis/src/main/java/com.yanzuoguang.redis/CacheLock.java
+38
-42
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/YzgErrorData.java
View file @
a9074143
package
com
.
yanzuoguang
.
util
;
package
com
.
yanzuoguang
.
util
;
import
com.yanzuoguang.util.exception.CodeException
;
import
com.yanzuoguang.util.exception.CodeException
;
import
com.yanzuoguang.util.exception.CodeTargetException
;
import
com.yanzuoguang.util.exception.RuntimeCodeException
;
import
com.yanzuoguang.util.exception.RuntimeCodeException
;
import
com.yanzuoguang.util.helper.JsonHelper
;
import
com.yanzuoguang.util.helper.JsonHelper
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
com.yanzuoguang.util.helper.StringHelper
;
...
@@ -63,6 +64,34 @@ public class YzgErrorData {
...
@@ -63,6 +64,34 @@ public class YzgErrorData {
}
}
}
}
/**
* 抛出异常
*
* @param code 错误码
* @param target 携带对象
* @param args 异常数据
*/
public
CodeTargetException
getCodeTargetException
(
String
code
,
Object
target
,
Object
...
args
)
{
return
getCodeTargetException
(
null
,
code
,
target
,
args
);
}
/**
* 抛出异常
*
* @param ex 上级错误
* @param code 错误码
* @param target 携带对象
* @param args 异常数据
*/
public
CodeTargetException
getCodeTargetException
(
Throwable
ex
,
String
code
,
Object
target
,
Object
...
args
)
{
String
message
=
getMessage
(
ErrorCode
,
code
,
args
);
if
(
ex
!=
null
)
{
return
new
CodeTargetException
(
TAG
+
code
,
message
,
target
,
ex
);
}
else
{
return
new
CodeTargetException
(
TAG
+
code
,
message
,
target
);
}
}
/**
/**
* 抛出异常
* 抛出异常
*
*
...
...
yzg-util-base/src/main/java/com/yanzuoguang/util/exception/CodeTargetException.java
0 → 100644
View file @
a9074143
package
com
.
yanzuoguang
.
util
.
exception
;
/**
* 途比达异常信息
*
* @author 颜佐光
*/
public
class
CodeTargetException
extends
CodeException
{
/**
* Constructs a new runtime exception with the specified detail message and
* cause. <p>Note that the detail message associated with
* {@code cause} is <i>not</i> automatically incorporated in
* this runtime exception's detail message.
*
* @param code the detail code (which is saved for later retrieval
* by the {@link #getCode()} method).
* @param message the detail message (which is saved for later retrieval
* by the {@link #getMessage()} method).
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
* @since 1.4
*/
public
CodeTargetException
(
String
code
,
String
message
,
Throwable
cause
)
{
super
(
code
,
message
,
cause
);
}
/**
* 构造函数
* throw new CodeException("01","该订单已过期",order);
* throw new CodeException("02","该订单未到使用时间",order);
*
* @param message 错误消息
* @param target 错误数据源,如订单数据
*/
public
CodeTargetException
(
String
message
,
Object
target
)
{
super
(
message
,
target
);
}
/**
* 构造函数
* throw new CodeException("01","该订单已过期",order);
* throw new CodeException("02","该订单未到使用时间",order);
*
* @param message 错误消息
* @param target 错误数据源,如订单数据
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
*/
public
CodeTargetException
(
String
message
,
Object
target
,
Throwable
cause
)
{
super
(
message
,
target
,
cause
);
}
/**
* 构造函数
* throw new CodeException("01","该订单已过期",order);
* throw new CodeException("02","该订单未到使用时间",order);
*
* @param code 错误码
* @param message 错误消息
* @param target 错误数据源,如订单数据
*/
public
CodeTargetException
(
String
code
,
String
message
,
Object
target
)
{
super
(
code
,
message
,
target
);
}
/**
* 构造函数
* throw new CodeException("01","该订单已过期",order);
* throw new CodeException("02","该订单未到使用时间",order);
*
* @param code 错误码
* @param message 错误消息
* @param target 错误数据源,如订单数据
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
*/
public
CodeTargetException
(
String
code
,
String
message
,
Object
target
,
Throwable
cause
)
{
super
(
code
,
message
,
target
,
cause
);
}
/**
* Constructs a new runtime exception with the specified detail message and
* cause. <p>Note that the detail message associated with
* {@code cause} is <i>not</i> automatically incorporated in
* this runtime exception's detail message.
*
* @param message the detail message (which is saved for later retrieval
* by the {@link #getMessage()} method).
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
* @since 1.4
*/
public
CodeTargetException
(
String
message
,
Throwable
cause
)
{
super
(
message
,
cause
);
}
/**
* Constructs a new runtime exception with {@code null} as its
* detail message. The cause is not initialized, and may subsequently be
* initialized by a call to {@link #initCause}.
*/
public
CodeTargetException
()
{
super
();
}
/**
* Constructs a new runtime exception with the specified detail message.
* The cause is not initialized, and may subsequently be initialized by a
* call to {@link #initCause}.
*
* @param code the detail code (which is saved for later retrieval
* by the {@link #getCode()} method).
* @param message the detail message. The detail message is saved for
* later retrieval by the {@link #getMessage()} method.
*/
public
CodeTargetException
(
String
code
,
String
message
)
{
super
(
code
,
message
);
}
/**
* Constructs a new runtime exception with the specified detail message.
* The cause is not initialized, and may subsequently be initialized by a
* call to {@link #initCause}.
*
* @param message the detail message. The detail message is saved for
* later retrieval by the {@link #getMessage()} method.
*/
public
CodeTargetException
(
String
message
)
{
super
(
message
);
}
/**
* Constructs a new runtime exception with the specified detail message and
* cause. <p>Note that the detail message associated with
* {@code cause} is <i>not</i> automatically incorporated in
* this runtime exception's detail message.
*
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
* @since 1.4
*/
public
CodeTargetException
(
Throwable
cause
)
{
super
(
cause
);
}
}
yzg-util-base/src/main/java/com/yanzuoguang/util/exception/ExceptionHelper.java
View file @
a9074143
...
@@ -2,7 +2,6 @@ package com.yanzuoguang.util.exception;
...
@@ -2,7 +2,6 @@ package com.yanzuoguang.util.exception;
import
com.yanzuoguang.util.contants.ResultConstants
;
import
com.yanzuoguang.util.contants.ResultConstants
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
com.yanzuoguang.util.log.Log
;
import
com.yanzuoguang.util.vo.ResponseResult
;
import
com.yanzuoguang.util.vo.ResponseResult
;
/**
/**
...
@@ -65,7 +64,10 @@ public class ExceptionHelper {
...
@@ -65,7 +64,10 @@ public class ExceptionHelper {
* @return 返回的数据
* @return 返回的数据
*/
*/
public
static
ResponseResult
getError
(
Exception
e
,
boolean
isFull
)
{
public
static
ResponseResult
getError
(
Exception
e
,
boolean
isFull
)
{
if
(
e
instanceof
RuntimeCodeException
)
{
if
(
e
instanceof
CodeTargetException
)
{
CodeTargetException
code
=
(
CodeTargetException
)
e
;
return
getError
(
code
.
getCode
(),
code
.
getMessage
(),
code
.
getTarget
(),
true
);
}
else
if
(
e
instanceof
RuntimeCodeException
)
{
RuntimeCodeException
code
=
(
RuntimeCodeException
)
e
;
RuntimeCodeException
code
=
(
RuntimeCodeException
)
e
;
return
getError
(
code
.
getCode
(),
code
.
getMessage
(),
code
.
getTarget
(),
isFull
);
return
getError
(
code
.
getCode
(),
code
.
getMessage
(),
code
.
getTarget
(),
isFull
);
}
else
{
}
else
{
...
...
yzg-util-db/src/main/java/com/yanzuoguang/db/impl/DbExecuteImpl.java
View file @
a9074143
...
@@ -71,7 +71,6 @@ public class DbExecuteImpl implements DbExecute {
...
@@ -71,7 +71,6 @@ public class DbExecuteImpl implements DbExecute {
* @param sql 需要查询的SQL语句
* @param sql 需要查询的SQL语句
* @param paras 查询语句的参数
* @param paras 查询语句的参数
* @param <T> 返回的集合的类型
* @param <T> 返回的集合的类型
* @return 集合
*/
*/
@Override
@Override
public
<
T
extends
Object
>
void
query
(
Class
targetClass
,
Class
<
T
>
cls
,
DbRow
<
T
>
rowHandle
,
String
sqlName
,
String
sql
,
Object
...
paras
)
{
public
<
T
extends
Object
>
void
query
(
Class
targetClass
,
Class
<
T
>
cls
,
DbRow
<
T
>
rowHandle
,
String
sqlName
,
String
sql
,
Object
...
paras
)
{
...
...
yzg-util-redis/src/main/java/com.yanzuoguang.redis/CacheLock.java
View file @
a9074143
...
@@ -10,7 +10,7 @@ import java.util.concurrent.TimeUnit;
...
@@ -10,7 +10,7 @@ import java.util.concurrent.TimeUnit;
*
*
* @author 颜佐光
* @author 颜佐光
*/
*/
public
class
CacheLock
implements
Runnable
{
public
class
CacheLock
<
T
,
M
>
implements
Runnable
{
/**
/**
* 是否执行标记
* 是否执行标记
...
@@ -20,7 +20,7 @@ public class CacheLock implements Runnable {
...
@@ -20,7 +20,7 @@ public class CacheLock implements Runnable {
/**
/**
* 缓存对象
* 缓存对象
*/
*/
private
final
Cache
cache
;
private
final
Cache
<
T
,
M
>
cache
;
/**
/**
* Redis 锁定时间(豪秒)
* Redis 锁定时间(豪秒)
...
@@ -34,7 +34,7 @@ public class CacheLock implements Runnable {
...
@@ -34,7 +34,7 @@ public class CacheLock implements Runnable {
/**
/**
* 关键字
* 关键字
*/
*/
private
final
String
key
;
private
final
T
key
;
/**
/**
* 执行函数
* 执行函数
*/
*/
...
@@ -52,39 +52,39 @@ public class CacheLock implements Runnable {
...
@@ -52,39 +52,39 @@ public class CacheLock implements Runnable {
/**
/**
* 构造函数
* 构造函数
*
*
* @param cache
* @param cache
缓存对象
* @param lockTime
* @param lockTime
锁定时间
* @param waitUnitTime
* @param waitUnitTime
等待单位
* @param key
* @param key
关键字
*/
*/
public
CacheLock
(
Cache
cache
,
int
lockTime
,
int
waitUnitTime
,
String
key
)
{
public
CacheLock
(
Cache
<
T
,
M
>
cache
,
int
lockTime
,
int
waitUnitTime
,
T
key
)
{
this
(
cache
,
lockTime
,
waitUnitTime
,
key
,
null
,
null
);
this
(
cache
,
lockTime
,
waitUnitTime
,
key
,
null
,
null
);
}
}
/**
/**
* 构造函数
* 构造函数
*
*
* @param cache
* @param cache
缓存对象
* @param lockTime
* @param lockTime
锁定时间
* @param waitUnitTime
* @param waitUnitTime
等待单位
* @param key
* @param key
关键字
* @param func
* @param func
执行函数
*/
*/
public
CacheLock
(
Cache
cache
,
int
lockTime
,
int
waitUnitTime
,
String
key
,
Runnable
func
)
{
public
CacheLock
(
Cache
<
T
,
M
>
cache
,
int
lockTime
,
int
waitUnitTime
,
T
key
,
Runnable
func
)
{
this
(
cache
,
lockTime
,
waitUnitTime
,
key
,
null
,
func
);
this
(
cache
,
lockTime
,
waitUnitTime
,
key
,
null
,
func
);
}
}
/**
/**
* 构造函数
* 构造函数
*
*
* @param cache
* @param cache
缓存对象
* @param lockTime
* @param lockTime
锁定时间
* @param waitUnitTime
* @param waitUnitTime
等待单位
* @param key
* @param key
关键字
* @param funcWait
等待执行
* @param funcWait
等待期间执行的函数
* @param func
* @param func
执行函数
*/
*/
public
CacheLock
(
Cache
cache
,
int
lockTime
,
int
waitUnitTime
,
String
key
,
Runnable
funcWait
,
Runnable
func
)
{
public
CacheLock
(
Cache
<
T
,
M
>
cache
,
int
lockTime
,
int
waitUnitTime
,
T
key
,
Runnable
funcWait
,
Runnable
func
)
{
this
.
cache
=
cache
;
this
.
cache
=
cache
;
this
.
lockTime
=
lockTime
;
this
.
lockTime
=
lockTime
;
this
.
waitUnitTime
=
waitUnitTime
;
this
.
waitUnitTime
=
waitUnitTime
;
...
@@ -96,7 +96,7 @@ public class CacheLock implements Runnable {
...
@@ -96,7 +96,7 @@ public class CacheLock implements Runnable {
/**
/**
* 等待次数
* 等待次数
*
*
* @return
* @return
等待次数
*/
*/
public
int
getWaitCount
()
{
public
int
getWaitCount
()
{
return
waitCount
;
return
waitCount
;
...
@@ -104,6 +104,8 @@ public class CacheLock implements Runnable {
...
@@ -104,6 +104,8 @@ public class CacheLock implements Runnable {
/**
/**
* 开始执行,每个关键字会等待其他关键字执行完成后执行
* 开始执行,每个关键字会等待其他关键字执行完成后执行
*
* @param func 运行函数
*/
*/
public
void
run
(
Runnable
func
)
{
public
void
run
(
Runnable
func
)
{
this
.
func
=
func
;
this
.
func
=
func
;
...
@@ -119,15 +121,9 @@ public class CacheLock implements Runnable {
...
@@ -119,15 +121,9 @@ public class CacheLock implements Runnable {
return
;
return
;
}
}
// 需要运行的函数
// 需要运行的函数
Runnable
runnable
=
new
Runnable
()
{
@Override
public
void
run
()
{
funcRun
();
}
};
do
{
do
{
// 开启唯一性锁,防止多人运行同一关键字的函数
// 开启唯一性锁,防止多人运行同一关键字的函数
cache
.
tryLockAndRun
(
key
,
lockTime
,
TimeUnit
.
SECONDS
,
runnable
);
cache
.
tryLockAndRun
(
key
,
lockTime
,
TimeUnit
.
SECONDS
,
this
::
funcRun
);
// 假如没有运行,则等待50毫秒后继续运行
// 假如没有运行,则等待50毫秒后继续运行
if
(!
runFlag
)
{
if
(!
runFlag
)
{
this
.
waitCount
++;
this
.
waitCount
++;
...
@@ -152,26 +148,26 @@ public class CacheLock implements Runnable {
...
@@ -152,26 +148,26 @@ public class CacheLock implements Runnable {
/**
/**
* 开始执行,每个关键字会等待其他关键字执行完成后执行
* 开始执行,每个关键字会等待其他关键字执行完成后执行
*
*
* @param cache
* @param cache
缓存对象
* @param lockTime
* @param lockTime
锁定时间
* @param waitUnitTime
* @param waitUnitTime
等待单位
* @param key
* @param key
关键字
* @param func
* @param func
执行函数
*/
*/
public
static
void
run
(
Cache
cache
,
int
lockTime
,
int
waitUnitTime
,
String
key
,
Runnable
func
)
{
public
static
<
T
,
M
>
void
run
(
Cache
<
T
,
M
>
cache
,
int
lockTime
,
int
waitUnitTime
,
T
key
,
Runnable
func
)
{
run
(
cache
,
lockTime
,
waitUnitTime
,
key
,
null
,
func
);
run
(
cache
,
lockTime
,
waitUnitTime
,
key
,
null
,
func
);
}
}
/**
/**
* 开始执行,每个关键字会等待其他关键字执行完成后执行
* 开始执行,每个关键字会等待其他关键字执行完成后执行
*
*
* @param cache
* @param cache
缓存对象
* @param lockTime
* @param lockTime
锁定时间
* @param waitUnitTime
* @param waitUnitTime
等待单位
* @param key
* @param key
关键字
* @param func
* @param func
执行函数
*/
*/
public
static
void
run
(
Cache
cache
,
int
lockTime
,
int
waitUnitTime
,
String
key
,
Runnable
funcWait
,
Runnable
func
)
{
public
static
<
T
,
M
>
void
run
(
Cache
<
T
,
M
>
cache
,
int
lockTime
,
int
waitUnitTime
,
T
key
,
Runnable
funcWait
,
Runnable
func
)
{
CacheLock
lock
=
new
CacheLock
(
cache
,
lockTime
,
waitUnitTime
,
key
,
funcWait
,
func
);
CacheLock
lock
=
new
CacheLock
(
cache
,
lockTime
,
waitUnitTime
,
key
,
funcWait
,
func
);
lock
.
run
();
lock
.
run
();
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment