-
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.
- Loading branch information
Showing
5 changed files
with
90 additions
and
18 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
34 changes: 34 additions & 0 deletions
34
src/main/java/com/nexters/covid/config/SpringAsyncConfig.java
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,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; | ||
} | ||
} |
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
47 changes: 47 additions & 0 deletions
47
src/main/java/com/nexters/covid/letter/service/MailAsyncService.java
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,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)); | ||
} | ||
} |
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