성공적인 계정 생성 후 연결된 고객에게 SMS를 보내야합니다.스프링 부트를 사용하여 성공적인 사용자 등록 후 sms를 보내려면 어떻게해야합니까?
이 목적을 위해 아래의 조언으로 SMS 서비스가 노출되었습니다.
package com.naresh.advice;
import javax.annotation.PostConstruct;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.naresh.dto.AccountDTO;
import com.naresh.dto.CustomerDTO;
import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Message;
import com.twilio.type.PhoneNumber;
@Component
@Aspect
public class SMSService {
@Value("${twilio.sms.authentication_Id:80b7c5a8b73a26a9b588a906d54269c3}")
private String authenticationId;
@Value("${twilio.sms.account_sid:AC038d9532222b3d39fce4b43a5dce9ce1}")
private String accountId;
@Value("${twilio.sms.from_number:+12566662741}")
private String fromNumber;
@PostConstruct
public void init() {
Twilio.init(accountId, authenticationId);
}
@AfterReturning(pointcut = "execution(* com.naresh.service.impl.CustomerServiceImpl.save(..)) && args(customerDTO,..)", returning = "custId")
public void sendSMS(JoinPoint joinPt, CustomerDTO customerDTO, Long custId) {
Message.creator(new PhoneNumber(customerDTO.getMobile()), new PhoneNumber(fromNumber),
"Customer " + custId + " registered successfully...").create();
}
@AfterReturning(pointcut = "execution(* com.naresh.service.impl.AccountServiceImpl.createAccount(..))", returning = "accDTO")
public void sendSMSAcc(JoinPoint joinPt, AccountDTO accDTO) {
CustomerDTO customerDTO = accDTO.getCustomer();
Message.creator(new PhoneNumber(customerDTO.getMobile()), new PhoneNumber(fromNumber),
"Hi " + customerDTO.getName() + ", Your " + accDTO.getAccountType() + " account " + accDTO.getAccNo()
+ " has been registered with us successfully.Your balance is " + accDTO.getBalance())
.create();
}
}
위의 내용은 계정 생성 작업이 성공적으로 완료된 경우에 올바르게 작동합니다. 그러나 우리가 어떤 오류라도 얻는다면, 그 당시에는 고객이 성공 메시지를받습니다.
도와주세요. docs에 따라 사전
고유 키 위반의 예를 들어 보겠습니다. 고유 키를 위반하는 경우 save() 호출시 예외가 발생하지 않습니다. 데이터를 커밋 할 때 예외가 발생합니다. 위의 시나리오에서 sms는 커밋되기 전에 트리거됩니다. 데이터베이스에 데이터를 저장하지 못하면 고객에게 SMS를 보내서는 안됩니다. 그러나 내 응용 프로그램에서 SMS가 트리거됩니다. –
그런 경우 pointcut을 변경해야합니다. 커밋 후에 트리거되도록하거나 이전에 save 메소드에서 커밋해야합니다. Aspects에는 공통점이 없지만 질문에 표시되지 않는 응용 프로그램의 논리 및 사용법이 있습니다. – Stanislav
안녕 Stabislav, 저장() 여기에 그 스프링 데이터 JPA가) (저장 나타냅니다 @ 거래 \t 공공 AccountDTO의 createAccount (AccountDTO accountDTO) { \t \t 계정 계정 = acctRepo.save()가 accountDTO (변환).; \t \t log.info (account.getAccNo() + "created successfully ..."); \t \t \t \t return convert (account); \t} –