-
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 pull request #97 from study-hub-inu/feat/concurrency-redisson
Feat/concurrency redisson
- Loading branch information
Showing
12 changed files
with
240 additions
and
15 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
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
.../kr/co/studyhubinu/studyhubserver/exception/apply/StudyApplyLockAcquisitionException.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 StudyApplyLockAcquisitionException extends CustomException { | ||
|
||
private final StatusType status; | ||
private static final String message = "스터디 지원에 대한 락 획득에 실패했습니다."; | ||
|
||
public StudyApplyLockAcquisitionException() { | ||
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
46 changes: 37 additions & 9 deletions
46
...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,51 @@ | ||
package kr.co.studyhubinu.studyhubserver.studypost.domain.implementations; | ||
|
||
import kr.co.studyhubinu.studyhubserver.exception.study.PostNotFoundException; | ||
import kr.co.studyhubinu.studyhubserver.common.timer.Timer; | ||
import kr.co.studyhubinu.studyhubserver.exception.apply.StudyApplyLockAcquisitionException; | ||
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.redisson.api.RLock; | ||
import org.redisson.api.RedissonClient; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class StudyPostApplyEventPublisher { | ||
|
||
private final StudyPostRepository studyPostRepository; | ||
private final RedissonClient redissonClient; | ||
private final StudyPostWriter studyPostWriter; | ||
private final StudyPostReader studyPostReader; | ||
|
||
@Transactional | ||
public void acceptApplyEventPublish(Long studyId) { | ||
StudyPostEntity studyPost = studyPostRepository.findByIdWithPessimisticLock(studyId).orElseThrow(PostNotFoundException::new); | ||
studyPost.decreaseRemainingSeat(); | ||
studyPost.closeStudyPostIfRemainingSeatIsZero(); | ||
studyPostRepository.save(studyPost); | ||
StudyPostEntity studyPost = studyPostReader.readByStudyId(studyId); | ||
RLock lock = redissonClient.getLock(studyPost.getId().toString()); | ||
boolean available = false; | ||
try { | ||
available = lock.tryLock(10, 1, TimeUnit.SECONDS); | ||
if (!available) { | ||
throw new StudyApplyLockAcquisitionException(); | ||
} | ||
studyPostWriter.updateStudyPostApply(studyPost.getId()); | ||
} 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); | ||
} | ||
|
||
} |
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
81 changes: 81 additions & 0 deletions
81
src/main/java/springfox/documentation/spring/web/plugins/WebMvcRequestHandlerProvider.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,81 @@ | ||
package springfox.documentation.spring.web.plugins; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; | ||
import org.springframework.context.annotation.Conditional; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.method.HandlerMethod; | ||
import org.springframework.web.servlet.mvc.method.RequestMappingInfo; | ||
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping; | ||
import springfox.documentation.RequestHandler; | ||
import springfox.documentation.spi.service.RequestHandlerProvider; | ||
import springfox.documentation.spring.web.OnServletBasedWebApplication; | ||
import springfox.documentation.spring.web.WebMvcRequestHandler; | ||
import springfox.documentation.spring.web.readers.operation.HandlerMethodResolver; | ||
|
||
import javax.servlet.ServletContext; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.StreamSupport; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
import static springfox.documentation.builders.BuilderDefaults.nullToEmptyList; | ||
import static springfox.documentation.spi.service.contexts.Orderings.byPatternsCondition; | ||
import static springfox.documentation.spring.web.paths.Paths.ROOT; | ||
|
||
@Component | ||
@Order(Ordered.HIGHEST_PRECEDENCE) | ||
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) | ||
@Conditional(OnServletBasedWebApplication.class) | ||
public class WebMvcRequestHandlerProvider implements RequestHandlerProvider { | ||
private final List<RequestMappingInfoHandlerMapping> handlerMappings; | ||
private final HandlerMethodResolver methodResolver; | ||
private final String contextPath; | ||
|
||
@Autowired | ||
public WebMvcRequestHandlerProvider( | ||
Optional<ServletContext> servletContext, | ||
HandlerMethodResolver methodResolver, | ||
List<RequestMappingInfoHandlerMapping> handlerMappings) { | ||
this.handlerMappings = handlerMappings.stream() | ||
.filter(mapping -> mapping.getPatternParser() == null) | ||
.collect(Collectors.toList()); | ||
this.methodResolver = methodResolver; | ||
this.contextPath = servletContext | ||
.map(ServletContext::getContextPath) | ||
.orElse(ROOT); | ||
} | ||
|
||
@Override | ||
public List<RequestHandler> requestHandlers() { | ||
return nullToEmptyList(handlerMappings).stream() | ||
.filter(requestMappingInfoHandlerMapping -> | ||
!("org.springframework.integration.http.inbound.IntegrationRequestMappingHandlerMapping" | ||
.equals(requestMappingInfoHandlerMapping.getClass() | ||
.getName()))) | ||
.map(toMappingEntries()) | ||
.flatMap((entries -> StreamSupport.stream(entries.spliterator(), false))) | ||
.map(toRequestHandler()) | ||
.sorted(byPatternsCondition()) | ||
.collect(toList()); | ||
} | ||
|
||
private Function<RequestMappingInfoHandlerMapping, | ||
Iterable<Map.Entry<RequestMappingInfo, HandlerMethod>>> toMappingEntries() { | ||
return input -> input.getHandlerMethods() | ||
.entrySet(); | ||
} | ||
|
||
private Function<Map.Entry<RequestMappingInfo, HandlerMethod>, RequestHandler> toRequestHandler() { | ||
return input -> new WebMvcRequestHandler( | ||
contextPath, | ||
methodResolver, | ||
input.getKey(), | ||
input.getValue()); | ||
} | ||
} |
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