@Async 을 활용하기 위해 쓰레드 관련 설정 정보를 추가한다.
@Configuration
@EnableAsync
public class AsyncConfig {
private final int CORE_POOL_SIZE = 3;
private final int MAX_POOL_SIZE = 10;
private final int QUEUE_CAPACITY = 100_000;
@Bean(name = "eventHandlerTaskExecutor")
public Executor threadPoolTaskExecutor(){
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(CORE_POOL_SIZE);
taskExecutor.setMaxPoolSize(MAX_POOL_SIZE);
taskExecutor.setQueueCapacity(QUEUE_CAPACITY);
taskExecutor.setThreadNamePrefix("Executor-");
return taskExecutor;
}
}
이벤트 관련 Dto Class 를 만들며 이를 Postfix로 -Event 로 하여 만들었다
@Getter
@Setter
@Builder
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor
public class InviteAccountEvent {
,,,
}
EventListener 관련 클래스를 정의하여 내가 보내고자 하는 큐를 이벤트로 잡아 보낸다.(@TransactionPhase.AFTER_COMMIT 중요)
@RequiredArgsConstructor
@Component
public class AccountEventListener {
private final SqsAccountPublisher accountPublisher;
@Async(value = "eventHandlerTaskExecutor")
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void sendInviteMail(InviteAccountDetailEvent event) {
accountPublisher.inviteAccountDetail(event);
}
}
실제 Event를 Publish하는 Service Class 를 확인해 보자면
@RequiredArgsConstructor
@Transactional
@Service
public class AccountManageService {
private final ApplicationEventPublisher publisher;
public void method(InviteAccountEvent request) {
publisher.publishEvent(request);
}
}
반응형