본문 바로가기
프로그래밍/Back-end

자바 AWS SNS ( Amazon Simple Notification Service ) ! 아마존 문자 서비스 구현 !

by @GodWin 2024. 10. 25.

-

 

-

안녕하세요? 오늘은 아마존 문자 서비스인 AWS SNS ( Amazon Simple Notification Service )에 대해서 알아보도록 하겠습니다.



AWS SNS 란??
: Amazon Simple Notification Service 의 약자로,
게시자에서 구독자에게 메시지 전송을 제공하는 관리형 서비스라고 설명하고 있다.

https://docs.aws.amazon.com/ko_kr/sns/latest/dg/welcome.html

 

Amazon이란 무엇입니까SNS? - Amazon Simple Notification Service

이 페이지에 작업이 필요하다는 점을 알려 주셔서 감사합니다. 실망시켜 드려 죄송합니다. 잠깐 시간을 내어 설명서를 향상시킬 수 있는 방법에 대해 말씀해 주십시오.

docs.aws.amazon.com

 


2탄에서는 Java Spring + 메이븐 프로젝트를 통해 구현하는 방법을 알아보도록 하겠습니다.

 사전 필수 작업은 1탄 참조 (현재 작업중 입니다.)


1. 메이븐 디펜던시 추가를 해줍니다. (pom.xml)

<!-- AWS -->
<dependency>
	<groupId>com.amazonaws</groupId>
	<artifactId>aws-java-sdk-core</artifactId>
	<version>1.11.927</version>
</dependency>

<dependency>	
    <groupId>com.amazonaws</groupId>
	<artifactId>aws-java-sdk-sns</artifactId>
	<version>1.11.927</version>
</dependency>

 


2. SenderDto 생성을 해줍니다.

@Getter public class SmsSenderDto {
 
    private String mobileNo;
    private String smsTxt;
    
    @Builder public SmsSenderDto(String mobileNo, String smsTxt) {
        this.mobileNo = mobileNo; this.smsTxt = smsTxt;
    }
}

 


3. Sender 생성을 해줍니다.

@Slf4j
public class SmsSender {
    
    public PublishResult send(SmsSenderDto senderDto) {
        
        PublishResult result = null;
        
        try {
            
            BasicAWSCredentials awsCreds = new BasicAWSCredentials("액세스키", "시크릿액세스키");
            
            AmazonSNSClientBuilder builder =
                AmazonSNSClientBuilder.standard();
            
            AmazonSNS sns = builder.withRegion(Regions.리전주소)
                .withCredentials(new AWSStaticCredentialsProvider(awsCreds)).build();
            
            Map<String, MessageAttributeValue> smsAttributes = 
                new HashMap<String, MessageAttributeValue>();
            
            smsAttributes.put("AWS.SNS.SMS.SenderID", new MessageAttributeValue()
                .withStringValue("mySenderId").withDataType("String"));
            
            smsAttributes.put("AWS.SNS.SMS.MaxPrice", new MessageAttributeValue()
                .withStringValue("0.50").withDataType("String"));
            
            smsAttributes.put("AWS.SNS.SMS.SMSType", new MessageAttributeValue()
                .withStringValue("Promotional").withDataType("String"));
            
            result = this.sendSMSMessage(sns, 
                senderDto.getSmsTxt(), 
                senderDto.getMobileNo(), 
                smsAttributes);
            
        } catch (Exception ex) {
            
            log.error("The sms was not sent.");
            log.error("Error message: " + ex.getMessage());
            throw new AmazonClientException(ex.getMessage(), ex);
        }
        return result;
    }
    
    public PublishResult sendSMSMessage(AmazonSNS sns, String message , String phoneNumber,
            Map<String, MessageAttributeValue> smsAttributes) {
        
        PublishResult result = sns.publish(new PublishRequest()
                .withMessage(message)
                .withPhoneNumber(phoneNumber)
                .withMessageAttributes(smsAttributes));
        
        return result;
    }

}

 


4. test 코드 작성을 합니다.

/**
 * AWS SNS SMS 발송 처리
 * @param     String mobileNo, String smsTxt
 * @return    PublishResult
 * @throws    
 */
public PublishResult sendSms(String mobileNo, String smsTxt) {
    PublishResult result = null;
    
    SmsSender sender = new SmsSender();
    
    SmsSenderDto dto = new SmsSenderDto(mobileNo, smsTxt);
    
    result = sender.send(dto);
    
    return result;
}



※ 참조 사이트
소스 : https://hyuns.io/59
AWS SNS 가이드 : https://docs.aws.amazon.com/ko_kr/code-samples/latest/catalog/code-catalog-javav2-example_code-sns.html


오늘은 아마존 문자 서비스인 AWS SNS ( Amazon Simple Notification Service )에 대해서 알아보았습니다.
그럼 오늘도 즐거운 하루 되시길 바라겠습니다.