Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
schbrain-parent
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
framework
schbrain-parent
Commits
44a3923f
Commit
44a3923f
authored
Sep 25, 2023
by
liaozan
🏀
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add leaseTime option for RedisLockUtils
parent
067fcbff
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
8 deletions
+23
-8
commons/common-util/src/main/java/com/schbrain/common/util/IdWorker.java
...util/src/main/java/com/schbrain/common/util/IdWorker.java
+1
-0
commons/common-util/src/main/java/com/schbrain/common/util/support/lock/RedisLockUtils.java
...com/schbrain/common/util/support/lock/RedisLockUtils.java
+22
-8
No files found.
commons/common-util/src/main/java/com/schbrain/common/util/IdWorker.java
View file @
44a3923f
...
...
@@ -23,6 +23,7 @@ import java.util.concurrent.ThreadLocalRandom;
* 刚开始使用时一般为 18 位,但时间距离起始时间超过一定值后,会变为 19 位。
* <p>
* 消耗完 18 位所需的时间:1 * 10^18 / (3600 * 24 * 365 * 1000 * 2^22) ≈ 7.56 年。
* 消耗完 19 位所需的时间:1 * 10^19 / (3600 * 24 * 365 * 1000 * 2^22) ≈ 75.6 年。
* <p>
* 所以从 2015-01-01 起,大概在 2022-07-22,即时间差超过 7.56 年,就会达到 19 位。
*/
...
...
commons/common-util/src/main/java/com/schbrain/common/util/support/lock/RedisLockUtils.java
View file @
44a3923f
...
...
@@ -22,33 +22,47 @@ public class RedisLockUtils {
private
static
final
String
APPLICATION_NAME
=
ApplicationName
.
get
();
private
static
final
Duration
DEFAULT_WAIT_TIME
=
Duration
.
ofSeconds
(
3
);
/**
* if -1, then lock will be renew automatically by watchdog thread
*/
private
static
final
Duration
DEFAULT_LEASE_TIME
=
Duration
.
ofSeconds
(-
1
);
private
static
RedissonClient
CLIENT
;
public
static
void
executeWithLock
(
String
lockName
,
Runnable
action
)
{
executeWithLock
(
lockName
,
Duration
.
ofSeconds
(
3
),
action
);
executeWithLock
(
lockName
,
DEFAULT_WAIT_TIME
,
action
);
}
public
static
void
executeWithLock
(
String
lockName
,
Duration
waitTime
,
Runnable
action
)
{
executeWithLock
(
lockName
,
waitTime
,
DEFAULT_LEASE_TIME
,
action
);
}
public
static
void
executeWithLock
(
String
lockName
,
Duration
timeout
,
Runnable
action
)
{
executeWithLock
(
lockName
,
timeout
,
()
->
{
public
static
void
executeWithLock
(
String
lockName
,
Duration
waitTime
,
Duration
leaseTime
,
Runnable
action
)
{
executeWithLock
(
lockName
,
waitTime
,
leaseTime
,
()
->
{
action
.
run
();
return
null
;
});
}
public
static
<
T
>
T
executeWithLock
(
String
lockName
,
Callable
<
T
>
action
)
{
return
executeWithLock
(
lockName
,
Duration
.
ofSeconds
(
3
),
action
);
return
executeWithLock
(
lockName
,
DEFAULT_WAIT_TIME
,
action
);
}
public
static
<
T
>
T
executeWithLock
(
String
lockName
,
Duration
waitTime
,
Callable
<
T
>
action
)
{
return
executeWithLock
(
lockName
,
waitTime
,
DEFAULT_LEASE_TIME
,
action
);
}
public
static
<
T
>
T
executeWithLock
(
String
lockName
,
Duration
timeout
,
Callable
<
T
>
action
)
{
public
static
<
T
>
T
executeWithLock
(
String
lockName
,
Duration
waitTime
,
Duration
leaseTime
,
Callable
<
T
>
action
)
{
RLock
lock
=
getClient
().
getLock
(
withPrefix
(
lockName
));
boolean
locked
;
try
{
locked
=
lock
.
tryLock
(
timeout
.
toMillis
(),
TimeUnit
.
MILLISECONDS
);
locked
=
lock
.
tryLock
(
waitTime
.
toMillis
(),
leaseTime
.
toMillis
(),
TimeUnit
.
MILLISECONDS
);
}
catch
(
InterruptedException
e
)
{
throw
new
BaseException
(
"Lock thread has been interrupted"
,
e
);
}
if
(!
locked
)
{
throw
new
BaseException
(
String
.
format
(
"Lock cannot be acquired within %d
seconds"
,
timeout
.
toSecond
s
()));
throw
new
BaseException
(
String
.
format
(
"Lock cannot be acquired within %d
mills"
,
waitTime
.
toMilli
s
()));
}
try
{
...
...
@@ -77,4 +91,4 @@ public class RedisLockUtils {
return
String
.
join
(
StrPool
.
COLON
,
APPLICATION_NAME
,
"lock"
,
lockName
);
}
}
\ No newline at end of file
}
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