-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: 필드 주입 대신 생성자 주입으로 변경 * refactor: 개행 및 EOL 추가 * refactor: 서류, 최종 메일 분리 및 비동기 처리 * refactor: 빌더 생성자로 변경 * test: displayName 추가 * fix: 스레드 작업 대기 시간 연장 * feat: 메일 예외 추가 * feat: 메일 병렬처리 추가 * fix: 대기시간 연장 * refactor: 메일 예약, 메일 전송 로직 분리 * fix: 메일 전송 테스트 비활성화 * fix: setter 추가 * feat: 서류 합격 메일 전송 * fix: 템플릿 정구 수 * feat: 메일 전송 비동기, 40개 배치 전송
- Loading branch information
Showing
9 changed files
with
152 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package land.leets.global.config; | ||
|
||
import java.util.concurrent.Executor; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.EnableAsync; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@EnableAsync | ||
@Configuration | ||
public class AsyncConfig { | ||
|
||
@Bean | ||
public Executor taskExecutor() { | ||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | ||
executor.setCorePoolSize(2); | ||
executor.setMaxPoolSize(4); | ||
executor.setQueueCapacity(200); | ||
executor.setThreadNamePrefix("Mailing-"); | ||
executor.setKeepAliveSeconds(60); | ||
executor.setRejectedExecutionHandler(((r, asyncExecutor) -> log.warn("더 이상 요청을 처리할 수 없습니다."))); | ||
executor.initialize(); | ||
return executor; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package land.leets.global.mail; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import org.springframework.mail.MailSendException; | ||
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.springframework.util.function.ThrowingSupplier; | ||
|
||
import jakarta.mail.Address; | ||
import jakarta.mail.MessagingException; | ||
import jakarta.mail.internet.MimeMessage; | ||
import land.leets.global.mail.dto.MailDto; | ||
import land.leets.global.mail.exception.MailException; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class MailFactory { | ||
|
||
private final JavaMailSender javaMailSender; | ||
|
||
public MimeMessage createMail(MailDto mailDto) { | ||
try { | ||
MimeMessage mimeMessage = javaMailSender.createMimeMessage(); | ||
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, false, "UTF-8"); | ||
mimeMessageHelper.setTo(mailDto.to()); | ||
mimeMessageHelper.setSubject(mailDto.title()); | ||
mimeMessageHelper.setText(mailDto.body(), true); | ||
return mimeMessage; | ||
} catch (MessagingException e) { | ||
throw new MailException(); | ||
} | ||
} | ||
|
||
@Async | ||
public void sendMails(List<MimeMessage> batch) { | ||
try { | ||
javaMailSender.send(batch.toArray(new MimeMessage[0])); | ||
} catch (MailSendException e) { | ||
MimeMessage[] failedMessages = e.getFailedMessages().keySet() | ||
.stream() | ||
.map(object -> (MimeMessage)object) | ||
.toArray(MimeMessage[]::new); | ||
retry(failedMessages); | ||
} | ||
} | ||
|
||
private void retry(MimeMessage[] failedMessages) { | ||
try { | ||
javaMailSender.send(failedMessages); | ||
} catch (MailSendException e) { | ||
e.getFailedMessages().keySet() | ||
.stream() | ||
.map(failure -> (MimeMessage)failure) | ||
.map(mimeMessage -> apply(mimeMessage::getAllRecipients)) | ||
.filter(Objects::nonNull) | ||
.forEach(address -> log.error("fail to send mail. address = {}", address)); | ||
} | ||
} | ||
|
||
private Address apply(ThrowingSupplier<Address[]> function) { | ||
try { | ||
return function.get()[0]; | ||
} catch (Exception e) { | ||
log.error("fail to load failed address"); | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package land.leets.global.mail; | ||
|
||
import java.util.List; | ||
import java.util.stream.IntStream; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import jakarta.mail.internet.MimeMessage; | ||
import land.leets.global.mail.dto.MailDto; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class MailManager { | ||
|
||
private static final int BATCH_SIZE = 40; | ||
|
||
private final MailFactory mailFactory; | ||
|
||
public void sendEmails(List<MailDto> mailDtos) { | ||
List<List<MimeMessage>> batchedMails = createChunkedMails(mailDtos); | ||
|
||
batchedMails.parallelStream() | ||
.forEach(mailFactory::sendMails); | ||
} | ||
|
||
private List<List<MimeMessage>> createChunkedMails(List<MailDto> mailDtos) { | ||
List<MimeMessage> messages = mailDtos.parallelStream() | ||
.map(mailFactory::createMail) | ||
.toList(); | ||
return partitionMessages(messages); | ||
} | ||
|
||
private List<List<MimeMessage>> partitionMessages(List<MimeMessage> messages) { | ||
return IntStream.range(0, (messages.size() + BATCH_SIZE - 1) / BATCH_SIZE) | ||
.mapToObj(i -> messages.subList(i * BATCH_SIZE, Math.min((i + 1) * BATCH_SIZE, messages.size()))) | ||
.toList(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
47 changes: 0 additions & 47 deletions
47
src/test/java/land/leets/domain/mail/usecase/SendFinalMailImplTest.java
This file was deleted.
Oops, something went wrong.
36 changes: 0 additions & 36 deletions
36
src/test/java/land/leets/global/mail/MailProviderTest.java
This file was deleted.
Oops, something went wrong.