Skip to content

Commit

Permalink
feat(server): 메일전송을 비동기로 처리한다. (#513)
Browse files Browse the repository at this point in the history
* feat: 이메일 전송 Async로 동작하도록 수정

* refactor: paymentInfo NoArgs추가

* refactor: index.html 현재 결제 API 맞춰 변경

* refactor: index.html 현재 결제 API 맞춰 변경

* refactor: mailExecutor Bean 등록

* refactor: newFixedThreadPool를 사용하도록 변경
  • Loading branch information
DWL5 committed Oct 27, 2021
1 parent 8f3f6d7 commit 0c73c7e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.tyfserver.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableAsync
public class AsyncConfig {

@Bean(name = "mailExecutor")
public ThreadPoolExecutor mailExecutor() {
return (ThreadPoolExecutor) Executors.newFixedThreadPool(5,
new CustomizableThreadFactory("mail-executor"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.core.io.ClassPathResource;
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;
Expand All @@ -20,16 +21,15 @@
import java.time.format.DateTimeFormatter;

@Component
@Async("mailExecutor")
@RequiredArgsConstructor
public class SmtpMailConnector {
// todo 메일 전송 시간이 김. 비동기로 해볼까?

private static final String PREFIX_SUBJECT = "[Thank You For]";

private final JavaMailSender javaMailSender;
private final TemplateEngine templateEngine;


public void sendVerificationCode(String mailAddress, String verificationCode) {
Context context = new Context();
context.setVariable("code", verificationCode);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.example.tyfserver.payment.domain;

import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.UUID;

@NoArgsConstructor
@Getter
public class PaymentInfo {
private UUID merchantUid;
Expand Down
52 changes: 26 additions & 26 deletions server/src/main/resources/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@

<script>
async function cancelPay() {
await jQuery.ajax({
url: "http://localhost:8080/payments/cancel",
method: "POST",
headers: {"Content-Type": "application/json"},
data: JSON.stringify({
merchantUid: "935cfb0e-86e5-4458-8994-993cd7dc4e19"
})
}).done(await function (data) {
console.log("환불 성공");
console.log(data);
}).fail(function (e) {
alert("실패!")
});
await jQuery.ajax({
url: "http://localhost:8080/payments/cancel",
method: "POST",
headers: {"Content-Type": "application/json"},
data: JSON.stringify({
merchantUid: "935cfb0e-86e5-4458-8994-993cd7dc4e19"
})
}).done(await function (data) {
console.log("환불 성공");
console.log(data);
}).fail(function (e) {
alert("실패!")
});
}
</script>

Expand All @@ -39,26 +39,27 @@

async function requestPay() {
// 가맹점 서버에 Payment 생성 요청
const amount = 1000;
const amount = 1100;
const email = "ssop6403@gmail.com";
const pageName = "hwanorama";
const merchantUid = await createMerchantUid(amount, email, pageName);
const itemId = "ITEM_1";
const merchantUid = await createMerchantUid(amount,itemId);
// IMP.request_pay(param, callback) 호출
let result;
await IMP.request_pay({ // param
pay_method: "card",
merchant_uid: merchantUid,
name: pageName,
name: itemId,
amount: amount,
buyer_email: email
}, async function (rsp) { // callback
if (rsp.success) { // 결제 성공 시: 결제 승인 또는 가상계좌 발급에 성공한 경우
console.log("아임포트 요청 성공");
console.log(rsp);
await jQuery.ajax({
url: "http://localhost:8080/donations", // 가맹점 서버
url: "http://localhost:8080/payments/charge", // 가맹점 서버
method: "POST",
headers: {"Content-Type": "application/json"},
headers: {"Content-Type": "application/json",
"Authorization" : ""},
data: JSON.stringify({
impUid: rsp.imp_uid,
merchantUid: rsp.merchant_uid
Expand All @@ -68,7 +69,7 @@
console.log("결제 끝!");
console.log(data);
result = {
"impUid": data.impUid,
"itemId": data.impUid,
"merchantUid": data.merchantUid
};
if(data.status === "success") {
Expand All @@ -85,16 +86,15 @@
});
}

async function createMerchantUid(amount, email, pageName) {
async function createMerchantUid(amount, itemId) {
let merchantUid;
await jQuery.ajax({
url: "http://localhost:8080/payments", // 가맹점 서버
url: "http://localhost:8080/payments/charge/ready", // 가맹점 서버
method: "POST",
headers: {"Content-Type": "application/json"},
headers: {"Content-Type": "application/json",
"Authorization" : ""},
data: JSON.stringify({
amount: amount,
email: email,
pageName: pageName,
itemId: itemId
}),
contentType: "application/json",
dataType: "json"
Expand Down

0 comments on commit 0c73c7e

Please sign in to comment.