forked from Fastcampus-Final-Team3/jober-backend
-
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.
회원 가입 시 스페이스 자동 생성 기능 리팩토링 (Fastcampus-Final-Team3#229)
* feat : 비동기 예외 처리 클래스 구현 - 기본적으로 @async 메서드에서 발생하는 예외는 호출자에게 전파가 되지 않음. - 이는 @async 어노테이션이 붙은 메서드가 별도의 스레드에서 실행되므로 메인 스레드에서 캐치를 할 수 없기 때문 - 비동기 메서드에서 발생하는 예외를 처리하는 클래스인 AsyncExceptionHandler를 구현 - AsyncUncaughtExceptionHandler 인터페이스를 구현하였으며, handleUncaughtException 메서드를 통해 예외를 처리 * feat : 비동기 설정 클래스 추가 - 비동기 처리를 위한 설정 클래스인 AsyncConfig를 추가 - 'threadPoolTaskExecutor'라는 이름의 Executor 빈을 등록하는 메서드를 구현 - Executor는 비동기 작업을 처리할 때 사용될 스레드 풀을 설정하며, corePoolSize는 5, maxPoolSize는 30, queueCapacity는 50으로 설정 - AsyncConfigurerSupport의 getAsyncUncaughtExceptionHandler() 메서드를 오버라이드하여 비동기 메서드에서 발생하는 예외를 처리하는 클래스인 AsyncExceptionHandler를 반환하도록 함 * refactor : 스페이스 생성 로직을 SpaceService 클래스로 분리 - 유저의 개인 및 단체 스페이스를 초기화하고 저장하는 작업을 비동기적으로 처리 * style : 불필요한 import 제거 * feat : MemberSignupEvent 클래스 생성 (Fastcampus-Final-Team3#228) - Event를 사용해 이벤트로 사용하는 클래스라는 것을 명시적으로 표현 - 이벤트에 필요한 정보를 담고 있으며, 이벤트가 발생했을 때 해당 정보를 사용 * refactor : MemberSignupEvent 클래스 필드 제거 (Fastcampus-Final-Team3#228) * refactor : 회원 가입 시 이벤트 디스패처를 통한 스페이스 초기화 로직 적용 (Fastcampus-Final-Team3#228) * feat : 회원 가입 이벤트 핸들러 구현 (Fastcampus-Final-Team3#228) * refactor : 비동기 처리 로직 제거 및 코드 정리 (Fastcampus-Final-Team3#228) * refactor : TransactionalEventListener로 변경 (Fastcampus-Final-Team3#228) - 트랜잭션이 성공할 때만 이벤트 핸들러를 실행되도록 TransactionalEventListener 사용 * refactor : TransactionalEventListener로 변경 (Fastcampus-Final-Team3#228) - 스프링 트랜잭션 성공에 따라 이벤트 핸들러 실행되도록 변경 * style : 불필요한 공백 제거 (Fastcampus-Final-Team3#228)
- Loading branch information
Showing
6 changed files
with
159 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.javajober.core.config; | ||
|
||
import java.util.concurrent.Executor; | ||
|
||
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.AsyncConfigurerSupport; | ||
import org.springframework.scheduling.annotation.EnableAsync; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
||
import com.javajober.core.exception.AsyncExceptionHandler; | ||
|
||
@Configuration | ||
@EnableAsync | ||
public class AsyncConfig extends AsyncConfigurerSupport { | ||
|
||
@Bean(name="threadPoolTaskExecutor") | ||
public Executor getAsyncExecutor() { | ||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | ||
executor.setCorePoolSize(5); | ||
executor.setMaxPoolSize(30); | ||
executor.setQueueCapacity(50); | ||
executor.setThreadNamePrefix("ASYNC-"); | ||
executor.initialize(); | ||
return executor; | ||
} | ||
|
||
@Override | ||
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { | ||
return new AsyncExceptionHandler(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/javajober/core/exception/AsyncExceptionHandler.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,20 @@ | ||
package com.javajober.core.exception; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public class AsyncExceptionHandler implements AsyncUncaughtExceptionHandler { | ||
|
||
@Override | ||
public void handleUncaughtException(Throwable throwable, Method method, Object... obj) { | ||
log.info("Exception message - " + throwable.getMessage()); | ||
log.info("Method name - " + method.getName()); | ||
for (Object param: obj) { | ||
log.info("Parameter value - " + param); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/javajober/member/event/MemberSignupEvent.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.javajober.member.event; | ||
|
||
import com.javajober.member.domain.Member; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class MemberSignupEvent { | ||
|
||
private final Member member; | ||
|
||
public MemberSignupEvent(final Member member) { | ||
this.member = member; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/javajober/member/event/MemberSignupEventHandler.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 com.javajober.member.event; | ||
|
||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.event.TransactionPhase; | ||
import org.springframework.transaction.event.TransactionalEventListener; | ||
|
||
import com.javajober.space.service.SpaceService; | ||
|
||
@Component | ||
public class MemberSignupEventHandler { | ||
|
||
private final SpaceService spaceService; | ||
|
||
public MemberSignupEventHandler(final SpaceService spaceService) { | ||
this.spaceService = spaceService; | ||
} | ||
|
||
@Async("threadPoolTaskExecutor") | ||
@TransactionalEventListener(classes = MemberSignupEvent.class, phase = TransactionPhase.AFTER_COMMIT) | ||
public void handleMemberSignupEvent(final MemberSignupEvent event) { | ||
spaceService.initializeAndSaveNewMemberSpaces(event.getMember()); | ||
} | ||
} |
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