-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e95c7b4
commit 598f0e9
Showing
5 changed files
with
82 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/java/com/thirdparty/ticketing/global/lock/redisson/RedissonLockAnnotation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.thirdparty.ticketing.global.lock.redisson; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface RedissonLockAnnotation { | ||
String key(); // SpEL 표현식으로 Lock 키를 결정 | ||
|
||
int waitTime() default 1; // 기본 대기 시간 | ||
|
||
int lockTTL() default 60; // 락의 TTL | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/com/thirdparty/ticketing/global/lock/redisson/RedissonLockAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.thirdparty.ticketing.global.lock.redisson; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.reflect.MethodSignature; | ||
import org.redisson.api.RLock; | ||
import org.redisson.api.RedissonClient; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.thirdparty.ticketing.domain.common.ErrorCode; | ||
import com.thirdparty.ticketing.domain.common.TicketingException; | ||
import com.thirdparty.ticketing.global.lock.CustomSpringELParser; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@Aspect | ||
@Component | ||
public class RedissonLockAspect { | ||
|
||
@Autowired private RedissonClient redissonClient; | ||
|
||
private static final String REDISSON_LOCK_PREFIX = "seat-lock-"; | ||
|
||
@Around("@annotation(com.thirdparty.ticketing.global.lock.redisson.RedissonLockAnnotation)") | ||
public Object lock(ProceedingJoinPoint joinPoint) throws Throwable { | ||
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); | ||
Method method = signature.getMethod(); | ||
RedissonLockAnnotation redissonLockAnnotation = | ||
method.getAnnotation(RedissonLockAnnotation.class); | ||
String lockKey = | ||
REDISSON_LOCK_PREFIX | ||
+ CustomSpringELParser.getDynamicValue( | ||
signature.getParameterNames(), | ||
joinPoint.getArgs(), | ||
redissonLockAnnotation.key()); | ||
|
||
int waitTime = redissonLockAnnotation.waitTime(); | ||
int lockTTL = redissonLockAnnotation.lockTTL(); | ||
|
||
RLock lock = redissonClient.getLock(lockKey); | ||
|
||
try { | ||
if (!lock.tryLock(waitTime, lockTTL, TimeUnit.SECONDS)) { | ||
return false; | ||
} | ||
return joinPoint.proceed(); | ||
} catch (InterruptedException e) { | ||
throw new TicketingException(ErrorCode.NOT_SELECTABLE_SEAT, e); | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
} |