diff --git a/src/main/java/com/server/capple/CappleApplication.java b/src/main/java/com/server/capple/CappleApplication.java index bcd0e14b..a032fc21 100644 --- a/src/main/java/com/server/capple/CappleApplication.java +++ b/src/main/java/com/server/capple/CappleApplication.java @@ -7,6 +7,7 @@ import org.springframework.cache.annotation.EnableCaching; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.data.jpa.repository.config.EnableJpaAuditing; +import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; import java.time.ZoneId; @@ -18,6 +19,7 @@ @EnableConfigurationProperties @EnableScheduling @EnableCaching +@EnableAsync public class CappleApplication { public static void main(String[] args) { diff --git a/src/main/java/com/server/capple/domain/mail/service/MailServiceImpl.java b/src/main/java/com/server/capple/domain/mail/service/MailServiceImpl.java index 08b36210..8ac8df0f 100644 --- a/src/main/java/com/server/capple/domain/mail/service/MailServiceImpl.java +++ b/src/main/java/com/server/capple/domain/mail/service/MailServiceImpl.java @@ -7,8 +7,10 @@ import com.server.capple.global.exception.RestApiException; import com.server.capple.global.exception.errorCode.MailErrorCode; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +@Slf4j @Service @RequiredArgsConstructor public class MailServiceImpl implements MailService { @@ -19,9 +21,12 @@ public class MailServiceImpl implements MailService { @Override public Boolean sendMailAddressCertificationMail(String email, Boolean isWhiteList) { - String certCode = mailUtil.sendMailAddressCertificationMail(email, isWhiteList); - String emailJwt = jwtService.createJwtFromEmail(email); - return mailRedisRepository.save(emailJwt, certCode); + mailUtil.sendMailAddressCertificationMail(email, isWhiteList).thenAccept(certCode -> { + String emailJwt = jwtService.createJwtFromEmail(email); + mailRedisRepository.save(emailJwt, certCode); + log.info("메일 발송 완료 : {}", email); + }); + return true; } @Override diff --git a/src/main/java/com/server/capple/domain/mail/service/MailUtil.java b/src/main/java/com/server/capple/domain/mail/service/MailUtil.java index 538c7fa2..efe6018d 100644 --- a/src/main/java/com/server/capple/domain/mail/service/MailUtil.java +++ b/src/main/java/com/server/capple/domain/mail/service/MailUtil.java @@ -1,10 +1,12 @@ package com.server.capple.domain.mail.service; +import java.util.concurrent.CompletableFuture; + public interface MailUtil { public static Boolean emailAddressFormVerification(String emailAddress) { String emailRegex = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}$"; return emailAddress.matches(emailRegex); } - String sendMailAddressCertificationMail(String receiver, Boolean isWhiteList); + CompletableFuture sendMailAddressCertificationMail(String receiver, Boolean isWhiteList); } diff --git a/src/main/java/com/server/capple/domain/mail/service/MailUtilImpl.java b/src/main/java/com/server/capple/domain/mail/service/MailUtilImpl.java index 2901e3c6..72037a52 100644 --- a/src/main/java/com/server/capple/domain/mail/service/MailUtilImpl.java +++ b/src/main/java/com/server/capple/domain/mail/service/MailUtilImpl.java @@ -5,13 +5,18 @@ import jakarta.mail.MessagingException; import jakarta.mail.internet.MimeMessage; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context; +import java.util.concurrent.CompletableFuture; + +@Slf4j @Component @RequiredArgsConstructor public class MailUtilImpl implements MailUtil { @@ -20,10 +25,11 @@ public class MailUtilImpl implements MailUtil { @Value("${mail.white-list-cert-code}") private String whiteListCertCode; + @Async @Override - public String sendMailAddressCertificationMail(String receiver, Boolean isWhiteList) { + public CompletableFuture sendMailAddressCertificationMail(String receiver, Boolean isWhiteList) { String certCode = generateCertCode(); - if(isWhiteList) certCode = whiteListCertCode; + if (isWhiteList) certCode = whiteListCertCode; MimeMessage mimeMessage = javaMailSender.createMimeMessage(); try { MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, false, "UTF-8"); @@ -31,8 +37,9 @@ public String sendMailAddressCertificationMail(String receiver, Boolean isWhiteL mimeMessageHelper.setSubject("[Capple] 회원가입 인증코드 안내"); mimeMessageHelper.setText(setCertMailContext(certCode), true); javaMailSender.send(mimeMessage); - return certCode; + return CompletableFuture.completedFuture(certCode); } catch (MessagingException e) { + log.error(MailErrorCode.MULTI_PART_CRAETION_FAILED.getMessage()); throw new RestApiException(MailErrorCode.MULTI_PART_CRAETION_FAILED); } }