Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
amos-boot-biz
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
项目统一框架
amos-boot-biz
Commits
56499d93
Commit
56499d93
authored
Feb 04, 2024
by
刘林
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(JG):一码通添加锁修改为Redisson
parent
803ae4f5
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
73 additions
and
60 deletions
+73
-60
GenerateCodeServiceImpl.java
.../module/ymt/biz/service/impl/GenerateCodeServiceImpl.java
+73
-60
No files found.
amos-boot-system-tzs/amos-boot-module-ymt/amos-boot-module-ymt-biz/src/main/java/com/yeejoin/amos/boot/module/ymt/biz/service/impl/GenerateCodeServiceImpl.java
View file @
56499d93
...
...
@@ -6,6 +6,8 @@ import com.yeejoin.amos.boot.module.ymt.api.enums.EquipmentCategoryEnum;
import
com.yeejoin.amos.boot.module.ymt.api.mapper.CategoryOtherInfoMapper
;
import
com.yeejoin.amos.boot.module.ymt.api.service.IGenerateCodeService
;
import
lombok.extern.slf4j.Slf4j
;
import
org.redisson.api.RLock
;
import
org.redisson.api.RedissonClient
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.data.redis.core.StringRedisTemplate
;
import
org.springframework.data.redis.core.ValueOperations
;
...
...
@@ -33,17 +35,19 @@ public class GenerateCodeServiceImpl implements IGenerateCodeService {
private
static
final
String
LOCK_KEY_SUPERVISORY
=
"sequence_lock_supervisory"
;
private
static
final
String
SEQUENCE_TYPE_UR
=
"%05d"
;
private
static
final
String
SEQUENCE_TYPE
=
"%07d"
;
private
static
final
long
LOCK_EXPIRATION_SECONDS
=
600
;
private
final
RedisTemplate
<
String
,
String
>
redisTemplate
;
private
final
StringRedisTemplate
stringRedisTemplate
;
private
final
CategoryOtherInfoMapper
categoryOtherInfoMapper
;
private
final
RedissonClient
redissonClient
;
private
String
rulePrefix
=
""
;
public
GenerateCodeServiceImpl
(
RedisTemplate
<
String
,
String
>
redisTemplate
,
StringRedisTemplate
stringRedisTemplate
,
CategoryOtherInfoMapper
categoryOtherInfoMapper
)
{
public
GenerateCodeServiceImpl
(
RedisTemplate
<
String
,
String
>
redisTemplate
,
StringRedisTemplate
stringRedisTemplate
,
CategoryOtherInfoMapper
categoryOtherInfoMapper
,
RedissonClient
redissonClient
)
{
this
.
redisTemplate
=
redisTemplate
;
this
.
redissonClient
=
redissonClient
;
this
.
stringRedisTemplate
=
stringRedisTemplate
;
this
.
categoryOtherInfoMapper
=
categoryOtherInfoMapper
;
}
/**
* 生成申请单编号(13位,GZ20231214000)
*
...
...
@@ -109,9 +113,11 @@ public class GenerateCodeServiceImpl implements IGenerateCodeService {
private
String
generateSupervisorySequence
(
String
sequenceKey
)
{
// 使用分布式锁,确保在并发情况下只有一个线程能够生成顺序码
Boolean
lockAcquired
=
obtainLock
(
GenerateCodeServiceImpl
.
LOCK_KEY_SUPERVISORY
);
if
(
Boolean
.
TRUE
.
equals
(
lockAcquired
))
{
RLock
lock
=
redissonClient
.
getLock
(
LOCK_KEY_SUPERVISORY
);
try
{
log
.
info
(
"尝试获取锁: {}"
,
lock
.
tryLock
(
10
,
TimeUnit
.
SECONDS
));
if
(
lock
.
isLocked
())
{
// 获取当前顺序码
ValueOperations
<
String
,
String
>
valueOps
=
redisTemplate
.
opsForValue
();
String
currentSequenceStr
=
valueOps
.
get
(
sequenceKey
);
...
...
@@ -126,16 +132,21 @@ public class GenerateCodeServiceImpl implements IGenerateCodeService {
// 更新顺序码
valueOps
.
set
(
sequenceKey
,
formattedSequence
);
return
sequenceKey
+
"-"
+
formattedSequence
;
}
finally
{
releaseLock
(
GenerateCodeServiceImpl
.
LOCK_KEY_SUPERVISORY
);
}
}
else
{
throw
new
RuntimeException
(
"Failed to acquire lock for sequence generation"
);
}
}
catch
(
InterruptedException
e
)
{
Thread
.
currentThread
().
interrupt
();
// 保持中断状态
throw
new
RuntimeException
(
"Thread interrupted while acquiring lock"
,
e
);
}
finally
{
lock
.
unlock
();
// 释放锁
log
.
info
(
"释放锁"
);
}
}
/**
* 监管码为空的话,初始化Redis
*
* @param sequenceKey key
* @return 顺序码
*/
...
...
@@ -166,10 +177,12 @@ public class GenerateCodeServiceImpl implements IGenerateCodeService {
* @return List
*/
public
List
<
String
>
generateBatchSequence
(
String
sequenceKey
,
int
batchSize
)
{
// 使用分布式锁,确保在并发情况下只有一个线程能够生成顺序码
Boolean
lockAcquired
=
obtainLock
(
LOCK_KEY_AF
);
if
(
Boolean
.
TRUE
.
equals
(
lockAcquired
))
{
RLock
lock
=
redissonClient
.
getLock
(
LOCK_KEY_AF
);
try
{
log
.
info
(
"尝试获取锁: {}"
,
lock
.
tryLock
(
10
,
TimeUnit
.
SECONDS
));
if
(
lock
.
isLocked
())
{
// 获取当前顺序码
ValueOperations
<
String
,
String
>
valueOps
=
redisTemplate
.
opsForValue
();
String
currentSequenceStr
=
valueOps
.
get
(
sequenceKey
);
...
...
@@ -192,13 +205,17 @@ public class GenerateCodeServiceImpl implements IGenerateCodeService {
}
return
sequenceList
;
}
finally
{
releaseLock
(
LOCK_KEY_AF
);
}
}
else
{
// 获取锁失败,可以选择重试或采取其他策略
throw
new
RuntimeException
(
"Failed to acquire lock for sequence generation"
);
}
}
catch
(
InterruptedException
e
)
{
Thread
.
currentThread
().
interrupt
();
// 保持中断状态
throw
new
RuntimeException
(
"Thread interrupted while acquiring lock"
,
e
);
}
finally
{
lock
.
unlock
();
// 释放锁
log
.
info
(
"释放锁"
);
}
}
/**
...
...
@@ -211,9 +228,13 @@ public class GenerateCodeServiceImpl implements IGenerateCodeService {
*/
public
String
generateSequence
(
String
sequenceKey
,
String
sequenceType
,
String
lockKey
)
{
// 使用分布式锁,确保在并发情况下只有一个线程能够生成顺序码
Boolean
lockAcquired
=
obtain
Lock
(
lockKey
);
if
(
Boolean
.
TRUE
.
equals
(
lockAcquired
))
{
RLock
lock
=
redissonClient
.
get
Lock
(
lockKey
);
try
{
log
.
info
(
"尝试获取锁: {}"
,
lock
.
tryLock
(
10
,
TimeUnit
.
SECONDS
));
if
(
lock
.
isLocked
())
{
// 获取当前顺序码
ValueOperations
<
String
,
String
>
valueOps
=
redisTemplate
.
opsForValue
();
String
currentSequenceStr
=
valueOps
.
get
(
sequenceKey
);
...
...
@@ -236,40 +257,47 @@ public class GenerateCodeServiceImpl implements IGenerateCodeService {
log
.
info
(
"===================>返回《{}》顺序码:{}<==================="
,
sequenceKey
,
result
);
return
result
;
}
finally
{
releaseLock
(
lockKey
);
}
}
else
{
throw
new
RuntimeException
(
"Failed to acquire lock for sequence generation"
);
}
}
catch
(
InterruptedException
e
)
{
Thread
.
currentThread
().
interrupt
();
// 保持中断状态
throw
new
RuntimeException
(
"Thread interrupted while acquiring lock"
,
e
);
}
finally
{
lock
.
unlock
();
// 释放锁
log
.
info
(
"释放锁"
);
}
}
/**
* 生成顺序码
*
* @param sequenceKey
redis
Key
* @param sequenceKey
Redis
Key
* @param sequenceType 生成码类型
* @param lockKey
redis锁
* @return
s
* @param lockKey
Redis锁Key
* @return
生成的顺序码
*/
public
String
generateElevatorSequence
(
String
sequenceKey
,
String
sequenceType
,
String
lockKey
)
{
// 使用分布式锁,确保在并发情况下只有一个线程能够生成顺序码
Boolean
lockAcquired
=
obtain
Lock
(
lockKey
);
if
(
Boolean
.
TRUE
.
equals
(
lockAcquired
))
{
RLock
lock
=
redissonClient
.
get
Lock
(
lockKey
);
try
{
log
.
info
(
"尝试获取锁: {}"
,
lock
.
tryLock
(
10
,
TimeUnit
.
SECONDS
));
if
(
lock
.
isLocked
())
{
// 获取当前顺序码
ValueOperations
<
String
,
String
>
valueOps
=
redisTemplate
.
opsForValue
();
String
currentSequenceStr
=
valueOps
.
get
(
sequenceKey
);
// 如果为空,则初始化为0
if
(
currentSequenceStr
==
null
)
{
currentSequenceStr
=
EquipmentCategoryEnum
.
getCodeByValue
(
sequenceKey
);
log
.
info
(
"===================>获取《{}》初始码:{}<==================="
,
sequenceKey
,
currentSequenceStr
);
return
currentSequenceStr
;
String
initialCode
=
EquipmentCategoryEnum
.
getCodeByValue
(
sequenceKey
);
log
.
info
(
"===================>获取《{}》初始码:{}<==================="
,
sequenceKey
,
initialCode
);
return
initialCode
;
}
Long
currentSequence
=
Long
.
parseLong
(
currentSequenceStr
);
Long
currentSequence
=
Long
.
parseLong
(
currentSequenceStr
);
log
.
info
(
"===================>获取《{}》当前顺序码:{}<==================="
,
sequenceKey
,
currentSequenceStr
);
currentSequence
++;
// 生成顺序码
String
formattedSequence
=
String
.
format
(
sequenceType
,
currentSequence
);
...
...
@@ -277,13 +305,16 @@ public class GenerateCodeServiceImpl implements IGenerateCodeService {
// 更新顺序码
valueOps
.
set
(
sequenceKey
,
formattedSequence
);
return
formattedSequence
;
}
finally
{
releaseLock
(
lockKey
);
}
}
else
{
throw
new
RuntimeException
(
"Failed to acquire lock for sequence generation"
);
}
}
catch
(
InterruptedException
e
)
{
Thread
.
currentThread
().
interrupt
();
// 保持中断状态
throw
new
RuntimeException
(
"Thread interrupted while acquiring lock"
,
e
);
}
finally
{
lock
.
unlock
();
// 释放锁
log
.
info
(
"释放锁"
);
}
}
/**
...
...
@@ -296,9 +327,11 @@ public class GenerateCodeServiceImpl implements IGenerateCodeService {
*/
public
boolean
reduceSequence
(
String
sequenceKey
,
String
sequenceType
,
String
lockKey
)
{
// 使用分布式锁,确保在并发情况下只有一个线程能够生成顺序码
Boolean
lockAcquired
=
obtainLock
(
lockKey
);
if
(
Boolean
.
TRUE
.
equals
(
lockAcquired
))
{
RLock
lock
=
redissonClient
.
getLock
(
lockKey
);
try
{
lock
.
lock
();
// 获取锁
log
.
info
(
"尝试获取锁: {}"
,
lock
.
tryLock
(
10
,
TimeUnit
.
SECONDS
));
if
(
lock
.
isLocked
())
{
// 获取当前顺序码
ValueOperations
<
String
,
String
>
valueOps
=
redisTemplate
.
opsForValue
();
String
currentSequenceStr
=
valueOps
.
get
(
sequenceKey
);
...
...
@@ -321,36 +354,16 @@ public class GenerateCodeServiceImpl implements IGenerateCodeService {
log
.
warn
(
"===================>无法回退《{}》顺序码,当前顺序码已为0<==================="
,
sequenceKey
);
return
false
;
// 无法回退,当前顺序码已为0
}
}
finally
{
releaseLock
(
lockKey
);
}
}
else
{
throw
new
RuntimeException
(
"Failed to acquire lock for sequence generation"
);
}
}
catch
(
InterruptedException
e
)
{
Thread
.
currentThread
().
interrupt
();
// 保持中断状态
throw
new
RuntimeException
(
"Thread interrupted while acquiring lock"
,
e
);
}
finally
{
lock
.
unlock
();
// 释放锁
log
.
info
(
"释放锁"
);
}
/**
* 分布式锁
*
* @param lockKey lockKey
* @return bool
*/
private
Boolean
obtainLock
(
String
lockKey
)
{
Boolean
lockAcquired
=
redisTemplate
.
opsForValue
().
setIfAbsent
(
lockKey
,
LOCK_VALUE
);
if
(
lockAcquired
!=
null
&&
lockAcquired
)
{
redisTemplate
.
expire
(
lockKey
,
LOCK_EXPIRATION_SECONDS
,
TimeUnit
.
SECONDS
);
}
return
lockAcquired
;
}
/**
* 释放锁
*
* @param lockKey lockKey
*/
private
void
releaseLock
(
String
lockKey
)
{
redisTemplate
.
delete
(
lockKey
);
}
/**
...
...
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