|
1 | | -package org.depromeet.spot.infrastructure.common.aop; |
2 | | - |
3 | | -import java.lang.reflect.Method; |
4 | | -import java.util.Arrays; |
5 | | - |
6 | | -import org.aspectj.lang.ProceedingJoinPoint; |
7 | | -import org.aspectj.lang.annotation.Around; |
8 | | -import org.aspectj.lang.annotation.Aspect; |
9 | | -import org.aspectj.lang.reflect.MethodSignature; |
10 | | -import org.depromeet.spot.common.annotation.DistributedLock; |
11 | | -import org.depromeet.spot.infrastructure.common.util.SpringELParser; |
12 | | -import org.redisson.api.RLock; |
13 | | -import org.redisson.api.RedissonClient; |
14 | | - |
15 | | -import lombok.RequiredArgsConstructor; |
16 | | -import lombok.extern.slf4j.Slf4j; |
17 | | - |
18 | | -@Slf4j |
19 | | -@Aspect |
| 1 | +// package org.depromeet.spot.infrastructure.common.aop; |
| 2 | +// |
| 3 | +// import java.lang.reflect.Method; |
| 4 | +// import java.util.Arrays; |
| 5 | +// |
| 6 | +// import org.aspectj.lang.ProceedingJoinPoint; |
| 7 | +// import org.aspectj.lang.annotation.Around; |
| 8 | +// import org.aspectj.lang.annotation.Aspect; |
| 9 | +// import org.aspectj.lang.reflect.MethodSignature; |
| 10 | +// import org.depromeet.spot.common.annotation.DistributedLock; |
| 11 | +// import org.depromeet.spot.infrastructure.common.util.SpringELParser; |
| 12 | +// import org.redisson.api.RLock; |
| 13 | +// import org.redisson.api.RedissonClient; |
| 14 | +// |
| 15 | +// import lombok.RequiredArgsConstructor; |
| 16 | +// import lombok.extern.slf4j.Slf4j; |
| 17 | +// |
| 18 | +// @Slf4j |
| 19 | +// @Aspect |
20 | 20 | // @Component |
21 | | -@RequiredArgsConstructor |
22 | | -public class DistributedLockAop { |
23 | | - |
24 | | - private final RedissonClient redissonClient; |
25 | | - private final TransactionAop aopForTransaction; |
26 | | - |
27 | | - private static final String REDISSON_LOCK_PREFIX = "LOCK:"; |
28 | | - |
29 | | - @Around("@annotation(org.depromeet.spot.common.annotation.DistributedLock)") |
30 | | - public Object lock(ProceedingJoinPoint joinPoint) throws Throwable { |
31 | | - MethodSignature signature = (MethodSignature) joinPoint.getSignature(); |
32 | | - Method method = signature.getMethod(); |
33 | | - DistributedLock distributedLock = method.getAnnotation(DistributedLock.class); |
34 | | - |
35 | | - String lockKey = generateLockKey(signature, joinPoint.getArgs(), distributedLock); |
36 | | - RLock rLock = redissonClient.getLock(lockKey); |
37 | | - |
38 | | - try { |
39 | | - if (!acquireLock(rLock, distributedLock)) { |
40 | | - return false; |
41 | | - } |
42 | | - return aopForTransaction.proceed(joinPoint); |
43 | | - } catch (InterruptedException e) { |
44 | | - log.error(Arrays.toString(e.getStackTrace())); |
45 | | - throw e; |
46 | | - } finally { |
47 | | - releaseLock(rLock); |
48 | | - } |
49 | | - } |
50 | | - |
51 | | - private String generateLockKey( |
52 | | - MethodSignature signature, Object[] args, DistributedLock distributeLock) { |
53 | | - return REDISSON_LOCK_PREFIX |
54 | | - + SpringELParser.getDynamicValue( |
55 | | - signature.getParameterNames(), args, distributeLock.key()); |
56 | | - } |
57 | | - |
58 | | - private boolean acquireLock(RLock rLock, DistributedLock distributeLock) |
59 | | - throws InterruptedException { |
60 | | - return rLock.tryLock( |
61 | | - distributeLock.waitTime(), distributeLock.leaseTime(), distributeLock.timeUnit()); |
62 | | - } |
63 | | - |
64 | | - private void releaseLock(RLock rLock) { |
65 | | - if (rLock != null && rLock.isHeldByCurrentThread()) { |
66 | | - rLock.unlock(); |
67 | | - } |
68 | | - } |
69 | | -} |
| 21 | +// @RequiredArgsConstructor |
| 22 | +// public class DistributedLockAop { |
| 23 | +// |
| 24 | +// private final RedissonClient redissonClient; |
| 25 | +// private final TransactionAop aopForTransaction; |
| 26 | +// |
| 27 | +// private static final String REDISSON_LOCK_PREFIX = "LOCK:"; |
| 28 | +// |
| 29 | +// @Around("@annotation(org.depromeet.spot.common.annotation.DistributedLock)") |
| 30 | +// public Object lock(ProceedingJoinPoint joinPoint) throws Throwable { |
| 31 | +// MethodSignature signature = (MethodSignature) joinPoint.getSignature(); |
| 32 | +// Method method = signature.getMethod(); |
| 33 | +// DistributedLock distributedLock = method.getAnnotation(DistributedLock.class); |
| 34 | +// |
| 35 | +// String lockKey = generateLockKey(signature, joinPoint.getArgs(), distributedLock); |
| 36 | +// RLock rLock = redissonClient.getLock(lockKey); |
| 37 | +// |
| 38 | +// try { |
| 39 | +// if (!acquireLock(rLock, distributedLock)) { |
| 40 | +// return false; |
| 41 | +// } |
| 42 | +// return aopForTransaction.proceed(joinPoint); |
| 43 | +// } catch (InterruptedException e) { |
| 44 | +// log.error(Arrays.toString(e.getStackTrace())); |
| 45 | +// throw e; |
| 46 | +// } finally { |
| 47 | +// releaseLock(rLock); |
| 48 | +// } |
| 49 | +// } |
| 50 | +// |
| 51 | +// private String generateLockKey( |
| 52 | +// MethodSignature signature, Object[] args, DistributedLock distributeLock) { |
| 53 | +// return REDISSON_LOCK_PREFIX |
| 54 | +// + SpringELParser.getDynamicValue( |
| 55 | +// signature.getParameterNames(), args, distributeLock.key()); |
| 56 | +// } |
| 57 | +// |
| 58 | +// private boolean acquireLock(RLock rLock, DistributedLock distributeLock) |
| 59 | +// throws InterruptedException { |
| 60 | +// return rLock.tryLock( |
| 61 | +// distributeLock.waitTime(), distributeLock.leaseTime(), distributeLock.timeUnit()); |
| 62 | +// } |
| 63 | +// |
| 64 | +// private void releaseLock(RLock rLock) { |
| 65 | +// if (rLock != null && rLock.isHeldByCurrentThread()) { |
| 66 | +// rLock.unlock(); |
| 67 | +// } |
| 68 | +// } |
| 69 | +// } |
0 commit comments