Toggle navigation
Toggle navigation
This project
Loading...
Sign in
yangyoupeng
/
microservice
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
yangyoupeng
2016-11-23 11:38:41 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
b2a53cca4dcc67610f5e3aaec6595d5daa6b8f08
b2a53cca
1 parent
1209c806
添加对excludelock的多线程测试。
excludelock的expire超时的设置调整
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
18 additions
and
14 deletions
src/microservice-parent/microservice-redis/src/main/java/com/zhaoonline/redis/lock/ExcludeLock.java
src/microservice-parent/microservice-redis/src/main/java/com/zhaoonline/redis/token/Token.java
src/microservice-parent/microservice-redis/src/test/java/com/zhaoonline/redis/lock/TestExcludeLock.java
src/microservice-parent/microservice-redis/src/test/java/com/zhaoonline/redis/lock/TestThread2.java
src/microservice-parent/microservice-redis/src/test/java/com/zhaoonline/redis/token/TestMultiSubmitToken.java
src/microservice-parent/microservice-redis/src/main/java/com/zhaoonline/redis/lock/ExcludeLock.java
View file @
b2a53cc
...
...
@@ -89,11 +89,10 @@ public class ExcludeLock {
while
(
timeout
>=
0
)
{
final
Lock
newLock
=
asLock
(
System
.
currentTimeMillis
()
+
lockExpiryInMillis
);
Boolean
result
=
valuOper
.
setIfAbsent
(
newLock
.
toString
());
valuOper
.
expire
(
lockExpiryInMillis
,
TimeUnit
.
MILLISECONDS
);
logger
.
debug
(
"new lock [{}] setIfAbsent result is [{}]"
,
newLock
,
result
);
// final String currentValueStr1 = valuOper.get();
// System.out.println(currentValueStr1);
if
(
result
)
{
Boolean
expireSetResult
=
valuOper
.
expire
(
lockExpiryInMillis
,
TimeUnit
.
MILLISECONDS
);
this
.
lock
=
newLock
;
return
true
;
}
...
...
@@ -108,7 +107,7 @@ public class ExcludeLock {
return
true
;
}
}
}
}
timeout
-=
DEFAULT_ACQUIRY_RESOLUTION_MILLIS
;
TimeUnit
.
MILLISECONDS
.
sleep
(
DEFAULT_ACQUIRY_RESOLUTION_MILLIS
);
logger
.
debug
(
"retry to lock the resouce with lock key:{} after sleep for {} milliseconds"
,
lockKeyPath
,
DEFAULT_ACQUIRY_RESOLUTION_MILLIS
);
...
...
src/microservice-parent/microservice-redis/src/main/java/com/zhaoonline/redis/token/Token.java
View file @
b2a53cc
...
...
@@ -55,14 +55,18 @@ public class Token<T> {
public
boolean
addToken
(
String
key
,
T
token
,
int
expiryInMillis
){
BoundValueOperations
<
String
,
T
>
valuOper
=
redisTemplate
.
boundValueOps
(
buildTokenKeyWithNameSpace
(
key
));
boolean
result
=
valuOper
.
setIfAbsent
(
token
);
valuOper
.
expire
(
expiryInMillis
,
TimeUnit
.
MILLISECONDS
);
if
(
result
){
valuOper
.
expire
(
expiryInMillis
,
TimeUnit
.
MILLISECONDS
);
}
return
result
;
}
public
boolean
addToken
(
String
key
,
T
token
){
BoundValueOperations
<
String
,
T
>
valuOper
=
redisTemplate
.
boundValueOps
(
buildTokenKeyWithNameSpace
(
key
));
boolean
result
=
valuOper
.
setIfAbsent
(
token
);
valuOper
.
expire
(
getTokenExpiryInMillis
(),
TimeUnit
.
MILLISECONDS
);
boolean
result
=
valuOper
.
setIfAbsent
(
token
);
if
(
result
){
valuOper
.
expire
(
getTokenExpiryInMillis
(),
TimeUnit
.
MILLISECONDS
);
}
return
result
;
}
private
String
buildTokenKeyWithNameSpace
(
String
key
){
...
...
@@ -70,8 +74,8 @@ public class Token<T> {
}
public
void
updateToken
(
String
key
,
T
token
){
BoundValueOperations
<
String
,
T
>
valuOper
=
redisTemplate
.
boundValueOps
(
buildTokenKeyWithNameSpace
(
key
));
valuOper
.
expire
(
getTokenExpiryInMillis
(),
TimeUnit
.
MILLISECONDS
);
valuOper
.
set
(
token
);
valuOper
.
expire
(
getTokenExpiryInMillis
(),
TimeUnit
.
MILLISECONDS
);
}
public
T
getToken
(
String
key
){
...
...
src/microservice-parent/microservice-redis/src/test/java/com/zhaoonline/redis/lock/TestExcludeLock.java
View file @
b2a53cc
...
...
@@ -44,10 +44,10 @@ public class TestExcludeLock {
ExcludeLock
lock
=
new
ExcludeLock
(
factory
,
testLock
);
Boolean
lockAcquirereuslt
=
lock
.
acquireLock
();
assertTrue
(
lockAcquirereuslt
);
Thread
.
sleep
(
3000
);
ExcludeLock
lock2
=
new
ExcludeLock
(
factory
,
testLock
);
Boolean
lockAcquirereuslt2
=
lock2
.
acquireLock
();
assert
Fals
e
(
lockAcquirereuslt2
);
assert
Tru
e
(
lockAcquirereuslt2
);
lock
.
getRedisTemplate
().
delete
(
lock
.
getLockKeyPath
());
}
...
...
src/microservice-parent/microservice-redis/src/test/java/com/zhaoonline/redis/lock/TestThread2.java
View file @
b2a53cc
...
...
@@ -26,8 +26,8 @@ public class TestThread2 {
}
@Test
public
void
testRun
(){
ExcludeLock
lock
=
new
ExcludeLock
(
factory
,
testLock
);
int
num
=
10
;
ExcludeLock
lock
=
new
ExcludeLock
(
factory
,
testLock
,
3000
,
30000
);
int
num
=
3
;
CountDownLatch
latch
=
new
CountDownLatch
(
num
);
for
(
int
i
=
0
;
i
<
num
;
i
++){
...
...
@@ -76,7 +76,8 @@ public class TestThread2 {
factory
.
setHostName
(
"192.168.0.188"
);
factory
.
setPort
(
6377
);
factory
.
setUsePool
(
true
);
factory
.
afterPropertiesSet
();
// factory.setDatabase(2);
// factory.afterPropertiesSet();
return
factory
;
}
...
...
src/microservice-parent/microservice-redis/src/test/java/com/zhaoonline/redis/token/TestMultiSubmitToken.java
View file @
b2a53cc
...
...
@@ -48,7 +48,7 @@ public class TestMultiSubmitToken extends RepositoryCommonBase {
assertTrue
(
addResult
);
Thread
.
sleep
(
5
0
);
Thread
.
sleep
(
20
0
);
addResult
=
submitToken
.
addSubmitToken
(
testTokenName
,
2L
);
...
...
Please
register
or
login
to post a comment