18 Aralık 2022 Pazar

AWS SNS API SnsClient Sınıfı

Maven
Şu satırı dahil ederiz
<dependency>
  <groupId>io.awspring.cloud</groupId>
  <artifactId>spring-cloud-aws-starter-sns</artifactId>
  <version>3.0.0-M33</version>
</dependency>
Örnek
Elimizde şöyle bir kod olsun
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProviderChain;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;

protected AwsCredentialsProvider getAwsCredentialsProvider() {
  return AwsCredentialsProviderChain.builder()
    .addCredentialsProvider(DefaultCredentialsProvider.create())
    .build();
}
createTopic metodu
Şöyle yaparız
import software.amazon.awssdk.regions.Region;

import software.amazon.awssdk.services.sns.SnsClient;
import software.amazon.awssdk.services.sns.model.CreateTopicRequest;

protected SnsClient snsClient() throws URISyntaxException {
  return SnsClient.builder()
    .region(Region.US_EAST_1)
    .endpointOverride(new URI("http://localhost:4566"))
    .credentialsProvider(getAwsCredentialsProvider())
    .build();
}

protected String createTopic(String topicName) throws URISyntaxException {
  var createTopicRequest = CreateTopicRequest.builder()
    .name(topicName)
    .build();
  return snsClient().createTopic(createTopicRequest).topicArn();
}
subscribe metodu
Şöyle yaparız
import software.amazon.awssdk.services.sns.model.SubscribeRequest;

protected SubscribeRequest createSubscribe(String topicName, String queueName)
throws URISyntaxException {
  var topicArn = createTopic(topicName);
  var queueUrl = createQueue(queueName);

  var subscribeRequest = SubscribeRequest.builder()
    .protocol("sqs")
    .topicArn(topicArn)
    .endpoint(queueUrl)
    .build();

  snsClient().subscribe(subscribeRequest);
  return subscribeRequest;
}


Hiç yorum yok:

Yorum Gönder