Skip to content

Commit

Permalink
fix: 메일 발송 비동기 처리 #3
Browse files Browse the repository at this point in the history
  • Loading branch information
623nana committed Aug 26, 2021
1 parent a2800d2 commit 5e575db
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/nexters/covid/CovidApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableAsync;

@EnableAsync
@EnableJpaAuditing
@SpringBootApplication
public class CovidApplication {
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/com/nexters/covid/config/SpringAsyncConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.nexters.covid.config;

import java.util.concurrent.Executor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
@EnableAsync
public class SpringAsyncConfig implements AsyncConfigurer {

@Value("${async.pool.core_pool_size}")
private Integer corePoolSize;
@Value("${async.pool.max_pool_size}")
private Integer maxPoolSize;
@Value("${async.pool.queue_capacity}")
private Integer queueCapacity;
@Value("${async.pool.prefix_name}")
private String prefixName;

@Bean(name = "threadPoolTaskExecutor")
public Executor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(corePoolSize);
taskExecutor.setMaxPoolSize(maxPoolSize);
taskExecutor.setMaxPoolSize(queueCapacity);
taskExecutor.setThreadNamePrefix(prefixName);
taskExecutor.initialize();
return taskExecutor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.nexters.covid.base.BaseResponse;
import com.nexters.covid.letter.service.MailService;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.ExecutionException;
import javax.mail.MessagingException;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -17,7 +18,7 @@ public class MailController {

@GetMapping("/mail/{sendOptionId}")
public BaseResponse<?> sendLetters(@PathVariable("sendOptionId") Long sendOptionId)
throws MessagingException, UnsupportedEncodingException {
throws MessagingException, ExecutionException, InterruptedException {
mailService.sendLetters(sendOptionId);
return new BaseResponse<>(200, 0, "", null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.nexters.covid.letter.service;

import com.nexters.covid.base.Constant;
import com.nexters.covid.base.LetterText;
import com.nexters.covid.letter.domain.Letter;
import com.nexters.covid.letter.domain.LetterRepository;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.CompletableFuture;
import javax.mail.internet.MimeMessage;
import lombok.RequiredArgsConstructor;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class MailAsyncService {

private final LetterRepository letterRepository;

@Async("threadPoolTaskExecutor")
@Transactional
public CompletableFuture<Boolean> sendLetter(JavaMailSender javaMailSender, MimeMessage msg,
MimeMessageHelper msgHelper, Letter letter, LetterText text) {
try {
msgHelper.setFrom(letter.getEmail(), "안녕, 나야");
msgHelper.setTo(letter.getLetterTo());
msgHelper.setSubject(generateLetterTitle(letter.getCreatedDate()));
msgHelper.setText(generateLetterTitle(letter.getCreatedDate()));
msgHelper.setText(text.generateLetterBody(letter), true);

javaMailSender.send(msg);
} catch (Exception e) {
return CompletableFuture.completedFuture(Boolean.FALSE);
}
letterRepository.updateLetterState(letter.getEncryptedId());
return CompletableFuture.completedFuture(Boolean.TRUE);
}

private String generateLetterTitle(LocalDateTime createdDate) {
return createdDate.format(DateTimeFormatter.ofPattern(
Constant.LETTER_TITLE_FORMAT));
}
}
22 changes: 5 additions & 17 deletions src/main/java/com/nexters/covid/letter/service/MailService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
import com.nexters.covid.letter.domain.Letter;
import com.nexters.covid.letter.domain.LetterRepository;
import com.nexters.covid.letter.domain.State;
import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.concurrent.ExecutionException;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import lombok.RequiredArgsConstructor;
Expand All @@ -18,12 +16,13 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@RequiredArgsConstructor
@Service
@RequiredArgsConstructor
public class MailService {

private final JavaMailSender javaMailSender;
private final LetterRepository letterRepository;
private final MailAsyncService mailAsyncService;

@Value("${prefix.letter.url}")
private String letterUrl;
Expand All @@ -38,25 +37,14 @@ public List<Letter> findLettersByOptionIdAndState(Long sendOptionId) {

@Transactional
public void sendLetters(Long sendOptionId)
throws MessagingException, UnsupportedEncodingException {
throws MessagingException {
List<Letter> letters = findLettersByOptionIdAndState(sendOptionId);
MimeMessage msg = javaMailSender.createMimeMessage();
MimeMessageHelper msgHelper = new MimeMessageHelper(msg, true, "UTF-8");
LetterText text = new LetterText(letterUrl, stickerUrl);

for (Letter letter : letters) {
msgHelper.setFrom(letter.getEmail(), "안녕, 나야");
msgHelper.setTo(letter.getLetterTo());
msgHelper.setSubject(generateLetterTitle(letter.getCreatedDate()));
msgHelper.setText(text.generateLetterBody(letter), true);

javaMailSender.send(msg);
letterRepository.updateLetterState(letter.getEncryptedId());
mailAsyncService.sendLetter(javaMailSender, msg, msgHelper, letter, text);
}
}

private String generateLetterTitle(LocalDateTime createdDate) {
return createdDate.format(DateTimeFormatter.ofPattern(
Constant.LETTER_TITLE_FORMAT));
}
}

0 comments on commit 5e575db

Please sign in to comment.