Fakultas Ilmu Komputer UI

Skip to content
Snippets Groups Projects
Commit 8ab4725d authored by Darrel Danadyaksa Poli's avatar Darrel Danadyaksa Poli
Browse files

[REFACTOR] Update EmailService to send OTP emails asynchronously using CompletableFuture

parent 08226c49
No related branches found
No related tags found
No related merge requests found
...@@ -6,8 +6,11 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -6,8 +6,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException; import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.concurrent.CompletableFuture;
@Service @Service
public class EmailService { public class EmailService {
private final JavaMailSender mailSender; private final JavaMailSender mailSender;
...@@ -20,7 +23,8 @@ public class EmailService { ...@@ -20,7 +23,8 @@ public class EmailService {
this.mailSender = mailSender; this.mailSender = mailSender;
} }
public boolean sendOTPMail(String to, String otp) { @Async("emailTaskExecutor")
public CompletableFuture<Boolean> sendOTPMail(String to, String otp) {
SimpleMailMessage message = new SimpleMailMessage(); SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(SENDER); message.setFrom(SENDER);
message.setTo(to); message.setTo(to);
...@@ -29,10 +33,10 @@ public class EmailService { ...@@ -29,10 +33,10 @@ public class EmailService {
try { try {
mailSender.send(message); mailSender.send(message);
return CompletableFuture.completedFuture(true);
} catch (MailException e) { } catch (MailException e) {
logger.warn("EmailService.sendOTPMail:: Failed to send mail with error; {}", e.getMessage()); logger.warn("EmailService.sendOTPMail:: Failed to send mail with error; {}", e.getMessage());
return false; return CompletableFuture.completedFuture(false);
} }
return true;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment