17 Kasım 2022 Perşembe

Amazon Simple Notification Service (AWS) SNS - Push Approach

Giriş
Açıklaması şöyle
Simple notification service allows user to publish messages to topic. A user subscribe to topic(s). Whenever a message is published to the topic by publisher, subscriber receives the message published in the topic. Both publisher and consumer are unaware of each other. They do not communicate directly.
Açıklaması şöyle
Amazon Simple Notification Service (SNS) is an API for sending notifications to applications and people. For many developers, the key to SNS is the “people” part—the ability to send push and SMS messages to customers. SNS’s API endpoints allow you to send individual messages, but most of the service’s functionality is built around SNS topics for sending batches of notifications over time.
Adımlar şöyle
- From Services, look for SNS and click on it.
- Open SNS console and from the left panel, select topics.
- Click on create a topic.
- Fill in the name for the topic and keep the default values the same.
- We are done with SNS!
Gradle
Şu satırı dahil ederiz
implementation 'io.awspring.cloud:spring-cloud-starter-aws-messaging:2.3.5'
SNS API
AmazonSNSClient Sınıfı
constructor
AmazonSNSClientBuilder.standard() metodu kullanılır
Örnek
Şöyle yaparız
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.sns.AmazonSNSClient;
import com.amazonaws.services.sns.AmazonSNSClientBuilder;

@Configuration
public class AwsSNSConfig {
  @Value("${cloud.aws.region.static}")
  private String region;

  @Value("${cloud.aws.credentials.access-key}")
  private String awsAccessKey;

  @Value("${cloud.aws.credentials.secret-key}")
  private String awsSecretKey;

  
  @Bean
  public AmazonSNSClient getAWSSNSClient() {
    return (AmazonSNSClient) AmazonSNSClientBuilder.standard()
      .withRegion(region)
      .withCredentials(
        new AWSStaticCredentialsProvider(new BasicAWSCredentials(awsAccessKey, 
                                                                 awsSecretKey)))
      .build();
    }
}
publish metodu
Örnek
Şöyle yaparız
import com.amazonaws.services.sns.AmazonSNSClient;
import com.amazonaws.services.sns.model.PublishRequest;
import com.amazonaws.services.sns.model.SubscribeRequest;

@RestController
public class SNSController {
  private final static String TOPIC_ARN = "---your sns topic arn---";

  @Autowired
  AmazonSNSClient amazonSNSClient;

  @PostMapping("/publish")
  public String publishMessageToSNSTopic() {
    PublishRequest publishRequest = 
      new PublishRequest(TOPIC_ARN, "Hi", "demo from sns");
    amazonSNSClient.publish(publishRequest);
    return "notification send successfully.";
  }
}


Hiç yorum yok:

Yorum Gönder