Using @Async annotation

- Changed synchronous mail transfer to asynchronous.
This commit is contained in:
Rebwon
2021-09-27 10:28:56 +09:00
committed by MaengSol
parent 4aace5f571
commit 989857c43d
2 changed files with 31 additions and 1 deletions

View File

@@ -2,13 +2,14 @@ package com.yam.app.account.infrastructure;
import com.yam.app.account.domain.RegisterAccountEvent;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
@Component
final class MailManager {
class MailManager {
private final MailDispatcher mailDispatcher;
private final TemplateEngine templateEngine;
@@ -21,6 +22,7 @@ final class MailManager {
this.host = host;
}
@Async
@TransactionalEventListener
public void handle(RegisterAccountEvent event) {
var newAccount = event.getAccount();

View File

@@ -0,0 +1,28 @@
package com.yam.app.common.configuration;
import java.util.concurrent.Executor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@Slf4j
@EnableAsync
@Configuration
public class AsyncConfiguration implements AsyncConfigurer {
@Override
public Executor getAsyncExecutor() {
var executor = new ThreadPoolTaskExecutor();
int processors = Runtime.getRuntime().availableProcessors();
log.info("processors count {}", processors);
executor.setCorePoolSize(processors);
executor.setMaxPoolSize(processors * 2);
executor.setQueueCapacity(50);
executor.setKeepAliveSeconds(60);
executor.setThreadNamePrefix("AsyncExecutor-");
executor.initialize();
return executor;
}
}