Skip to content

Commit

Permalink
refactor: retryable 사용
Browse files Browse the repository at this point in the history
  • Loading branch information
minsang-alt committed Dec 24, 2024
1 parent 3f4f4e1 commit 019b67d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 20 deletions.
29 changes: 10 additions & 19 deletions src/main/java/dynamicquad/agilehub/email/AmazonSESService.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,22 @@ public class AmazonSESService implements SMTPService {

@Override
@Async("emailExecutor")
//@Retry(maxRetries = 3, retryFor = {GeneralException.class}, delay = 500)
@Retryable(retryFor = {GeneralException.class}, maxAttempts = 3, backoff = @Backoff(delay = 500))
public CompletableFuture<Void> sendEmail(String subject, Map<String, Object> variables, String... to) {
// Amazon SES를 이용한 이메일 발송
// 재시도 로직
int attempts = 0;
log.error("시도 #{}", attempts + 1);
return CompletableFuture.runAsync(() -> {
doSendEmail(subject, variables, to);
});


}
return CompletableFuture.runAsync(() -> {
try {
String content = htmlTemplateEngine.process("invite", createContext(variables));
SendEmailRequest request = createSendEmailRequest(subject, content, to);

amazonSimpleEmailService.sendEmail(request);
} catch (Exception e) {
log.error("AmazonSESService 이메일 발송 실패: {}", e.getMessage());
throw new GeneralException(ErrorStatus.EMAIL_NOT_SENT);
}
});

public void doSendEmail(String subject, Map<String, Object> variables, String[] to) {
try {
String content = htmlTemplateEngine.process("invite", createContext(variables));
SendEmailRequest request = createSendEmailRequest(subject, content, to);

amazonSimpleEmailService.sendEmail(request);
} catch (Exception e) {
log.error("AmazonSESService 이메일 발송 실패: {}", e.getMessage());
throw new GeneralException(ErrorStatus.EMAIL_NOT_SENT);
}
}

private SendEmailRequest createSendEmailRequest(String subject, String content, String... to) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ private void sendEmail(SendInviteMail sendInviteMail, String token) {
log.info("이메일 전송 완료");
})
.exceptionally(e -> {
log.error("이메일 전송 실패", e);
// 여기서는 모든 재시도가 실패한 후의 최종 실패 처리
log.error("이메일 전송 최종 실패", e);
storeInvitationStatus(sendInviteMail.getEmail(), InvitationStatus.FAILED);
return null;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import java.util.concurrent.ThreadPoolExecutor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
@EnableAsync
@EnableRetry
public class AsyncConfig {

private static final boolean WAIT_TASK_COMPLETE = true;
Expand Down

0 comments on commit 019b67d

Please sign in to comment.