RedisLockUtil加锁解锁方法名修复sonar.LocksNotUnlockedCheck

This commit is contained in:
gaoqr
2025-05-22 09:32:00 +08:00
parent e0bfeb6567
commit 2f78e41313
9 changed files with 23 additions and 23 deletions
@@ -32,7 +32,7 @@ public class RedisLockUtil {
* @param key rediskey
* @return true拿到锁,反之没有做对应的业务提醒
*/
public boolean lock(String key) {
public boolean acquireLock(String key) {
boolean getLock = false;
try {
RLock lock = redissonClient.getLock(key);
@@ -51,7 +51,7 @@ public class RedisLockUtil {
* @param waitTime 等待获取锁的时间,传空则按照默认配置
* @return true拿到锁,反之没有做对应的业务提醒
*/
public boolean lock(String key, Integer waitTime, Integer timeout) {
public boolean acquireLock(String key, Integer waitTime, Integer timeout) {
boolean getLock = false;
try {
RLock lock = redissonClient.getLock(key);
@@ -70,7 +70,7 @@ public class RedisLockUtil {
*
* @param key rediskey
*/
public void unlock(String key) {
public void releaseLock(String key) {
try {
RLock lock = redissonClient.getLock(key);
lock.unlock();
@@ -88,7 +88,7 @@ public class RedisLockUtil {
* @param timeout
* @return
*/
public boolean lock(String key, String uniqeKey, Integer timeout) {
public boolean acquireLock(String key, String uniqeKey, Integer timeout) {
return redisTemplate.opsForValue().setIfAbsent(key, uniqeKey, timeout, TimeUnit.MILLISECONDS);
}
@@ -100,7 +100,7 @@ public class RedisLockUtil {
* @param key
* @param uniqeKey 解锁时校验,避免同key下其他任务解锁到
*/
public void unlock(String key, String uniqeKey) {
public void releaseLock(String key, String uniqeKey) {
Object object = redisTemplate.opsForValue().get(key);
if (ObjectUtil.equal(object, uniqeKey)) {
Boolean delete = redisTemplate.delete(key);
@@ -231,7 +231,7 @@ public class OrderController {
@PreAuthorize("@ss.hasPermission('production:manager-list:create')")
public CommonResult<String> getApiData(@RequestParam(value = "orderNo") String orderNo){
boolean isLocked = redisLockUtil.lock(lockKey);
boolean isLocked = redisLockUtil.acquireLock(lockKey);
if (!isLocked) {
log.error(errKey, LOCK_ERR);
return error(LOCK_ERR);
@@ -257,7 +257,7 @@ public class OrderController {
return error((ServiceException) cause);
} finally {
redisLockUtil.unlock(lockKey);
redisLockUtil.releaseLock(lockKey);
}
}
@@ -286,7 +286,7 @@ public class OrderController {
Boolean flag = true;
boolean isLocked = redisLockUtil.lock(lockKey);
boolean isLocked = redisLockUtil.acquireLock(lockKey);
if (!isLocked) {
log.error(errKey,LOCK_ERR);
@@ -405,7 +405,7 @@ public class OrderController {
} finally {
redisLockUtil.unlock(lockKey);
redisLockUtil.releaseLock(lockKey);
}
}
@@ -83,7 +83,7 @@ public class DefaultExcelOrderImportHandler implements AbstractExcelOrderImportH
Long taskId = createImportTask(file.getOriginalFilename());
String importLockKey = String.format(RedisKeyConstants.ORDER_IMPORT_LOCK_KEY, getUserOrganId());
// 检查机构导入锁
boolean lockResult = redisLockUtil.lock(importLockKey, String.valueOf(taskId), chenfengCacheProperties.getLockTimeout());
boolean lockResult = redisLockUtil.acquireLock(importLockKey, String.valueOf(taskId), chenfengCacheProperties.getLockTimeout());
if (!lockResult) {
throw new ServiceException(ORDER_IMPORT_ORGAN_LOCK_ERROR);
}
@@ -95,12 +95,12 @@ public class DefaultExcelOrderImportHandler implements AbstractExcelOrderImportH
EasyExcelFactory.read(inputStream, excelListener).headRowNumber(-1).sheet().doRead();
} catch (ServiceException sev) {
// 导入锁解锁
redisLockUtil.unlock(importLockKey, taskId.toString());
redisLockUtil.releaseLock(importLockKey, taskId.toString());
throw sev;
} catch (Exception e) {
log.error(ORDER_IMPORT_FILE_ANALYZE_ERROR.getMsg(), e);
// 导入锁解锁
redisLockUtil.unlock(importLockKey, taskId.toString());
redisLockUtil.releaseLock(importLockKey, taskId.toString());
throw new ServiceException(ORDER_IMPORT_FILE_ANALYZE_ERROR);
}
@@ -73,7 +73,7 @@ public class DefaultXmlOrderImportHandler implements AbstractXmlOrderImportHandl
Long taskId = createImportTask(file.getOriginalFilename());
String importLockKey = String.format(RedisKeyConstants.ORDER_IMPORT_LOCK_KEY, getUserOrganId());
// 检查机构导入锁
boolean lockResult = redisLockUtil.lock(importLockKey, String.valueOf(taskId), chenfengCacheProperties.getLockTimeout());
boolean lockResult = redisLockUtil.acquireLock(importLockKey, String.valueOf(taskId), chenfengCacheProperties.getLockTimeout());
if (!lockResult) {
throw new ServiceException(ORDER_IMPORT_ORGAN_LOCK_ERROR);
}
@@ -82,12 +82,12 @@ public class DefaultXmlOrderImportHandler implements AbstractXmlOrderImportHandl
defaultOrderImportXmlParserHandler.analyzeFile(file);
} catch (ServiceException sev) {
// 导入锁解锁
redisLockUtil.unlock(importLockKey, taskId.toString());
redisLockUtil.releaseLock(importLockKey, taskId.toString());
throw sev;
} catch (Exception e) {
log.error(ORDER_IMPORT_FILE_ANALYZE_ERROR.getMsg(), e);
// 导入锁解锁
redisLockUtil.unlock(importLockKey, taskId.toString());
redisLockUtil.releaseLock(importLockKey, taskId.toString());
throw new ServiceException(ORDER_IMPORT_FILE_ANALYZE_ERROR);
}
@@ -341,7 +341,7 @@ public class WebCadOrderImportServiceImpl implements WebCadOrderImportService {
private void addSyncLock(Long organId) {
String importLockKey = String.format(RedisKeyConstants.ORDER_IMPORT_LOCK_KEY, organId);
// 检查机构导入锁
boolean lockResult = redisLockUtil.lock(importLockKey, REDIS_UNIQUEKEY, chenfengCacheProperties.getLockTimeout());
boolean lockResult = redisLockUtil.acquireLock(importLockKey, REDIS_UNIQUEKEY, chenfengCacheProperties.getLockTimeout());
if (!lockResult) {
throw new ServiceException(ORDER_IMPORT_ORGAN_LOCK_ERROR);
}
@@ -355,7 +355,7 @@ public class WebCadOrderImportServiceImpl implements WebCadOrderImportService {
private void addAsyncLock(Long taskId) {
String importLockKey = String.format(RedisKeyConstants.ORDER_IMPORT_LOCK_KEY, getUserOrganId());
// 检查机构导入锁
boolean lockResult = redisLockUtil.lock(importLockKey, String.valueOf(taskId), chenfengCacheProperties.getLockTimeout());
boolean lockResult = redisLockUtil.acquireLock(importLockKey, String.valueOf(taskId), chenfengCacheProperties.getLockTimeout());
if (!lockResult) {
throw new ServiceException(ORDER_IMPORT_ORGAN_LOCK_ERROR);
}
@@ -367,11 +367,11 @@ public class WebCadOrderImportServiceImpl implements WebCadOrderImportService {
private void unlock(boolean sync, boolean initAsyncSuccess, Long organId, Long taskId) {
if(sync) {
// 解同步锁
redisLockUtil.unlock(String.format(RedisKeyConstants.ORDER_IMPORT_LOCK_KEY, organId), REDIS_UNIQUEKEY);
redisLockUtil.releaseLock(String.format(RedisKeyConstants.ORDER_IMPORT_LOCK_KEY, organId), REDIS_UNIQUEKEY);
} else {
// 异步发送消息失败了,解开异步锁
if (!initAsyncSuccess && ObjectUtil.isNotNull(taskId)) {
redisLockUtil.unlock(String.format(RedisKeyConstants.ORDER_IMPORT_LOCK_KEY, organId), String.valueOf(taskId));
redisLockUtil.releaseLock(String.format(RedisKeyConstants.ORDER_IMPORT_LOCK_KEY, organId), String.valueOf(taskId));
}
}
}
@@ -154,7 +154,7 @@ public class DefaultExcelOrderImportConsumer {
// 手动确认消息接收
channel.basicAck(deliveryTag, false);
// 导入锁解锁
redisLockUtil.unlock(String.format(RedisKeyConstants.ORDER_IMPORT_LOCK_KEY, organId), message);
redisLockUtil.releaseLock(String.format(RedisKeyConstants.ORDER_IMPORT_LOCK_KEY, organId), message);
}
}
@@ -150,7 +150,7 @@ public class DefaultXmlOrderImportConsumer {
// 手动确认消息接收
channel.basicAck(deliveryTag, false);
// 导入锁解锁
redisLockUtil.unlock(String.format(RedisKeyConstants.ORDER_IMPORT_LOCK_KEY, organId), message);
redisLockUtil.releaseLock(String.format(RedisKeyConstants.ORDER_IMPORT_LOCK_KEY, organId), message);
}
}
@@ -85,7 +85,7 @@ public class OrderImportDeadLetterConsumer {
// 导入锁解锁
if (ObjectUtil.isNotNull(organId)) {
redisLockUtil.unlock(String.format(RedisKeyConstants.ORDER_IMPORT_LOCK_KEY, organId), message);
redisLockUtil.releaseLock(String.format(RedisKeyConstants.ORDER_IMPORT_LOCK_KEY, organId), message);
}
}
@@ -183,7 +183,7 @@ public class WebCadOrderImportConsumer {
channel.basicAck(deliveryTag, false);
}
// 导入锁解锁
redisLockUtil.unlock(String.format(RedisKeyConstants.ORDER_IMPORT_LOCK_KEY, organId), message);
redisLockUtil.releaseLock(String.format(RedisKeyConstants.ORDER_IMPORT_LOCK_KEY, organId), message);
}
}