Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public Object lock(ProceedingJoinPoint joinPoint) {
RedissonMultiLock annotation = method.getAnnotation(RedissonMultiLock.class);

List<String> lockKeys;

List<String> dynamicKeys = parseKeyList(annotation.key(), joinPoint);
lockKeys = dynamicKeys.stream()
.map(key -> buildLockKey(annotation.group(), key))
Expand All @@ -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
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package nbc.ticketing.ticket911.common.funtional;

@FunctionalInterface
public interface ThrowingSupplier<T> {
T get() throws Throwable;
}
Original file line number Diff line number Diff line change
@@ -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> T executeWithLock(String key, long waitTime, long leaseTime, Callable<T> action);
<T> T executeWithLock(String key, long waitTime, long leaseTime, ThrowingSupplier<T> action);

<T> T executeWithMultiLock(List<String> key, long waitTime, long leaseTime, Callable<T> action);
<T> T executeWithMultiLock(List<String> key, long waitTime, long leaseTime, ThrowingSupplier<T> action);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,24 +19,31 @@ public class RedissonLockRedisService implements LockRedisService {
private final LockRedisRepository lockRedisRepository;

@Override
public <T> T executeWithLock(String key, long waitTime, long leaseTime, Callable<T> action) {
public <T> T executeWithLock(String key, long waitTime, long leaseTime, ThrowingSupplier<T> 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);
}
}

@Override
public <T> T executeWithMultiLock(List<String> keys, long waitTime, long leaseTime, Callable<T> action) {
public <T> T executeWithMultiLock(List<String> keys, long waitTime, long leaseTime, ThrowingSupplier<T> action)
throws LockRedisException {
List<String> lockedKeys = new ArrayList<>();

try {
Expand All @@ -47,10 +54,16 @@ public <T> T executeWithMultiLock(List<String> 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) {
Expand Down
Loading