-
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.
* chore: redisson 추가 * feat: 분산락에 사용될 key 정의 * feat: redisson 설정, 어노테이션, 애스펙트 작성 * fix: 수행 시간 출력하도록 수정 * refactor: RDBMS 락 대신 분산 락 사용 * feat: toString() 작성 * refactor: 이벤트 핸들러 분리 * fix: 테스트 환경에서 RedissonConfig 로드하지 않도록 수정 * remove: 불필요한 코드 삭제 * fix: test 프로퍼티 수정 * refactor: api 대신 yaml 파일 이용 * fix: view 중복으로 쌓이지 않도록 수정 * refactor: 반환형 수정 * refactor: Order 삭제 * fix: 빌드 실패 수정 * remove: 불필요한 중괄호 삭제 * remove: 불필요한 파일 삭제 * refactor: RedissonClient 생성 방식 변경 * remove: redisson 프로퍼티 제거 * refactor: 예외 변경
- Loading branch information
Showing
19 changed files
with
208 additions
and
73 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
42 changes: 42 additions & 0 deletions
42
...om/nexters/goalpanzi/application/firebase/event/handler/PushNotificationEventHandler.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,42 @@ | ||
package com.nexters.goalpanzi.application.firebase.event.handler; | ||
|
||
import com.nexters.goalpanzi.application.firebase.TopicGenerator; | ||
import com.nexters.goalpanzi.application.mission.event.CompleteMissionEvent; | ||
import com.nexters.goalpanzi.application.mission.event.JoinMissionEvent; | ||
import com.nexters.goalpanzi.infrastructure.firebase.PushNotificationSender; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.event.TransactionPhase; | ||
import org.springframework.transaction.event.TransactionalEventListener; | ||
|
||
import static com.nexters.goalpanzi.domain.firebase.PushNotificationMessage.MISSION_COMPLETED; | ||
import static com.nexters.goalpanzi.domain.firebase.PushNotificationMessage.MISSION_JOINED; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class PushNotificationEventHandler { | ||
|
||
private final PushNotificationSender pushNotificationSender; | ||
|
||
@Async | ||
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) | ||
void handleJoinMissionEvent(final JoinMissionEvent event) { | ||
pushNotificationSender.sendIndividualMessage( | ||
MISSION_JOINED.getTitle(), | ||
MISSION_JOINED.getBody(event.nickname()), | ||
event.deviceToken() | ||
); | ||
} | ||
|
||
@Async | ||
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) | ||
void handleCompleteMissionEvent(final CompleteMissionEvent event) { | ||
String topic = TopicGenerator.getTopic(event.missionId()); | ||
pushNotificationSender.sendGroupMessage( | ||
MISSION_COMPLETED.getTitle(), | ||
MISSION_COMPLETED.getBody(), | ||
topic | ||
); | ||
} | ||
} |
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
15 changes: 15 additions & 0 deletions
15
src/main/java/com/nexters/goalpanzi/common/annotation/RedissonLock.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,15 @@ | ||
package com.nexters.goalpanzi.common.annotation; | ||
|
||
import java.lang.annotation.*; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
public @interface RedissonLock { | ||
String value() default "Unknown"; | ||
|
||
long waitTime() default 5L; | ||
|
||
TimeUnit timeUnit() default TimeUnit.SECONDS; | ||
} |
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
49 changes: 49 additions & 0 deletions
49
src/main/java/com/nexters/goalpanzi/common/aop/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,49 @@ | ||
package com.nexters.goalpanzi.common.aop; | ||
|
||
import com.nexters.goalpanzi.common.annotation.RedissonLock; | ||
import com.nexters.goalpanzi.exception.BaseException; | ||
import com.nexters.goalpanzi.exception.ErrorCode; | ||
import com.nexters.goalpanzi.infrastructure.redisson.LockKey; | ||
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; | ||
|
||
@RequiredArgsConstructor | ||
@Slf4j | ||
@Aspect | ||
@Component | ||
public class RedissonLockAspect { | ||
|
||
private final RedissonClient redissonClient; | ||
|
||
@Around("@annotation(com.nexters.goalpanzi.common.annotation.RedissonLock)") | ||
public Object lockMissionVerification(final ProceedingJoinPoint joinPoint) throws Throwable { | ||
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); | ||
String methodName = methodSignature.getName(); | ||
RedissonLock annotation = methodSignature.getMethod().getAnnotation(RedissonLock.class); | ||
String lockTarget = annotation.value(); | ||
|
||
var args = joinPoint.getArgs(); | ||
LockKey lockKey = LockKey.of(lockTarget, args); | ||
RLock lock = redissonClient.getLock(lockKey.toString()); | ||
|
||
boolean lockable = lock.tryLock(annotation.waitTime(), annotation.timeUnit()); | ||
if (!lockable) { | ||
throw new BaseException(ErrorCode.FAILED_TO_ACQUIRE_REDISSON_LOCK); | ||
} | ||
log.info("{} acquired {} lock with key: {}.", methodName, lockTarget, lockKey); | ||
|
||
joinPoint.proceed(); | ||
|
||
lock.unlock(); | ||
log.info("{} released {} lock with key: {}.", methodName, lockTarget, lockKey); | ||
|
||
return joinPoint; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/nexters/goalpanzi/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,25 @@ | ||
package com.nexters.goalpanzi.config; | ||
|
||
import org.redisson.Redisson; | ||
import org.redisson.api.RedissonClient; | ||
import org.redisson.config.Config; | ||
import org.springframework.boot.autoconfigure.data.redis.RedisProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class RedissonConfig { | ||
|
||
private static final String HOST_PREFIX = "redis://"; | ||
|
||
@Bean | ||
public RedissonClient redissonClient(RedisProperties redisProperties) { | ||
Config config = new Config(); | ||
config.useSingleServer().setAddress(makeAddress(redisProperties)); | ||
return Redisson.create(config); | ||
} | ||
|
||
private String makeAddress(RedisProperties redisProperties) { | ||
return HOST_PREFIX + redisProperties.getHost() + ":" + redisProperties.getPort(); | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
src/main/java/com/nexters/goalpanzi/infrastructure/redisson/LockKey.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 com.nexters.goalpanzi.infrastructure.redisson; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.util.Arrays; | ||
|
||
@RequiredArgsConstructor | ||
public class LockKey { | ||
|
||
public static final String LOCK_PREFIX = "LOCK"; | ||
|
||
private final String target; | ||
private final String value; | ||
|
||
public static LockKey of(final String target, final Object... args) { | ||
return new LockKey(target, Arrays.toString(args)); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return LOCK_PREFIX + ":" + target + ":" + value; | ||
} | ||
} |
11 changes: 8 additions & 3 deletions
11
src/test/java/com/nexters/goalpanzi/GoalpanziApplicationTests.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,13 +1,18 @@ | ||
package com.nexters.goalpanzi; | ||
|
||
import com.nexters.goalpanzi.config.redis.RedisInitializer; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.ContextConfiguration; | ||
|
||
@SpringBootTest | ||
@ContextConfiguration( | ||
initializers = {RedisInitializer.class} | ||
) | ||
class GoalpanziApplicationTests { | ||
|
||
@Test | ||
void contextLoads() { | ||
} | ||
@Test | ||
void contextLoads() { | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
src/test/java/com/nexters/goalpanzi/application/auth/apple/AppleApiCallerTest.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
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
Oops, something went wrong.