-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main'
- Loading branch information
Showing
17 changed files
with
371 additions
and
19 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
24 changes: 24 additions & 0 deletions
24
src/main/java/kr/co/studyhubinu/studyhubserver/common/redisson/CustomSpringELParser.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,24 @@ | ||
package kr.co.studyhubinu.studyhubserver.common.redisson; | ||
|
||
import lombok.NoArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.expression.spel.standard.SpelExpressionParser; | ||
import org.springframework.expression.spel.support.StandardEvaluationContext; | ||
|
||
@Slf4j | ||
@NoArgsConstructor | ||
public class CustomSpringELParser { | ||
public static String getDynamicValue(String[] parameterNames, Object[] args, String key) { | ||
SpelExpressionParser parser = new SpelExpressionParser(); | ||
StandardEvaluationContext context = new StandardEvaluationContext(); | ||
for (int i = 0; i < parameterNames.length; i++) { | ||
context.setVariable(parameterNames[i], args[i]); | ||
} | ||
Object value = parser.parseExpression(key).getValue(context, Object.class); | ||
if (value == null) { | ||
log.warn("CustomSpringELParser evaluated value is null for key={}", key); | ||
return null; | ||
} | ||
return value.toString(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/kr/co/studyhubinu/studyhubserver/common/redisson/RedissonDistributedLock.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,14 @@ | ||
package kr.co.studyhubinu.studyhubserver.common.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 RedissonDistributedLock { | ||
|
||
String hashKey(); | ||
String field(); | ||
} |
64 changes: 64 additions & 0 deletions
64
...ain/java/kr/co/studyhubinu/studyhubserver/common/redisson/RedissonDistributedLockAop.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,64 @@ | ||
package kr.co.studyhubinu.studyhubserver.common.redisson; | ||
|
||
import kr.co.studyhubinu.studyhubserver.exception.apply.LockAcquisitionException; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
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.stereotype.Component; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@Aspect | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Component | ||
public class RedissonDistributedLockAop { | ||
|
||
private final RedissonClient redissonClient; | ||
private static final int LOCK_WAIT_TIME = 10; | ||
private static final int LOCK_LEASE_TIME = 3; | ||
|
||
@Around("@annotation(kr.co.studyhubinu.studyhubserver.common.redisson.RedissonDistributedLock)") | ||
public Object around(ProceedingJoinPoint joinPoint) throws Throwable { | ||
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); | ||
Method method = signature.getMethod(); | ||
RedissonDistributedLock redissonDistributedLock = method.getAnnotation(RedissonDistributedLock.class); | ||
String hashKey = getDynamicValue(signature, joinPoint, redissonDistributedLock.hashKey()); | ||
String field = getDynamicValue(signature, joinPoint, redissonDistributedLock.field()); | ||
|
||
RLock lock = redissonClient.getLock(hashKey + ":" + field); | ||
|
||
Object result; | ||
boolean available = false; | ||
try { | ||
available = lock.tryLock(LOCK_WAIT_TIME, LOCK_LEASE_TIME, TimeUnit.SECONDS); | ||
if (!available) { | ||
log.warn("Redisson GetLock Timeout {}", field); | ||
throw new LockAcquisitionException(); | ||
} | ||
|
||
result = joinPoint.proceed(); | ||
} catch (InterruptedException e) { | ||
throw new LockAcquisitionException(); | ||
} finally { | ||
if (available) { | ||
lock.unlock(); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
// 메서드 파라미터(field와 hashkey)를 기반으로 동적으로 값을 지정 | ||
public String getDynamicValue(MethodSignature signature, ProceedingJoinPoint joinPoint, String distributedLock) { | ||
return CustomSpringELParser.getDynamicValue( | ||
signature.getParameterNames(), | ||
joinPoint.getArgs(), | ||
distributedLock); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/kr/co/studyhubinu/studyhubserver/config/RedissonConfig.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,28 @@ | ||
package kr.co.studyhubinu.studyhubserver.config; | ||
|
||
import org.redisson.Redisson; | ||
import org.redisson.api.RedissonClient; | ||
import org.redisson.config.Config; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class RedissonConfig { | ||
@Value("${spring.redis.host}") | ||
private String redisHost; | ||
|
||
@Value("${spring.redis.port}") | ||
private int redisPort; | ||
|
||
private static final String REDISSON_HOST_PREFIX = "redis://"; | ||
|
||
@Bean | ||
public RedissonClient redissonClient() { | ||
RedissonClient redisson = null; | ||
Config config = new Config(); | ||
config.useSingleServer().setAddress(REDISSON_HOST_PREFIX + redisHost + ":" + redisPort); | ||
redisson = Redisson.create(config); | ||
return redisson; | ||
} | ||
} |
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
25 changes: 25 additions & 0 deletions
25
src/main/java/kr/co/studyhubinu/studyhubserver/exception/apply/LockAcquisitionException.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,25 @@ | ||
package kr.co.studyhubinu.studyhubserver.exception.apply; | ||
|
||
import kr.co.studyhubinu.studyhubserver.exception.StatusType; | ||
import kr.co.studyhubinu.studyhubserver.exception.common.CustomException; | ||
|
||
public class LockAcquisitionException extends CustomException { | ||
|
||
private final StatusType status; | ||
private static final String message = "스터디 지원에 대한 락 획득에 실패했습니다."; | ||
|
||
public LockAcquisitionException() { | ||
super(message); | ||
this.status = StatusType.STUDY_APPLY_LOCK_ACQUISITION; | ||
} | ||
|
||
@Override | ||
public StatusType getStatus() { | ||
return status; | ||
} | ||
|
||
@Override | ||
public String getMessage() { | ||
return message; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...va/kr/co/studyhubinu/studyhubserver/exception/study/PostRemainingSeatOverStudyPerson.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,25 @@ | ||
package kr.co.studyhubinu.studyhubserver.exception.study; | ||
|
||
import kr.co.studyhubinu.studyhubserver.exception.StatusType; | ||
import kr.co.studyhubinu.studyhubserver.exception.common.CustomException; | ||
|
||
public class PostRemainingSeatOverStudyPerson extends CustomException { | ||
|
||
private final StatusType status; | ||
private static final String message = "잔여석은 0개가 될 수 없습니다"; | ||
|
||
public PostRemainingSeatOverStudyPerson() { | ||
super(message); | ||
this.status = StatusType.REMAINING_SEATS_OVER_STUDY_PERSON; | ||
} | ||
|
||
@Override | ||
public StatusType getStatus() { | ||
return status; | ||
} | ||
|
||
@Override | ||
public String getMessage() { | ||
return message; | ||
} | ||
} |
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
43 changes: 32 additions & 11 deletions
43
...yhubinu/studyhubserver/studypost/domain/implementations/StudyPostApplyEventPublisher.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,23 +1,44 @@ | ||
package kr.co.studyhubinu.studyhubserver.studypost.domain.implementations; | ||
|
||
import kr.co.studyhubinu.studyhubserver.exception.study.PostNotFoundException; | ||
import kr.co.studyhubinu.studyhubserver.studypost.domain.StudyPostEntity; | ||
import kr.co.studyhubinu.studyhubserver.studypost.repository.StudyPostRepository; | ||
import kr.co.studyhubinu.studyhubserver.common.redisson.RedissonDistributedLock; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class StudyPostApplyEventPublisher { | ||
|
||
private final StudyPostRepository studyPostRepository; | ||
private final StudyPostWriter studyPostWriter; | ||
|
||
@Transactional | ||
public void acceptApplyEventPublish(Long studyId) { | ||
StudyPostEntity studyPost = studyPostRepository.findByIdWithPessimisticLock(studyId).orElseThrow(PostNotFoundException::new); | ||
studyPost.decreaseRemainingSeat(); | ||
studyPost.closeStudyPostIfRemainingSeatIsZero(); | ||
studyPostRepository.save(studyPost); | ||
@RedissonDistributedLock(hashKey = "'apply'", field = "#studyPostId") | ||
public void acceptApplyEventPublish(Long studyPostId) { | ||
studyPostWriter.updateStudyPostApply(studyPostId); | ||
} | ||
|
||
// RLock lock = redissonClient.getLock(studyPostId.toString()); | ||
// boolean available = false; | ||
// try { | ||
// available = lock.tryLock(10, 1, TimeUnit.SECONDS); | ||
// if (!available) { | ||
// throw new StudyApplyLockAcquisitionException(); | ||
// } | ||
// studyPostWriter.updateStudyPostApply(studyPostId); | ||
// } catch (InterruptedException e) { | ||
// throw new StudyApplyLockAcquisitionException(); | ||
// } finally { | ||
// if (available) { | ||
// lock.unlock(); | ||
// } | ||
// } | ||
// @Transactional | ||
// public void acceptApplyEventPublish(Long studyId) { | ||
// StudyPostEntity studyPost = studyPostRepository.findByIdWithPessimisticLock(studyId).orElseThrow(PostNotFoundException::new); | ||
// studyPost.decreaseRemainingSeat(); | ||
// studyPost.closeStudyPostIfRemainingSeatIsZero(); | ||
// studyPostRepository.save(studyPost); | ||
// } | ||
|
||
|
||
} |
23 changes: 23 additions & 0 deletions
23
...va/kr/co/studyhubinu/studyhubserver/studypost/domain/implementations/StudyPostReader.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,23 @@ | ||
package kr.co.studyhubinu.studyhubserver.studypost.domain.implementations; | ||
|
||
import kr.co.studyhubinu.studyhubserver.exception.study.PostNotFoundException; | ||
import kr.co.studyhubinu.studyhubserver.studypost.domain.StudyPostEntity; | ||
import kr.co.studyhubinu.studyhubserver.studypost.repository.StudyPostRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
@Slf4j | ||
public class StudyPostReader { | ||
|
||
private final StudyPostRepository studyPostRepository; | ||
|
||
public StudyPostEntity readByStudyId(Long studyId) { | ||
return studyPostRepository.findByStudyId(studyId).orElseThrow(PostNotFoundException::new); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
...va/kr/co/studyhubinu/studyhubserver/studypost/domain/implementations/StudyPostWriter.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,28 @@ | ||
package kr.co.studyhubinu.studyhubserver.studypost.domain.implementations; | ||
|
||
import kr.co.studyhubinu.studyhubserver.exception.study.PostNotFoundException; | ||
import kr.co.studyhubinu.studyhubserver.studypost.domain.StudyPostEntity; | ||
import kr.co.studyhubinu.studyhubserver.studypost.repository.StudyPostRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class StudyPostWriter { | ||
|
||
private final StudyPostRepository studyPostRepository; | ||
private final StudyPostReader studyPostReader; | ||
|
||
@Transactional | ||
public void updateStudyPostApply(Long studyPostId) { | ||
StudyPostEntity studyPost = studyPostRepository.findById(studyPostId).orElseThrow(PostNotFoundException::new); | ||
studyPost.decreaseRemainingSeat(); | ||
studyPost.closeStudyPostIfRemainingSeatIsZero(); | ||
studyPostRepository.saveAndFlush(studyPost); | ||
} | ||
|
||
} |
Oops, something went wrong.