Using @Async annotation
- Changed synchronous mail transfer to asynchronous.
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user