diff --git a/src/main/java/nbc/ticketing/ticket911/common/aop/RedissonMultiLockAspect.java b/src/main/java/nbc/ticketing/ticket911/common/aop/RedissonMultiLockAspect.java index 7493bd5..ba3476b 100644 --- a/src/main/java/nbc/ticketing/ticket911/common/aop/RedissonMultiLockAspect.java +++ b/src/main/java/nbc/ticketing/ticket911/common/aop/RedissonMultiLockAspect.java @@ -39,7 +39,6 @@ public Object lock(ProceedingJoinPoint joinPoint) { RedissonMultiLock annotation = method.getAnnotation(RedissonMultiLock.class); List lockKeys; - List dynamicKeys = parseKeyList(annotation.key(), joinPoint); lockKeys = dynamicKeys.stream() .map(key -> buildLockKey(annotation.group(), key)) @@ -49,19 +48,7 @@ public Object lock(ProceedingJoinPoint joinPoint) { lockKeys, annotation.waitTime(), annotation.leaseTime(), - () -> { - try { - return joinPoint.proceed(); - } catch (Throwable e) { - if (e instanceof Error) { - throw (Error)e; - } - if (e instanceof RuntimeException) { - throw (RuntimeException)e; - } - throw new RuntimeException(e); - } - } + joinPoint::proceed ); } diff --git a/src/main/java/nbc/ticketing/ticket911/common/funtional/ThrowingSupplier.java b/src/main/java/nbc/ticketing/ticket911/common/funtional/ThrowingSupplier.java new file mode 100644 index 0000000..fae4be7 --- /dev/null +++ b/src/main/java/nbc/ticketing/ticket911/common/funtional/ThrowingSupplier.java @@ -0,0 +1,6 @@ +package nbc.ticketing.ticket911.common.funtional; + +@FunctionalInterface +public interface ThrowingSupplier { + T get() throws Throwable; +} diff --git a/src/main/java/nbc/ticketing/ticket911/domain/lock/LockRedisService.java b/src/main/java/nbc/ticketing/ticket911/domain/lock/LockRedisService.java index 392fe5e..29279d5 100644 --- a/src/main/java/nbc/ticketing/ticket911/domain/lock/LockRedisService.java +++ b/src/main/java/nbc/ticketing/ticket911/domain/lock/LockRedisService.java @@ -1,10 +1,11 @@ package nbc.ticketing.ticket911.domain.lock; import java.util.List; -import java.util.concurrent.Callable; + +import nbc.ticketing.ticket911.common.funtional.ThrowingSupplier; public interface LockRedisService { - T executeWithLock(String key, long waitTime, long leaseTime, Callable action); + T executeWithLock(String key, long waitTime, long leaseTime, ThrowingSupplier action); - T executeWithMultiLock(List key, long waitTime, long leaseTime, Callable action); + T executeWithMultiLock(List key, long waitTime, long leaseTime, ThrowingSupplier action); } diff --git a/src/main/java/nbc/ticketing/ticket911/infrastructure/redisson/RedissonLockRedisService.java b/src/main/java/nbc/ticketing/ticket911/infrastructure/redisson/RedissonLockRedisService.java index 92f6752..b1a98dd 100644 --- a/src/main/java/nbc/ticketing/ticket911/infrastructure/redisson/RedissonLockRedisService.java +++ b/src/main/java/nbc/ticketing/ticket911/infrastructure/redisson/RedissonLockRedisService.java @@ -2,11 +2,11 @@ import java.util.ArrayList; import java.util.List; -import java.util.concurrent.Callable; import org.springframework.stereotype.Service; import lombok.RequiredArgsConstructor; +import nbc.ticketing.ticket911.common.funtional.ThrowingSupplier; import nbc.ticketing.ticket911.domain.lock.LockRedisRepository; import nbc.ticketing.ticket911.domain.lock.LockRedisService; import nbc.ticketing.ticket911.infrastructure.redisson.exception.LockRedisException; @@ -19,16 +19,22 @@ public class RedissonLockRedisService implements LockRedisService { private final LockRedisRepository lockRedisRepository; @Override - public T executeWithLock(String key, long waitTime, long leaseTime, Callable action) { + public T executeWithLock(String key, long waitTime, long leaseTime, ThrowingSupplier action) { boolean locked = lockRedisRepository.lock(key, waitTime, leaseTime); if (!locked) { throw new LockRedisException(LockRedisExceptionCode.LOCK_TIMEOUT); } try { - return action.call(); - } catch (RuntimeException re) { - throw re; - } catch (Exception e) { + return action.get(); + } catch (LockRedisException lre) { + throw lre; + } catch (Throwable t) { + if (t instanceof RuntimeException re) { + throw re; + } + if (t instanceof Error err) { + throw err; + } throw new LockRedisException(LockRedisExceptionCode.LOCK_PROCEED_FAIL); } finally { lockRedisRepository.unlock(key); @@ -36,7 +42,8 @@ public T executeWithLock(String key, long waitTime, long leaseTime, Callable } @Override - public T executeWithMultiLock(List keys, long waitTime, long leaseTime, Callable action) { + public T executeWithMultiLock(List keys, long waitTime, long leaseTime, ThrowingSupplier action) + throws LockRedisException { List lockedKeys = new ArrayList<>(); try { @@ -47,10 +54,16 @@ public T executeWithMultiLock(List keys, long waitTime, long leaseTi } lockedKeys.add(key); } - return action.call(); - } catch (RuntimeException re) { - throw re; - } catch (Exception e) { + return action.get(); + } catch (LockRedisException lre) { + throw lre; + } catch (Throwable t) { + if (t instanceof RuntimeException re) { + throw re; + } + if (t instanceof Error err) { + throw err; + } throw new LockRedisException(LockRedisExceptionCode.LOCK_PROCEED_FAIL); } finally { for (String key : lockedKeys) {