diff --git a/cf-framework/cf-spring-boot-starter-redis/src/main/java/com/cf/imes/framework/redis/config/ChenfengCacheAutoConfiguration.java b/cf-framework/cf-spring-boot-starter-redis/src/main/java/com/cf/imes/framework/redis/config/ChenfengCacheAutoConfiguration.java index 861118c27..f8136ab2e 100644 --- a/cf-framework/cf-spring-boot-starter-redis/src/main/java/com/cf/imes/framework/redis/config/ChenfengCacheAutoConfiguration.java +++ b/cf-framework/cf-spring-boot-starter-redis/src/main/java/com/cf/imes/framework/redis/config/ChenfengCacheAutoConfiguration.java @@ -3,7 +3,6 @@ package com.cf.imes.framework.redis.config; import cn.hutool.core.text.StrPool; import com.cf.imes.framework.redis.core.TimeoutRedisCacheManager; import com.cf.imes.framework.redis.util.RedisLockUtil; -import org.redisson.api.RedissonClient; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.cache.CacheProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; @@ -82,7 +81,7 @@ public class ChenfengCacheAutoConfiguration { } @Bean - public RedisLockUtil lockUtil(ChenfengCacheProperties chenfengCacheProperties, RedissonClient client, RedisTemplate redisTemplate) { - return new RedisLockUtil(chenfengCacheProperties, client, redisTemplate); + public RedisLockUtil lockUtil(RedisTemplate redisTemplate) { + return new RedisLockUtil(redisTemplate); } } diff --git a/cf-framework/cf-spring-boot-starter-redis/src/main/java/com/cf/imes/framework/redis/util/RedisLockUtil.java b/cf-framework/cf-spring-boot-starter-redis/src/main/java/com/cf/imes/framework/redis/util/RedisLockUtil.java index 87b26a5e1..6cac73ca4 100644 --- a/cf-framework/cf-spring-boot-starter-redis/src/main/java/com/cf/imes/framework/redis/util/RedisLockUtil.java +++ b/cf-framework/cf-spring-boot-starter-redis/src/main/java/com/cf/imes/framework/redis/util/RedisLockUtil.java @@ -1,11 +1,8 @@ package com.cf.imes.framework.redis.util; import cn.hutool.core.util.ObjectUtil; -import com.cf.imes.framework.redis.config.ChenfengCacheProperties; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.redisson.api.RLock; -import org.redisson.api.RedissonClient; import org.springframework.data.redis.core.RedisTemplate; import java.util.concurrent.TimeUnit; @@ -20,65 +17,8 @@ import java.util.concurrent.TimeUnit; @Slf4j public class RedisLockUtil { - private ChenfengCacheProperties chenfengCacheProperties; - - private RedissonClient redissonClient; - private RedisTemplate redisTemplate; - /** - * 加锁 - * - * @param key rediskey - * @return true拿到锁,反之没有做对应的业务提醒 - */ - public boolean acquireLock(String key) { - boolean getLock = false; - try { - RLock rLock = redissonClient.getLock(key); - return rLock.tryLock(chenfengCacheProperties.getLockWaitTime(), chenfengCacheProperties.getLockTimeout(), TimeUnit.MILLISECONDS); - } catch (Exception e) { - log.error("[RedisLockUtil][lock]加锁失败", e); - return getLock; - } - } - - /** - * 加锁 - * - * @param key rediskey - * @param timeout 锁的超时时间,传空则按照默认配置 - * @param waitTime 等待获取锁的时间,传空则按照默认配置 - * @return true拿到锁,反之没有做对应的业务提醒 - */ - public boolean acquireLock(String key, Integer waitTime, Integer timeout) { - boolean getLock = false; - try { - RLock rLock = redissonClient.getLock(key); - int wt = ObjectUtil.isNull(waitTime) ? chenfengCacheProperties.getLockWaitTime() : waitTime; - int to = ObjectUtil.isNull(timeout) ? chenfengCacheProperties.getLockTimeout() : timeout; - return rLock.tryLock(wt, to, TimeUnit.MILLISECONDS); - } catch (Exception e) { - log.error("[RedisLockUtil][lock]加锁失败", e); - return getLock; - } - } - - /** - * 解锁 - * 使用注意:配合以上两个redisson的lock只能在当前线程中unlock,否则失败!!! - * - * @param key rediskey - */ - public void releaseLock(String key) { - try { - RLock rLock = redissonClient.getLock(key); - rLock.unlock(); - } catch (Exception e) { - log.error("[RedisLockUtil][unlock]解锁失败", e); - } - } - /** * 手动set nx ex加锁 * 使用注意:可用于跨服务 diff --git a/cf-module-prod-executor/cf-module-prod-executor-biz/src/main/java/com/cf/imes/module/executor/controller/admin/order/OrderController.java b/cf-module-prod-executor/cf-module-prod-executor-biz/src/main/java/com/cf/imes/module/executor/controller/admin/order/OrderController.java index 861e932c7..ea5ab814b 100644 --- a/cf-module-prod-executor/cf-module-prod-executor-biz/src/main/java/com/cf/imes/module/executor/controller/admin/order/OrderController.java +++ b/cf-module-prod-executor/cf-module-prod-executor-biz/src/main/java/com/cf/imes/module/executor/controller/admin/order/OrderController.java @@ -10,6 +10,8 @@ import com.cf.imes.framework.common.pojo.PageResult; import com.cf.imes.framework.common.util.object.BeanUtils; import com.cf.imes.framework.mybatis.core.generator.SnowFlakeGenerator; import com.cf.imes.framework.operatelog.core.annotations.OperateLog; +import com.cf.imes.framework.redis.config.ChenfengCacheProperties; +import com.cf.imes.framework.redis.constants.RedisKeyConstants; import com.cf.imes.framework.redis.util.RedisLockUtil; import com.cf.imes.framework.security.core.util.SecurityFrameworkUtils; import com.cf.imes.module.executor.controller.admin.order.vo.order.*; @@ -107,7 +109,9 @@ public class OrderController { @Resource private OrderInputProcessor orderInputProcessor; - private String lockKey = "order:1631:lock:"; + @Resource + private ChenfengCacheProperties chenfengCacheProperties; + private String errKey = "生产数据导入异常,{}"; public static final String ORDER_PLATE_MODEL = "imes_order_plate_model"; @@ -229,13 +233,15 @@ public class OrderController { @OperateLog(module="管理后台 - 生产单管理",name = "生产单api数据导入新增",type = IMPORT) @Parameter(name = "orderNo", description = "原订单号", required = true, example = "20230809027818") @PreAuthorize("@ss.hasPermission('production:manager-list:create')") - public CommonResult getApiData(@RequestParam(value = "orderNo") String orderNo){ - - boolean isLocked = redisLockUtil.acquireLock(lockKey); + public CommonResult getApiData(@RequestParam(value = "orderNo") String orderNo) throws InterruptedException { + // 加导入锁 + String importLockValue = "syncApiImport"; + String importLockKey = String.format(RedisKeyConstants.ORDER_IMPORT_LOCK_KEY, getUserOrganId()); + boolean isLocked = redisLockUtil.acquireLock(importLockKey, importLockValue, chenfengCacheProperties.getLockTimeout()); if (!isLocked) { - log.error(errKey, LOCK_ERR); - return error(LOCK_ERR); + throw new ServiceException(ORDER_IMPORT_ORGAN_LOCK_ERROR); } + Thread.sleep(20000); Long orderId = null; try { orderId = (Long) identifierGenerator.nextId(null); @@ -257,7 +263,8 @@ public class OrderController { return error((ServiceException) cause); } finally { - redisLockUtil.releaseLock(lockKey); + // 导入锁解锁 + redisLockUtil.releaseLock(importLockKey, importLockValue); } } @@ -285,11 +292,12 @@ public class OrderController { Map mapOrder = null; Boolean flag = true; - - boolean isLocked = redisLockUtil.acquireLock(lockKey); + // 加导入锁 + String importLockValue = "syncFileImport"; + String importLockKey = String.format(RedisKeyConstants.ORDER_IMPORT_LOCK_KEY, getUserOrganId()); + boolean isLocked = redisLockUtil.acquireLock(importLockKey, importLockValue, chenfengCacheProperties.getLockTimeout()); if (!isLocked) { - log.error(errKey,LOCK_ERR); - + throw new ServiceException(ORDER_IMPORT_ORGAN_LOCK_ERROR); } else { try { if (type == 0) { //excel @@ -404,8 +412,8 @@ public class OrderController { } } finally { - - redisLockUtil.releaseLock(lockKey); + // 导入锁解锁 + redisLockUtil.releaseLock(importLockKey, importLockValue); } }