Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
王仕雄
/
wangsx-test
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 40230f12
authored
Dec 16, 2019
by
王仕雄
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
redis分布式锁参照
1 parent
589393c2
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
191 additions
and
0 deletions
khdo/src/main/java/com/bootdo/BootdoApplication.java
khdo/src/main/java/com/bootdo/common/utils/CommonRedisHelper.java
khdo/src/main/java/com/bootdo/common/utils/TestJoba.java
khdo/src/main/java/com/bootdo/BootdoApplication.java
View file @
40230f1
...
...
@@ -36,6 +36,7 @@ public class BootdoApplication {
System
.
out
.
println
(
"111"
);
System
.
out
.
println
(
"000411"
);
System
.
out
.
println
(
"0002"
);
System
.
out
.
println
(
"0001"
);
return
new
Object
();
}
}
khdo/src/main/java/com/bootdo/common/utils/CommonRedisHelper.java
0 → 100644
View file @
40230f1
package
com
.
bootdo
.
common
.
utils
;
import
java.util.Objects
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.data.redis.core.RedisCallback
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.stereotype.Component
;
@Component
public
class
CommonRedisHelper
{
public
static
final
String
LOCK_PREFIX
=
"redis_lock_saas_cso_job"
;
public
static
final
int
LOCK_EXPIRE
=
1000
*
30
;
// ms 此处默认设置为30秒
@SuppressWarnings
(
"rawtypes"
)
@Autowired
private
RedisTemplate
redisTemplate
;
/**
* 分布式锁
* 如果锁持有者,在超时时间之后都没有释放锁(死锁),那么后续拿锁一样会成功
* @param key 键
* @param lockExpire 超时时间(单位毫秒)
* @return
*/
@SuppressWarnings
({
"unchecked"
,
"rawtypes"
})
public
boolean
lock
(
String
key
,
int
lockExpire
){
String
lock
=
LOCK_PREFIX
+
key
;
// 利用lambda表达式
return
(
Boolean
)
redisTemplate
.
execute
((
RedisCallback
)
connection
->
{
long
expireAt
=
System
.
currentTimeMillis
()
+
lockExpire
+
1
;
Boolean
acquire
=
connection
.
setNX
(
lock
.
getBytes
(),
String
.
valueOf
(
expireAt
).
getBytes
());
if
(
acquire
)
{
return
true
;
}
else
{
byte
[]
value
=
connection
.
get
(
lock
.
getBytes
());
if
(
Objects
.
nonNull
(
value
)
&&
value
.
length
>
0
)
{
long
expireTime
=
Long
.
parseLong
(
new
String
(
value
));
if
(
expireTime
<
System
.
currentTimeMillis
())
{
// 如果锁已经过期
byte
[]
oldValue
=
connection
.
getSet
(
lock
.
getBytes
(),
String
.
valueOf
(
System
.
currentTimeMillis
()
+
lockExpire
+
1
).
getBytes
());
// 防止死锁
return
Long
.
parseLong
(
new
String
(
oldValue
))
<
System
.
currentTimeMillis
();
}
}
}
return
false
;
});
}
/**
* 删除锁
*
* @param key
*/
@SuppressWarnings
(
"unchecked"
)
public
void
delete
(
String
key
)
{
redisTemplate
.
delete
(
LOCK_PREFIX
+
key
);
}
// public static void main(String[] args) throws InterruptedException {
//
// System.out.println(new String(String.valueOf(System.currentTimeMillis() + LOCK_EXPIRE + 1).getBytes()));
// Thread.sleep(3000);
// byte [] ss = String.valueOf(System.currentTimeMillis() + LOCK_EXPIRE + 1).getBytes();
// System.out.println(ss);
// String value =new String(ss);
// System.out.println(value);
// System.out.println(Long.parseLong(value));
// }
}
khdo/src/main/java/com/bootdo/common/utils/TestJoba.java
0 → 100644
View file @
40230f1
package
com
.
bootdo
.
common
.
utils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.scheduling.annotation.Scheduled
;
import
org.springframework.stereotype.Component
;
/**
*
* @ClassName: TestJoba
* @Description: redis分布式锁的测试类,可保留参照
* @author wangsx
* @date 2019年12月4日
*
*/
@Component
public
class
TestJoba
{
@Autowired
private
CommonRedisHelper
commonRedisHelper
;
public
final
static
String
timer_key
=
"xsbftimer"
;
@Scheduled
(
cron
=
"1 02 15 * * ?"
)
public
void
xsbfTimeTaskOfEnableDt
()
throws
Exception
{
try
{
if
(
commonRedisHelper
.
lock
(
timer_key
,
1000
*
30
))
{
System
.
out
.
println
(
"job-1 开始工作了,拿锁成功..."
);
Thread
.
sleep
(
3000
);
//休眠3秒
}
else
{
System
.
out
.
println
(
"job-1 拿锁失败..."
);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
commonRedisHelper
.
delete
(
timer_key
);
System
.
out
.
println
(
"job-1 工作结束了,释放锁..."
);
}
}
@Scheduled
(
cron
=
"52 02 15 * * ?"
)
public
void
xsbfTimeTaskOfEnableDt2
()
throws
Exception
{
try
{
if
(
commonRedisHelper
.
lock
(
timer_key
,
1000
*
30
))
{
System
.
out
.
println
(
"job-2 开始工作了,拿锁成功..."
);
Thread
.
sleep
(
3000
);
//休眠3秒
}
else
{
System
.
out
.
println
(
"job-2 拿锁失败..."
);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
commonRedisHelper
.
delete
(
timer_key
);
System
.
out
.
println
(
"job-2 工作结束了,释放锁..."
);
}
}
@Scheduled
(
cron
=
"3 02 15 * * ?"
)
public
void
xsbfTimeTaskOfEnableDt3
()
throws
Exception
{
try
{
if
(
commonRedisHelper
.
lock
(
timer_key
,
1000
*
30
))
{
System
.
out
.
println
(
"job-3 开始工作了,拿锁成功..."
);
Thread
.
sleep
(
3000
);
//休眠3秒
}
else
{
System
.
out
.
println
(
"job-3 拿锁失败..."
);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
commonRedisHelper
.
delete
(
timer_key
);
System
.
out
.
println
(
"job-3 工作结束了,释放锁..."
);
}
}
@Scheduled
(
cron
=
"4 02 15 * * ?"
)
public
void
xsbfTimeTaskOfEnableDt4
()
throws
Exception
{
try
{
if
(
commonRedisHelper
.
lock
(
timer_key
,
1000
*
30
))
{
System
.
out
.
println
(
"job-4 开始工作了,拿锁成功..."
);
Thread
.
sleep
(
3000
);
//休眠3秒
}
else
{
System
.
out
.
println
(
"job-4 拿锁失败..."
);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
commonRedisHelper
.
delete
(
timer_key
);
System
.
out
.
println
(
"job-4 工作结束了,释放锁..."
);
}
}
@Scheduled
(
cron
=
"5 02 15 * * ?"
)
public
void
xsbfTimeTaskOfEnableDt5
()
throws
Exception
{
try
{
if
(
commonRedisHelper
.
lock
(
timer_key
,
1000
*
30
))
{
System
.
out
.
println
(
"job-5 开始工作了,拿锁成功..."
);
Thread
.
sleep
(
3000
);
//休眠3秒
}
else
{
System
.
out
.
println
(
"job-5 拿锁失败..."
);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
commonRedisHelper
.
delete
(
timer_key
);
System
.
out
.
println
(
"job-5 工作结束了,释放锁..."
);
}
}
}
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