-
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.
#575 [feat] DistributedLock 템플릿 구현 및 활용
- Loading branch information
Showing
5 changed files
with
52 additions
and
22 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
module-domain/src/main/java/com/mile/common/lock/DistributedLock.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,36 @@ | ||
package com.mile.common.lock; | ||
|
||
import com.mile.exception.message.ErrorMessage; | ||
import com.mile.exception.model.MileException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.redisson.api.RLock; | ||
import org.redisson.api.RedissonClient; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class DistributedLock { | ||
|
||
private final RedissonClient redissonClient; | ||
|
||
public void getLock(final String key) { | ||
final RLock lock = redissonClient.getLock(key); | ||
try { | ||
checkAvailability(lock.tryLock(3, 4, TimeUnit.SECONDS)); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public void afterLock(final String key) { | ||
final RLock lock = redissonClient.getLock(key); | ||
lock.unlock(); | ||
} | ||
|
||
public void checkAvailability(final Boolean available) { | ||
if (!available) throw new MileException(ErrorMessage.TIME_OUT_EXCEPTION); | ||
} | ||
|
||
} |
23 changes: 6 additions & 17 deletions
23
...m/service/lock/MoimNameRequestAspect.java → ...le/common/lock/MoimNameRequestAspect.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 |
---|---|---|
@@ -1,46 +1,35 @@ | ||
package com.mile.moim.service.lock; | ||
package com.mile.common.lock; | ||
|
||
import com.mile.exception.message.ErrorMessage; | ||
import com.mile.exception.model.MileException; | ||
import lombok.RequiredArgsConstructor; | ||
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.redisson.api.RLock; | ||
import org.redisson.api.RedissonClient; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
@Aspect | ||
@RequiredArgsConstructor | ||
@Component | ||
public class MoimNameRequestAspect { | ||
|
||
private final RedissonClient redissonClient; | ||
private final static String MOIM_NAME_LOCK = "MOIM_NAME_LOCK : "; | ||
private final DistributedLock distributedLock; | ||
private final static String MOIM_NAME_LOCK = "MOIM_NAME_LOCK"; | ||
private final AopForTransaction aopForTransaction; | ||
|
||
@Pointcut("@annotation(com.mile.moim.service.lock.AtomicValidateUniqueMoimName)") | ||
@Pointcut("@annotation(com.mile.common.lock.AtomicValidateUniqueMoimName)") | ||
public void uniqueMoimNameCut() { | ||
} | ||
|
||
@Around("uniqueMoimNameCut()") | ||
public Object validateUniqueName(final ProceedingJoinPoint joinPoint) throws Throwable { | ||
final RLock lock = redissonClient.getLock(MOIM_NAME_LOCK); | ||
try { | ||
checkAvailability(lock.tryLock(3, 4, TimeUnit.SECONDS)); | ||
distributedLock.getLock(MOIM_NAME_LOCK); | ||
return aopForTransaction.proceed(joinPoint); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} finally { | ||
lock.unlock(); | ||
distributedLock.afterLock(MOIM_NAME_LOCK); | ||
} | ||
} | ||
|
||
public void checkAvailability(final Boolean available) { | ||
if (!available) throw new MileException(ErrorMessage.TIME_OUT_EXCEPTION); | ||
} | ||
|
||
} |
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