ÎļþÃû´Ó ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisLockAspect.java ÐÞ¸Ä |
| | |
| | | package com.ruoyi.common.core.redis; |
| | | package com.ruoyi.framework.aspectj; |
| | | |
| | | |
| | | import com.ruoyi.common.annotation.RedisLock; |
| | | import com.ruoyi.common.utils.file.ImageUtils; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.aspectj.lang.ProceedingJoinPoint; |
| | | import org.aspectj.lang.annotation.Around; |
| | | import org.aspectj.lang.annotation.Aspect; |
| | | import org.aspectj.lang.annotation.Pointcut; |
| | | import org.aspectj.lang.reflect.MethodSignature; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.redisson.api.RLock; |
| | | import org.redisson.api.RedissonClient; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.core.annotation.Order; |
| | | import org.springframework.stereotype.Component; |
| | |
| | | |
| | | /** |
| | | * åå¸å¼éï¼æ³¨è§£å®ç°çæ¬ï¼ |
| | | * |
| | | * @author shenxinquan |
| | | */ |
| | | @Component |
| | | |
| | | @Slf4j |
| | | @Aspect |
| | | @Order(9) |
| | | @Component |
| | | public class RedisLockAspect { |
| | | @Autowired |
| | | private RedisLockUtil redisUtil; |
| | | |
| | | private static final Logger log = LoggerFactory.getLogger(RedisLockAspect.class); |
| | | @Autowired |
| | | private RedissonClient redissonClient; |
| | | |
| | | private static final String LOCK_TITLE = "RedisLock_"; |
| | | |
| | | @Pointcut("@annotation(com.ruoyi.common.annotation.RedisLock)") |
| | | public void annotationPointcut() { |
| | |
| | | String key = ""; |
| | | // é»è®¤30ç§è¿ææ¶é´ |
| | | int expireTime = 30; |
| | | |
| | | try { |
| | | // å¾å°è®¿é®çæ¹æ³å¯¹è±¡ |
| | | Method method = className.getMethod(methodName, argClass); |
| | |
| | | } catch (Exception e) { |
| | | throw new RuntimeException("redisåå¸å¼éæ³¨è§£åæ°å¼å¸¸", e); |
| | | } |
| | | Object res = new Object(); |
| | | if (redisUtil.acquire(key, expireTime, TimeUnit.SECONDS)) { |
| | | try { |
| | | res = joinPoint.proceed(); |
| | | return res; |
| | | } catch (Exception e) { |
| | | throw new RuntimeException(e); |
| | | } finally { |
| | | redisUtil.release(key); |
| | | |
| | | Object res; |
| | | try { |
| | | if (acquire(key, expireTime, TimeUnit.SECONDS)) { |
| | | try { |
| | | res = joinPoint.proceed(); |
| | | return res; |
| | | } catch (Exception e) { |
| | | throw new RuntimeException(e); |
| | | } finally { |
| | | release(key); |
| | | } |
| | | } else { |
| | | throw new RuntimeException("redisåå¸å¼éæ³¨è§£åæ°å¼å¸¸"); |
| | | } |
| | | }else { |
| | | throw new RuntimeException("redisåå¸å¼éæ³¨è§£åæ°å¼å¸¸"); |
| | | } catch (IllegalMonitorStateException e) { |
| | | log.error("lock timeout => key : " + key + " , ThreadName : " + Thread.currentThread().getName()); |
| | | throw new RuntimeException("lock timeout => key : " + key); |
| | | } catch (Exception e) { |
| | | throw new Exception("redisåå¸å¼æªç¥å¼å¸¸", e); |
| | | } |
| | | } |
| | | |
| | |
| | | |
| | | /** |
| | | * è·åkeyä¸#p0ä¸çåæ°åç§° |
| | | * |
| | | * @param key |
| | | * @return |
| | | */ |
| | | private static List<String> getKeyParsList(String key) { |
| | | List<String> listPar = new ArrayList<>(); |
| | |
| | | String parName; |
| | | int indexPre = key.indexOf("#"); |
| | | if (plusIndex > 0) { |
| | | indexNext = key.indexOf("#") + key.substring(key.indexOf("#")).indexOf("+"); |
| | | indexNext = key.indexOf("#") + plusIndex; |
| | | parName = key.substring(indexPre, indexNext); |
| | | } else { |
| | | parName = key.substring(indexPre); |
| | |
| | | return listPar; |
| | | } |
| | | |
| | | /** |
| | | * å éï¼RLockï¼å¸¦è¶
æ¶æ¶é´ç |
| | | */ |
| | | private boolean acquire(String key, long expire, TimeUnit expireUnit) { |
| | | //声ækey对象 |
| | | key = LOCK_TITLE + key; |
| | | try { |
| | | //è·åé对象 |
| | | RLock mylock = redissonClient.getLock(key); |
| | | //å éï¼å¹¶ä¸è®¾ç½®éè¿ææ¶é´ï¼é²æ¢æ»éç产ç |
| | | mylock.tryLock(expire, expire, expireUnit); |
| | | } catch (InterruptedException e) { |
| | | return false; |
| | | } |
| | | log.info("lock => key : " + key + " , ThreadName : " + Thread.currentThread().getName()); |
| | | //å éæå |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * éçéæ¾ |
| | | */ |
| | | private void release(String lockName) { |
| | | //å¿
é¡»æ¯åå éæ¶çåä¸ä¸ªkey |
| | | String key = LOCK_TITLE + lockName; |
| | | //è·åæå¯¹è±¡ |
| | | RLock mylock = redissonClient.getLock(key); |
| | | //éæ¾éï¼è§£éï¼ |
| | | mylock.unlock(); |
| | | log.info("unlock => key : " + key + " , ThreadName : " + Thread.currentThread().getName()); |
| | | } |
| | | |
| | | } |