Skip to content

Commit

Permalink
Merge pull request #268 from MOONSHOT-Team/feature/#267
Browse files Browse the repository at this point in the history
[Refactor] #267 - 유저 회원가입 이벤트 발행 비동기 처리
  • Loading branch information
its-sky authored Apr 16, 2024
2 parents 1b0e577 + e38e8ff commit 70b488b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 41 deletions.
27 changes: 27 additions & 0 deletions moonshot-api/src/main/java/org/moonshot/config/AsyncConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.moonshot.config;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@EnableAsync
@Configuration
public class AsyncConfig implements AsyncConfigurer {

@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(4);
executor.setMaxPoolSize(4);
executor.setQueueCapacity(4);
executor.setKeepAliveSeconds(60);
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
executor.setThreadNamePrefix("async-executor-");
executor.initialize();
return executor;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.moonshot.user.service;

import java.time.LocalDateTime;
import lombok.RequiredArgsConstructor;
import org.moonshot.discord.SignUpEvent;
import org.moonshot.user.model.User;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class UserSignUpService {

private final ApplicationEventPublisher eventPublisher;

@Async
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void publishSignUpEvent(User user) {
eventPublisher.publishEvent(SignUpEvent.of(
user.getName(),
user.getEmail() == null ? "" : user.getEmail(),
user.getSocialPlatform().toString(),
LocalDateTime.now(),
user.getImageUrl()
));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import static org.moonshot.user.service.validator.UserValidator.isNewUser;

import java.time.LocalDateTime;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.moonshot.discord.SignUpEvent;
import org.moonshot.jwt.JwtTokenProvider;
import org.moonshot.jwt.TokenResponse;
import org.moonshot.openfeign.dto.response.google.GoogleInfoResponse;
Expand All @@ -16,10 +14,9 @@
import org.moonshot.user.dto.response.SocialLoginResponse;
import org.moonshot.user.model.User;
import org.moonshot.user.repository.UserRepository;
import org.moonshot.user.service.UserSignUpService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
Expand All @@ -28,19 +25,17 @@ public class GoogleLoginStrategy implements SocialLoginStrategy {

@Value("${google.client-id}")
private String googleClientId;

@Value("${google.client-secret}")
private String googleClientSecret;

@Value("${google.redirect-url}")
private String googleRedirectUrl;

private final GoogleAuthApiClient googleAuthApiClient;
private final GoogleApiClient googleApiClient;

private final ApplicationEventPublisher eventPublisher;
private final JwtTokenProvider jwtTokenProvider;
private final UserRepository userRepository;
private final UserSignUpService userSignUpService;

@Override
@Transactional
Expand All @@ -64,7 +59,7 @@ public SocialLoginResponse login(SocialLoginRequest request) {
.email(userResponse.email())
.build());
user = newUser;
publishSignUpEvent(newUser);
userSignUpService.publishSignUpEvent(newUser);
} else {
user = findUser.get();
user.resetDeleteAt();
Expand All @@ -78,16 +73,4 @@ public boolean support(String provider) {
return provider.equals("GOOGLE");
}

@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void publishSignUpEvent(User user) {
eventPublisher.publishEvent(SignUpEvent.of(
user.getName(),
user.getEmail() == null ? "" : user.getEmail(),
user.getSocialPlatform().toString(),
LocalDateTime.now(),
user.getImageUrl()
));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import static org.moonshot.util.MDCUtil.USER_REQUEST_ORIGIN;
import static org.moonshot.util.MDCUtil.get;

import java.time.LocalDateTime;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.moonshot.discord.SignUpEvent;
import org.moonshot.jwt.JwtTokenProvider;
import org.moonshot.jwt.TokenResponse;
import org.moonshot.openfeign.dto.response.kakao.KakaoTokenResponse;
Expand All @@ -18,10 +16,9 @@
import org.moonshot.user.dto.response.SocialLoginResponse;
import org.moonshot.user.model.User;
import org.moonshot.user.repository.UserRepository;
import org.moonshot.user.service.UserSignUpService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
Expand All @@ -30,16 +27,15 @@ public class KakaoLoginStrategy implements SocialLoginStrategy {

@Value("${kakao.client-id}")
private String kakaoClientId;

@Value("${kakao.redirect-uri}")
private String kakaoRedirectUri;

private final KakaoAuthApiClient kakaoAuthApiClient;
private final KakaoApiClient kakaoApiClient;

private final ApplicationEventPublisher eventPublisher;
private final JwtTokenProvider jwtTokenProvider;
private final UserRepository userRepository;
private final UserSignUpService userSignUpService;

@Override
@Transactional
Expand All @@ -63,7 +59,7 @@ public SocialLoginResponse login(SocialLoginRequest request) {
.email(null)
.build());
user = newUser;
publishSignUpEvent(newUser);
userSignUpService.publishSignUpEvent(newUser);
} else {
user = findUser.get();
user.resetDeleteAt();
Expand All @@ -77,16 +73,4 @@ public boolean support(String provider) {
return provider.equals("KAKAO");
}

@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void publishSignUpEvent(User user) {
eventPublisher.publishEvent(SignUpEvent.of(
user.getName(),
user.getEmail() == null ? "" : user.getEmail(),
user.getSocialPlatform().toString(),
LocalDateTime.now(),
user.getImageUrl()
));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

import org.moonshot.user.dto.request.SocialLoginRequest;
import org.moonshot.user.dto.response.SocialLoginResponse;
import org.moonshot.user.model.User;

public interface SocialLoginStrategy {

SocialLoginResponse login(final SocialLoginRequest request);
boolean support(String provider);
void publishSignUpEvent(final User user);

}

0 comments on commit 70b488b

Please sign in to comment.