Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
L
long-tern-care-service
Project
Project
Details
Activity
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
hubin
long-tern-care-service
Commits
b630b9af
Commit
b630b9af
authored
Mar 19, 2023
by
zhangwanglin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
定时任务
parent
a27ca066
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
130 additions
and
6 deletions
+130
-6
RedisLock.java
...redis/src/main/java/com/hungraim/ltc/redis/RedisLock.java
+115
-0
SrvTaskScheduledController.java
...ltc/governance/controller/SrvTaskScheduledController.java
+15
-6
No files found.
common/common-redis/src/main/java/com/hungraim/ltc/redis/RedisLock.java
0 → 100644
View file @
b630b9af
package
com
.
hungraim
.
ltc
.
redis
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.data.redis.core.StringRedisTemplate
;
import
org.springframework.stereotype.Component
;
import
javax.annotation.Resource
;
import
java.util.concurrent.TimeUnit
;
/**
* @author zwl
* @ClassName RedisLock
* @description
* @date 2023/3/19 22:04
**/
@Component
public
class
RedisLock
{
@Autowired
private
RedisTemplate
redisTemplate
;
/**
* 查询
*
* @param key 键
* @return 值
*/
public
Object
get
(
Object
key
)
{
return
redisTemplate
.
opsForValue
().
get
(
key
);
}
/**
* 保存
*
* @param key 键
* @param value 值
* @param expire 失效时长
* @param unit 失效时长的单位
*/
public
void
set
(
Object
key
,
Object
value
,
long
expire
,
TimeUnit
unit
)
{
if
(
expire
>
0
)
{
redisTemplate
.
opsForValue
().
set
(
key
,
value
,
expire
,
unit
);
}
}
/**
* 线程等待
*
* @param millis
*/
private
void
sleep
(
long
millis
)
{
try
{
Thread
.
sleep
(
millis
);
}
catch
(
InterruptedException
e
)
{
// do nothing
}
}
/**
* 尝试加锁
*
* @param key
* @param expireMilliseconds 锁超时时长,单位:毫秒
* @param retryTimes 重试次数,每秒重试 1 次
* @return
*/
public
boolean
tryLock
(
Object
key
,
long
expireMilliseconds
,
long
retryTimes
)
{
for
(
long
i
=
1
;
i
<=
retryTimes
;
i
++)
{
if
(
tryLock
(
key
,
expireMilliseconds
))
{
return
true
;
}
else
if
(
i
!=
retryTimes
)
{
sleep
(
1000
);
}
}
return
false
;
}
/**
* 尝试加锁
*
* @param key
* @param expireMilliseconds 锁超时时长,单位:毫秒
* @return
*/
public
boolean
tryLock
(
Object
key
,
long
expireMilliseconds
)
{
String
expireAt
=
String
.
valueOf
(
System
.
currentTimeMillis
()
+
expireMilliseconds
+
1
);
Boolean
acquire
=
redisTemplate
.
opsForValue
().
setIfAbsent
(
key
,
expireAt
,
expireMilliseconds
,
TimeUnit
.
MILLISECONDS
);
if
(
acquire
!=
null
)
{
return
acquire
;
}
else
{
return
false
;
}
}
/**
* 删除锁
*
* @param key
* @return
*/
public
boolean
delLock
(
Object
key
)
{
return
redisTemplate
.
delete
(
key
);
}
/**
* 删除缓存
*
* @param key
* @return
*/
public
boolean
delete
(
Object
key
)
{
return
redisTemplate
.
delete
(
key
);
}
}
governance/src/main/java/com/hungraim/ltc/governance/controller/SrvTaskScheduledController.java
View file @
b630b9af
package
com
.
hungraim
.
ltc
.
governance
.
controller
;
import
com.hungraim.ltc.governance.service.SrvTaskService
;
import
com.hungraim.ltc.redis.RedisLock
;
import
com.hungraim.ltc.util.DateUtils
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
...
@@ -22,17 +23,25 @@ public class SrvTaskScheduledController {
@Autowired
private
SrvTaskService
srvTaskService
;
@Scheduled
(
cron
=
"0 50 20 * * ?"
)
@Autowired
private
RedisLock
redisLock
;
public
static
final
String
JOB_KEY
=
"redis.job.task:srvTask"
;
@Scheduled
(
cron
=
"0 35 23 * * ?"
)
public
void
timeAllocationSrvTask
()
{
log
.
info
(
"timeAllocationSrvTask========start"
);
boolean
flag
=
redisLock
.
tryLock
(
JOB_KEY
,
1000
*
30
);
if
(
flag
){
String
nextDay
=
DateUtils
.
getNextDay
();
log
.
info
(
"timeAllocationSrvTask========start:{}"
,
nextDay
);
srvTaskService
.
timeAllocationSrvTask
(
nextDay
);
log
.
info
(
"timeAllocationSrvTask========end:{}"
);
}
@Scheduled
(
cron
=
"*/5 * * * * ?"
)
public
void
test111
()
{
log
.
info
(
"test111==============================================="
);
}
}
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