Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
PubTools-tools
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
liuxingyu
PubTools-tools
Commits
4456aee3
Commit
4456aee3
authored
Aug 16, 2023
by
lihao
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' into develop
parents
eb545c9c
c3c78695
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
69 additions
and
81 deletions
+69
-81
TestController.java
.../java/com/mushiny/redisson/controller/TestController.java
+26
-26
RedissonLockUtil.java
...ain/java/com/mushiny/redisson/utils/RedissonLockUtil.java
+18
-30
application.yml
redisson-lock/src/main/resources/application.yml
+1
-1
MwmsRedissonApplicationTests.java
.../src/test/java/pubtools/MwmsRedissonApplicationTests.java
+24
-24
No files found.
redisson-lock/src/main/java/com/mushiny/redisson/controller/TestController.java
View file @
4456aee3
...
...
@@ -4,13 +4,20 @@ import com.mushiny.redisson.enums.LockType;
import
com.mushiny.redisson.utils.RedissonLockUtil
;
import
com.mushiny.redisson.utils.ResultForLock
;
import
jodd.net.HttpStatus
;
import
lombok.extern.slf4j.Slf4j
;
import
org.redisson.api.RLock
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.jdbc.core.JdbcTemplate
;
import
org.springframework.transaction.annotation.Transactional
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
reactor.util.function.Tuple2
;
import
javax.servlet.http.HttpServletResponse
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
import
static
jdk
.
nashorn
.
internal
.
runtime
.
regexp
.
joni
.
Config
.
log
;
/**
* 描述信息
...
...
@@ -19,6 +26,7 @@ import javax.servlet.http.HttpServletResponse;
* @since 2023/8/10 13:03
*/
@RestController
@Slf4j
public
class
TestController
{
@Autowired
RedissonLockUtil
redissonLockUtil
;
...
...
@@ -32,29 +40,26 @@ public class TestController {
@PostMapping
(
"api/reentrant"
)
public
ResultForLock
<
String
>
reentrant
(
HttpServletResponse
response
)
{
SimpleDateFormat
dateFormat
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
String
threadId
=
Thread
.
currentThread
().
getName
()
+
Thread
.
currentThread
().
getId
();
redissonLockUtil
.
getLockByKey
(
"265213225444411"
,
LockType
.
REENTRANT_LOCK
.
value
());
Boolean
getLock
=
redissonLockUtil
.
tryLock
(
50000L
,
"265213225444411"
,
LockType
.
REENTRANT_LOCK
.
value
());
if
(
getLock
)
{
System
.
out
.
println
(
threadId
+
"拿锁成功"
);
log
.
info
(
"enter"
+
threadId
+
dateFormat
.
format
(
new
Date
()));
System
.
out
.
println
(
threadId
+
dateFormat
.
format
(
new
Date
()));
Tuple2
<
RLock
,
Boolean
>
rlocks
=
redissonLockUtil
.
tryLock
(
30000L
,
-
1L
,
"265213225444411"
,
LockType
.
REENTRANT_LOCK
.
value
());
if
(
rlocks
.
getT2
())
{
log
.
info
(
threadId
+
"拿锁成功"
+
dateFormat
.
format
(
new
Date
()));
try
{
count
++;
jdbcTemplate
.
execute
(
"UPDATE descrease a SET a.number = a.number-1"
);
// SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// Date startDate = new Date();
// System.out.println(threadId + "执行调度ping" + dateFormat.format(startDate));
Thread
.
sleep
(
2000
);
// System.out.println("结束时间为" + dateFormat.format(new Date()));
System
.
out
.
println
(
"执行数量为"
+
count
);
Thread
.
sleep
(
31000
);
System
.
out
.
println
(
"成功执行"
+
count
+
"-----------"
+
dateFormat
.
format
(
new
Date
()));
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
new
ResultForLock
().
error
();
}
finally
{
r
edissonLockUtil
.
releaseLock
(
"265213225444411"
,
LockType
.
REENTRANT_LOCK
.
value
()
);
r
locks
.
getT1
().
unlock
(
);
}
}
else
{
errCount
++;
System
.
out
.
println
(
"拿锁失败"
+
errCount
);
response
.
setStatus
(
500
);
return
new
ResultForLock
().
error
();
}
...
...
@@ -63,21 +68,16 @@ public class TestController {
@PostMapping
(
"api/reentrant
WatchDog
"
)
public
ResultForLock
<
String
>
reentrant
WatchDog
(
HttpServletResponse
response
)
{
@PostMapping
(
"api/reentrant
ReleaseTime
"
)
public
ResultForLock
<
String
>
reentrant
ReleaseTime
(
HttpServletResponse
response
)
{
String
threadId
=
Thread
.
currentThread
().
getName
()
+
Thread
.
currentThread
().
getId
();
redissonLockUtil
.
getLockByKey
(
"265213225444411"
,
LockType
.
REENTRANT_LOCK
.
value
());
Boolean
getLock
=
redissonLockUtil
.
tryLock
(
1000L
,-
1L
,
"265213225444411"
,
LockType
.
REENTRANT_LOCK
.
value
());
if
(
getLock
)
{
Tuple2
<
RLock
,
Boolean
>
rlocks
=
redissonLockUtil
.
tryLock
(
30000L
,
1000L
,
"265213225444411"
,
LockType
.
REENTRANT_LOCK
.
value
());
if
(
rlocks
.
getT2
())
{
System
.
out
.
println
(
threadId
+
"拿锁成功"
);
try
{
count
++;
jdbcTemplate
.
execute
(
"UPDATE descrease a SET a.number = a.number-1"
);
// SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// Date startDate = new Date();
// System.out.println(threadId + "执行调度ping" + dateFormat.format(startDate));
Thread
.
sleep
(
2000
);
// System.out.println("结束时间为" + dateFormat.format(new Date()));
System
.
out
.
println
(
"执行数量为"
+
count
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
...
...
@@ -101,14 +101,14 @@ public class TestController {
@PostMapping
(
"api/reentrantByManual"
)
public
ResultForLock
<
String
>
reentrantByManual
()
{
String
threadId
=
Thread
.
currentThread
().
getName
()
+
Thread
.
currentThread
().
getId
();
RLock
rLock
=
redissonLockUtil
.
getLockByKey
(
"265213225444411"
,
LockType
.
REENTRANT_LOCK
.
value
());
RLock
rLock
=
redissonLockUtil
.
getLockByKey
(
"265213225444411"
,
LockType
.
REENTRANT_LOCK
.
value
());
// 支持过期解锁功能,10秒钟以后自动解锁, 无需调用unlock方法手动解锁
//lock.lock(10, TimeUnit.SECONDS);
r
Lock
.
lock
(
);
// * 加锁 锁的有效期默认30秒
r
edissonLockUtil
.
lock
(
rLock
);
// * 加锁 锁的有效期默认30秒
try
{
count
++;
jdbcTemplate
.
execute
(
"UPDATE descrease a SET a.number = a.number-1"
);
Thread
.
sleep
(
2000
);
Thread
.
sleep
(
3
2000
);
System
.
out
.
println
(
"执行数量为"
+
count
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
...
...
@@ -127,8 +127,8 @@ public class TestController {
public
ResultForLock
<
String
>
fair
()
{
String
threadId
=
Thread
.
currentThread
().
getName
()
+
Thread
.
currentThread
().
getId
();
redissonLockUtil
.
getLockByKey
(
"265213225444411"
,
LockType
.
FAIR_LOCK
.
value
());
Boolean
getLock
=
redissonLockUtil
.
tryLock
(
50000L
,
"265213225444411"
,
LockType
.
FAIR
_LOCK
.
value
());
if
(
getLock
)
{
Tuple2
<
RLock
,
Boolean
>
rlocks
=
redissonLockUtil
.
tryLock
(
20000L
,
-
1L
,
"265213225444411"
,
LockType
.
REENTRANT
_LOCK
.
value
());
if
(
rlocks
.
getT2
()
)
{
System
.
out
.
println
(
threadId
+
"拿锁成功"
);
try
{
count
++;
...
...
redisson-lock/src/main/java/com/mushiny/redisson/utils/RedissonLockUtil.java
View file @
4456aee3
...
...
@@ -8,6 +8,8 @@ import org.redisson.api.RedissonClient;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
org.springframework.util.ObjectUtils
;
import
reactor.util.function.Tuple2
;
import
reactor.util.function.Tuples
;
import
java.util.concurrent.TimeUnit
;
...
...
@@ -24,7 +26,7 @@ public class RedissonLockUtil {
public
static
final
Long
WAITTIME
=
12000L
;
public
static
final
Long
RELEASETIME
=
14000
L
;
public
static
final
Long
RELEASETIME
=
-
1
L
;
@Autowired
RedissonClient
redissonClient
;
...
...
@@ -55,30 +57,19 @@ public class RedissonLockUtil {
}
/**
* 非阻塞式,在1s内尝试获取锁
* 获取成功默认锁30s,过期看门狗自动续期
*
* @param key
* @param waitTime
* @param type
* @return
* 手动加锁,锁的有效期默认30秒
* @param rLock
*/
public
Boolean
tryLock
(
Long
waitTime
,
String
key
,
int
type
)
{
boolean
lock
=
false
;
if
(
ObjectUtils
.
isEmpty
(
waitTime
))
{
waitTime
=
WAITTIME
;
}
try
{
RLock
rLock
=
getLockByKey
(
key
,
type
);
lock
=
rLock
.
tryLock
(
waitTime
,
TimeUnit
.
MILLISECONDS
);
if
(!
lock
)
{
log
.
error
(
"分布式锁加锁:{}失败,请重新尝试"
);
// throw new RedissonException(RedissonErrorCode.METHOD_IN_EXECUTION);
public
void
lock
(
RLock
rLock
)
{
rLock
.
lock
();
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
lock
;
/**
* 手动解锁
* @param rLock
*/
public
void
unlock
(
RLock
rLock
)
{
rLock
.
unlock
();
}
...
...
@@ -91,8 +82,9 @@ public class RedissonLockUtil {
* @param type
* @return
*/
public
Boolean
tryLock
(
Long
waitTime
,
Long
releaseTime
,
String
key
,
int
type
)
{
public
Tuple2
<
RLock
,
Boolean
>
tryLock
(
Long
waitTime
,
Long
releaseTime
,
String
key
,
int
type
)
{
boolean
lock
=
false
;
RLock
rLock
=
null
;
if
(
ObjectUtils
.
isEmpty
(
waitTime
))
{
waitTime
=
WAITTIME
;
}
...
...
@@ -100,16 +92,12 @@ public class RedissonLockUtil {
releaseTime
=
RELEASETIME
;
}
try
{
RLock
rLock
=
getLockByKey
(
key
,
type
);
rLock
=
getLockByKey
(
key
,
type
);
lock
=
rLock
.
tryLock
(
waitTime
,
releaseTime
,
TimeUnit
.
MILLISECONDS
);
if
(!
lock
)
{
log
.
error
(
"分布式锁加锁:{}失败,请重新尝试"
);
// throw new RedissonException(RedissonErrorCode.METHOD_IN_EXECUTION);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
lock
;
return
Tuples
.
of
(
rLock
,
lock
)
;
}
...
...
redisson-lock/src/main/resources/application.yml
View file @
4456aee3
...
...
@@ -14,7 +14,7 @@ spring:
driver-class-name
:
com.mysql.cj.jdbc.Driver
url
:
jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2b8
username
:
root
password
:
123456
password
:
wasd11647
...
...
redisson-lock/src/test/java/pubtools/MwmsRedissonApplicationTests.java
View file @
4456aee3
...
...
@@ -11,29 +11,29 @@ import java.util.Date;
@SpringBootTest
class
MwmsRedissonApplicationTests
{
@Autowired
RedissonLockUtil
redissonLockUtil
;
@Test
void
contextLoads
()
{
String
threadId
=
Thread
.
currentThread
().
getName
()
+
Thread
.
currentThread
().
getId
();
redissonLockUtil
.
getLockByKey
(
"2652132254444"
,
LockType
.
READ_LOCK
.
value
());
Boolean
getLock
=
redissonLockUtil
.
tryLock
(
null
,
"2652132254444"
,
LockType
.
REENTRANT_LOCK
.
value
());
if
(
getLock
)
{
System
.
out
.
println
(
threadId
+
"拿锁成功"
);
try
{
SimpleDateFormat
dateFormat
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
Date
startDate
=
new
Date
();
System
.
out
.
println
(
threadId
+
"执行调度ping"
+
dateFormat
.
format
(
startDate
));
Thread
.
sleep
(
10000
);
System
.
out
.
println
(
"结束时间为"
+
dateFormat
.
format
(
new
Date
()));
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
else
{
System
.
out
.
println
(
"拿锁失败"
);
}
}
//
//
@Autowired
//
RedissonLockUtil redissonLockUtil;
//
//
@Test
//
void contextLoads() {
//
String threadId = Thread.currentThread().getName() + Thread.currentThread().getId();
//
redissonLockUtil.getLockByKey("2652132254444", LockType.READ_LOCK.value());
// Boolean getLock = redissonLockUtil.tryLock(null,-1L
, "2652132254444", LockType.REENTRANT_LOCK.value());
//
if (getLock) {
//
System.out.println(threadId + "拿锁成功");
//
try {
//
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//
Date startDate = new Date();
//
System.out.println(threadId + "执行调度ping" + dateFormat.format(startDate));
//
Thread.sleep(10000);
//
System.out.println("结束时间为" + dateFormat.format(new Date()));
//
} catch (Exception e) {
//
e.printStackTrace();
//
}
//
} else {
//
System.out.println("拿锁失败");
//
}
//
}
}
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