Skip to content
Snippets Groups Projects

redisson改造锁

Merged jiangxiaoming requested to merge jiangxiaoming into dev
Compare and
2 files
+ 98
84
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -48,6 +48,8 @@ import com.seasky.ledgerincome.to.VoucherTo;
import com.seasky.ledgerincome.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
@@ -114,6 +116,8 @@ public class EntryCommandServiceImpl implements EntryCommandService {
EntryBillFlowMapper entryBillFlowMapper;
@Autowired
RedisTemplate redisTemplate;
@Autowired
RedissonClient redissonClient;
//未分配、待认领、需要补充但未提交补充信息的这三部分回单可以暂存入账 作废 全走storageTempAll去了
/*@Transactional
@@ -177,12 +181,11 @@ public class EntryCommandServiceImpl implements EntryCommandService {
public void entryTemp(EntryTempCmd cmd) {
Long incomeId = cmd.getIncomeId();
String lockKey = "lock:entryTemp:" + incomeId;
String lockValue = UUID.randomUUID().toString();
Boolean success = redisTemplate.opsForValue().setIfAbsent(lockKey, lockValue, LOCK_TIMEOUT, TimeUnit.SECONDS);
if(!success){
throw new RuntimeException("当前订单已有用户在处理");
}
try{
RLock lock = redissonClient.getLock(lockKey);
try {
if (!lock.tryLock(LOCK_TIMEOUT, TimeUnit.SECONDS)) {
throw new RuntimeException("当前订单已有用户在处理");
}
AtomicBoolean voucher = new AtomicBoolean(false);
Map<Long,IncomeAggregate> map = new HashMap<>();
@@ -205,9 +208,12 @@ public class EntryCommandServiceImpl implements EntryCommandService {
// push(Arrays.asList(incomeAggregate),detailList,"收入入账",voucher,map,infoOut);
pushVoucher(detailList,voucher,map,infoOut);
}).start();
}finally {
if (lockValue.equals(redisTemplate.opsForValue().get(lockKey))) {
redisTemplate.delete(lockKey);
}catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("获取锁失败", e);
} finally {
if (lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
}
@@ -270,12 +276,11 @@ public class EntryCommandServiceImpl implements EntryCommandService {
public void entryTempWithInvoice(SuppInvoiceCmd cmd) {
Long incomeId = cmd.getIncomeId();
String lockKey = "lock:entryTempWithInvoice:" + incomeId;
String lockValue = UUID.randomUUID().toString();
Boolean success = redisTemplate.opsForValue().setIfAbsent(lockKey, lockValue, LOCK_TIMEOUT, TimeUnit.SECONDS);
if(!success){
throw new RuntimeException("当前订单已有用户在处理");
}
try {
RLock lock = redissonClient.getLock(lockKey);
try {
if (!lock.tryLock(LOCK_TIMEOUT, TimeUnit.SECONDS)) {
throw new RuntimeException("当前订单已有用户在处理");
}
IncomeAggregate incomeAggregate = incomeRepository.findById(cmd.getIncomeId());
EntryBillAggregate entryBillAggregate = entryBillRepository.findByIncomeId(cmd.getIncomeId());
VoucherAggregate agg = voucherRepository.findByOutOrderNo(cmd.getIncomeId());
@@ -299,9 +304,12 @@ public class EntryCommandServiceImpl implements EntryCommandService {
// push(Arrays.asList(incomeAggregate),detailList,"收入入账",new AtomicBoolean(true),map,infoOut);
pushVoucher(detailList,new AtomicBoolean(true),map,infoOut);
}).start();
}finally {
if (lockValue.equals(redisTemplate.opsForValue().get(lockKey))) {
redisTemplate.delete(lockKey);
}catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("获取锁失败", e);
} finally {
if (lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
}
@@ -396,12 +404,11 @@ public class EntryCommandServiceImpl implements EntryCommandService {
public void complete(CompleteIncomeCmd cmd) {
String key = cmd.getUserCode()+"-"+cmd.getUserName();
String lockKey = "lock:complete:" + key;
String lockValue = UUID.randomUUID().toString();
Boolean success = redisTemplate.opsForValue().setIfAbsent(lockKey, lockValue, LOCK_TIMEOUT, TimeUnit.SECONDS);
if(!success){
throw new RuntimeException("当前订单已有用户在处理");
}
try{
RLock lock = redissonClient.getLock(lockKey);
try {
if (!lock.tryLock(LOCK_TIMEOUT, TimeUnit.SECONDS)) {
throw new RuntimeException("当前订单已有用户在处理");
}
List<String> entryCodeList = cmd.getEntryCodeList();
if(entryCodeList == null){
throw new RuntimeException("entryCodeList为空");
@@ -488,23 +495,25 @@ public class EntryCommandServiceImpl implements EntryCommandService {
voucherRepository.save(voucherAggregate);
}
}
}finally {
if (lockValue.equals(redisTemplate.opsForValue().get(lockKey))) {
redisTemplate.delete(lockKey);
}
}
}catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("获取锁失败", e);
} finally {
if (lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
}
@Override
public void entryTempLinkInvoice(EntryTempCmd cmd) {
Long incomeId = cmd.getIncomeId();
String lockKey = "lock:entryTempLinkInvoice:" + incomeId;
String lockValue = UUID.randomUUID().toString();
Boolean success = redisTemplate.opsForValue().setIfAbsent(lockKey, lockValue, LOCK_TIMEOUT, TimeUnit.SECONDS);
if(!success){
throw new RuntimeException("当前订单已有用户在处理");
}
RLock lock = redissonClient.getLock(lockKey);
try {
if (!lock.tryLock(LOCK_TIMEOUT, TimeUnit.SECONDS)) {
throw new RuntimeException("当前订单已有用户在处理");
}
AtomicBoolean voucher =new AtomicBoolean(false);
EntryBillAggregate entryBillAggregate = entryBillRepository.findByIncomeId(cmd.getIncomeId());
VoucherAggregate agg = voucherRepository.findByOutOrderNo(cmd.getIncomeId());
@@ -542,9 +551,12 @@ public class EntryCommandServiceImpl implements EntryCommandService {
// push(Arrays.asList(incomeAggregate),detailList,"收入入账",voucher,map,infoOut);
pushVoucher(detailList,voucher,map,infoOut);
}).start();
}finally {
if (lockValue.equals(redisTemplate.opsForValue().get(lockKey))) {
redisTemplate.delete(lockKey);
}catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("获取锁失败", e);
} finally {
if (lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
}
@@ -643,12 +655,11 @@ public class EntryCommandServiceImpl implements EntryCommandService {
public void entryIncome(EntryTempCmd cmd) {
Long incomeId = cmd.getIncomeId();
String lockKey = "lock:entryIncome:" + incomeId;
String lockValue = UUID.randomUUID().toString();
Boolean success = redisTemplate.opsForValue().setIfAbsent(lockKey, lockValue, LOCK_TIMEOUT, TimeUnit.SECONDS);
if(!success){
throw new RuntimeException("当前订单已有用户在处理");
}
RLock lock = redissonClient.getLock(lockKey);
try {
if (!lock.tryLock(LOCK_TIMEOUT, TimeUnit.SECONDS)) {
throw new RuntimeException("当前订单已有用户在处理");
}
AtomicBoolean voucher = new AtomicBoolean(false);
IncomeAggregate incomeAggregate = incomeRepository.findById(incomeId);
if(incomeAggregate.getReceiptBillVo().getIncomeTypeName().equals("已线下入账")){
@@ -677,9 +688,12 @@ public class EntryCommandServiceImpl implements EntryCommandService {
// push(Arrays.asList(incomeAggregate),detailList, "收入入账",voucher,map,infoOut);
pushVoucher(detailList,voucher,map,infoOut);
}).start();
}catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("获取锁失败", e);
}finally {
if (lockValue.equals(redisTemplate.opsForValue().get(lockKey))) {
redisTemplate.delete(lockKey);
if (lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
}
@@ -777,13 +791,12 @@ public class EntryCommandServiceImpl implements EntryCommandService {
@Override
public void entryIncomeLinkInvoice(EntryTempCmd cmd) {
Long incomeId = cmd.getIncomeId();
String lockValue = UUID.randomUUID().toString();
String lockKey = "lock:entryIncomeLinkInvoice:" + incomeId;
Boolean success = redisTemplate.opsForValue().setIfAbsent(lockKey, lockValue, LOCK_TIMEOUT, TimeUnit.SECONDS);
if(!success){
throw new RuntimeException("当前订单已有用户在处理");
}
try{
RLock lock = redissonClient.getLock(lockKey);
try {
if (!lock.tryLock(LOCK_TIMEOUT, TimeUnit.SECONDS)) {
throw new RuntimeException("当前订单已有用户在处理");
}
AtomicBoolean voucher = new AtomicBoolean(false);
Map<Long,IncomeAggregate> map = new HashMap<>();
List<VchDetailEntity> detailList = new ArrayList<>();
@@ -798,9 +811,12 @@ public class EntryCommandServiceImpl implements EntryCommandService {
//push(Arrays.asList(incomeAggregate),detailList,"收入入账",voucher,map,infoOut);
pushVoucher(detailList,voucher,map,infoOut);
}).start();
}catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("获取锁失败", e);
}finally {
if (lockValue.equals(redisTemplate.opsForValue().get(lockKey))) {
redisTemplate.delete(lockKey);
if (lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
}
@@ -919,12 +935,11 @@ public class EntryCommandServiceImpl implements EntryCommandService {
public void entryIncomeWithInvoice(SuppInvoiceCmd cmd) {
Long incomeId = cmd.getIncomeId();
String lockKey = "lock:entryIncomeWithInvoice:" + incomeId;
String lockValue = UUID.randomUUID().toString();
Boolean success = redisTemplate.opsForValue().setIfAbsent(lockKey, lockValue, LOCK_TIMEOUT, TimeUnit.SECONDS);
if(!success){
throw new RuntimeException("当前订单已有用户在处理");
}
RLock lock = redissonClient.getLock(lockKey);
try {
if (!lock.tryLock(LOCK_TIMEOUT, TimeUnit.SECONDS)) {
throw new RuntimeException("当前订单已有用户在处理");
}
EntryBillAggregate entryBillAggregate = entryBillRepository.findByIncomeId(cmd.getIncomeId());
VoucherAggregate agg = voucherRepository.findByOutOrderNo(cmd.getIncomeId());
// 查询凭证 是否有申请中的
@@ -955,12 +970,14 @@ public class EntryCommandServiceImpl implements EntryCommandService {
pushVoucher(detailList,new AtomicBoolean(true),map,infoOut);
}).start();
}catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("获取锁失败", e);
}finally {
if (lockValue.equals(redisTemplate.opsForValue().get(lockKey))) {
redisTemplate.delete(lockKey);
if (lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
}
@Transactional
public void entryIncomeWithInvoiceTemp(IncomeAggregate incomeAggregate,List<Long> invoiceIds,SuppInvoiceCmd cmd,
@@ -1047,12 +1064,11 @@ public class EntryCommandServiceImpl implements EntryCommandService {
@Override
public void storageTempAll(Long[] longList) {
String lockKey = "lock:storageTempAll";
String lockValue = UUID.randomUUID().toString();
Boolean success = redisTemplate.opsForValue().setIfAbsent(lockKey, lockValue, LOCK_TIMEOUT, TimeUnit.SECONDS);
if(!success){
throw new RuntimeException("当前订单已有用户在处理");
}
try{
RLock lock = redissonClient.getLock(lockKey);
try {
if (!lock.tryLock(LOCK_TIMEOUT, TimeUnit.SECONDS)) {
throw new RuntimeException("当前订单已有用户在处理");
}
List<VchDetailEntity> detailListAll = new ArrayList<>();
List<IncomeAggregate> incomeAggregates = incomeRepository.findByIds(longList);
Map<Long,IncomeAggregate> map = new HashMap<>();
@@ -1063,9 +1079,12 @@ public class EntryCommandServiceImpl implements EntryCommandService {
new Thread(()->{
pushVoucher(detailListAll,new AtomicBoolean(true),map,infoOut);
}).start();
}catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("获取锁失败", e);
}finally {
if (lockValue.equals(redisTemplate.opsForValue().get(lockKey))) {
redisTemplate.delete(lockKey);
if (lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
}