Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
yangxiujun
/
paidan_demo
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 63a2f096
authored
Nov 16, 2023
by
Ren Ping
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat:自动任务增加redis锁
1 parent
143a4fef
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
3 additions
and
3 deletions
project-dispatch/src/main/java/com/dituhui/pea/dispatch/common/RedissonUtil.java
project-dispatch/src/main/java/com/dituhui/pea/dispatch/service/impl/SchedulerServiceImpl.java
project-dispatch/src/main/java/com/dituhui/pea/dispatch/common/RedissonUtil.java
View file @
63a2f09
...
@@ -35,14 +35,14 @@ public class RedissonUtil {
...
@@ -35,14 +35,14 @@ public class RedissonUtil {
* @param lockKey
* @param lockKey
* @param operation
* @param operation
*/
*/
public
static
void
lockOperation
(
String
lockKey
,
Runnable
operation
)
{
public
static
void
lockOperation
(
String
lockKey
,
long
timeout
,
Runnable
operation
)
{
RedissonClient
redissonClient
=
SpringUtil
.
getBean
(
RedissonClient
.
class
);
RedissonClient
redissonClient
=
SpringUtil
.
getBean
(
RedissonClient
.
class
);
RLock
lock
=
redissonClient
.
getLock
(
lockKey
);
RLock
lock
=
redissonClient
.
getLock
(
lockKey
);
try
{
try
{
while
(
true
)
{
while
(
true
)
{
//第一个参数:尝试获取锁的最大等待时间,超过这个值,则获取失败
//第一个参数:尝试获取锁的最大等待时间,超过这个值,则获取失败
//第二个参数:锁的持有时间,超过这个时间锁会自动失效
//第二个参数:锁的持有时间,超过这个时间锁会自动失效
boolean
res
=
lock
.
tryLock
(
60
,
60
,
TimeUnit
.
MINUTES
);
boolean
res
=
lock
.
tryLock
(
timeout
,
timeout
,
TimeUnit
.
MINUTES
);
if
(
res
)
{
if
(
res
)
{
log
.
info
(
">>> "
+
Thread
.
currentThread
()
+
" 获取 redis lock"
);
log
.
info
(
">>> "
+
Thread
.
currentThread
()
+
" 获取 redis lock"
);
//成功获得锁,在这里处理业务
//成功获得锁,在这里处理业务
...
...
project-dispatch/src/main/java/com/dituhui/pea/dispatch/service/impl/SchedulerServiceImpl.java
View file @
63a2f09
...
@@ -114,7 +114,7 @@ public class SchedulerServiceImpl implements SchedulerService {
...
@@ -114,7 +114,7 @@ public class SchedulerServiceImpl implements SchedulerService {
for
(
int
i
=
1
;
i
<=
nextDaysLimit
;
i
++)
{
for
(
int
i
=
1
;
i
<=
nextDaysLimit
;
i
++)
{
String
currDay
=
LocalDate
.
now
().
plusDays
(
i
).
format
(
DateTimeFormatter
.
ISO_LOCAL_DATE
);
String
currDay
=
LocalDate
.
now
().
plusDays
(
i
).
format
(
DateTimeFormatter
.
ISO_LOCAL_DATE
);
boolean
finalCutOff
=
cutOff
;
boolean
finalCutOff
=
cutOff
;
RedissonUtil
.
lockOperation
(
AutoDispatchJob
.
TEAM_JOB_PREFIX
+
teamId
,
()
->
{
RedissonUtil
.
lockOperation
(
AutoDispatchJob
.
TEAM_JOB_PREFIX
+
teamId
,
60
,
()
->
{
dispatchRun2OneDay
(
teamId
,
currDay
,
today
,
finalCutOff
);
dispatchRun2OneDay
(
teamId
,
currDay
,
today
,
finalCutOff
);
});
});
}
}
...
...
Write
Preview
Markdown
is supported
Attach a file
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 post a comment