SNSContoh Amazon menggunakan SDK untuk Kotlin - AWS SDKContoh Kode

Ada lebih banyak AWS SDK contoh yang tersedia di GitHub repo SDKContoh AWS Dokumen.

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

SNSContoh Amazon menggunakan SDK untuk Kotlin

Contoh kode berikut menunjukkan cara melakukan tindakan dan menerapkan skenario umum dengan menggunakan AWS SDK for Kotlin dengan AmazonSNS.

Tindakan adalah kutipan kode dari program yang lebih besar dan harus dijalankan dalam konteks. Sementara tindakan menunjukkan cara memanggil fungsi layanan individual, Anda dapat melihat tindakan dalam konteks dalam skenario terkait.

Skenario adalah contoh kode yang menunjukkan kepada Anda bagaimana menyelesaikan tugas tertentu dengan memanggil beberapa fungsi dalam layanan atau dikombinasikan dengan yang lain Layanan AWS.

Setiap contoh menyertakan tautan ke kode sumber lengkap, di mana Anda dapat menemukan instruksi tentang cara mengatur dan menjalankan kode dalam konteks.

Memulai

Contoh kode berikut menunjukkan cara memulai menggunakan AmazonSNS.

SDKuntuk Kotlin
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

import aws.sdk.kotlin.services.sns.SnsClient import aws.sdk.kotlin.services.sns.model.ListTopicsRequest import aws.sdk.kotlin.services.sns.paginators.listTopicsPaginated import kotlinx.coroutines.flow.transform /** Before running this Kotlin code example, set up your development environment, including your credentials. For more information, see the following documentation topic: https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html */ suspend fun main() { listTopicsPag() } suspend fun listTopicsPag() { SnsClient { region = "us-east-1" }.use { snsClient -> snsClient .listTopicsPaginated(ListTopicsRequest { }) .transform { it.topics?.forEach { topic -> emit(topic) } } .collect { topic -> println("The topic ARN is ${topic.topicArn}") } } }

Tindakan

Contoh kode berikut menunjukkan cara menggunakanCreateTopic.

SDKuntuk Kotlin
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

suspend fun createSNSTopic(topicName: String): String { val request = CreateTopicRequest { name = topicName } SnsClient { region = "us-east-1" }.use { snsClient -> val result = snsClient.createTopic(request) return result.topicArn.toString() } }

Contoh kode berikut menunjukkan cara menggunakanDeleteTopic.

SDKuntuk Kotlin
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

suspend fun deleteSNSTopic(topicArnVal: String) { val request = DeleteTopicRequest { topicArn = topicArnVal } SnsClient { region = "us-east-1" }.use { snsClient -> snsClient.deleteTopic(request) println("$topicArnVal was successfully deleted.") } }

Contoh kode berikut menunjukkan cara menggunakanGetTopicAttributes.

SDKuntuk Kotlin
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

suspend fun getSNSTopicAttributes(topicArnVal: String) { val request = GetTopicAttributesRequest { topicArn = topicArnVal } SnsClient { region = "us-east-1" }.use { snsClient -> val result = snsClient.getTopicAttributes(request) println("${result.attributes}") } }

Contoh kode berikut menunjukkan cara menggunakanListSubscriptions.

SDKuntuk Kotlin
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

suspend fun listSNSSubscriptions() { SnsClient { region = "us-east-1" }.use { snsClient -> val response = snsClient.listSubscriptions(ListSubscriptionsRequest {}) response.subscriptions?.forEach { sub -> println("Sub ARN is ${sub.subscriptionArn}") println("Sub protocol is ${sub.protocol}") } } }

Contoh kode berikut menunjukkan cara menggunakanListTopics.

SDKuntuk Kotlin
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

suspend fun listSNSTopics() { SnsClient { region = "us-east-1" }.use { snsClient -> val response = snsClient.listTopics(ListTopicsRequest { }) response.topics?.forEach { topic -> println("The topic ARN is ${topic.topicArn}") } } }

Contoh kode berikut menunjukkan cara menggunakanPublish.

SDKuntuk Kotlin
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

suspend fun pubTopic( topicArnVal: String, messageVal: String, ) { val request = PublishRequest { message = messageVal topicArn = topicArnVal } SnsClient { region = "us-east-1" }.use { snsClient -> val result = snsClient.publish(request) println("${result.messageId} message sent.") } }
  • Untuk API detailnya, lihat Publish in AWS SDKuntuk API referensi Kotlin.

Contoh kode berikut menunjukkan cara menggunakanSetTopicAttributes.

SDKuntuk Kotlin
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

suspend fun setTopAttr( attribute: String?, topicArnVal: String?, value: String?, ) { val request = SetTopicAttributesRequest { attributeName = attribute attributeValue = value topicArn = topicArnVal } SnsClient { region = "us-east-1" }.use { snsClient -> snsClient.setTopicAttributes(request) println("Topic ${request.topicArn} was updated.") } }

Contoh kode berikut menunjukkan cara menggunakanSubscribe.

SDKuntuk Kotlin
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

Berlangganan alamat email ke suatu topik.

suspend fun subEmail( topicArnVal: String, email: String, ): String { val request = SubscribeRequest { protocol = "email" endpoint = email returnSubscriptionArn = true topicArn = topicArnVal } SnsClient { region = "us-east-1" }.use { snsClient -> val result = snsClient.subscribe(request) return result.subscriptionArn.toString() } }

Berlangganan fungsi Lambda ke suatu topik.

suspend fun subLambda( topicArnVal: String?, lambdaArn: String?, ) { val request = SubscribeRequest { protocol = "lambda" endpoint = lambdaArn returnSubscriptionArn = true topicArn = topicArnVal } SnsClient { region = "us-east-1" }.use { snsClient -> val result = snsClient.subscribe(request) println(" The subscription Arn is ${result.subscriptionArn}") } }

Contoh kode berikut menunjukkan cara menggunakanTagResource.

SDKuntuk Kotlin
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

suspend fun addTopicTags(topicArn: String) { val tag = Tag { key = "Team" value = "Development" } val tag2 = Tag { key = "Environment" value = "Gamma" } val tagList = mutableListOf<Tag>() tagList.add(tag) tagList.add(tag2) val request = TagResourceRequest { resourceArn = topicArn tags = tagList } SnsClient { region = "us-east-1" }.use { snsClient -> snsClient.tagResource(request) println("Tags have been added to $topicArn") } }

Contoh kode berikut menunjukkan cara menggunakanUnsubscribe.

SDKuntuk Kotlin
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

suspend fun unSub(subscriptionArnVal: String) { val request = UnsubscribeRequest { subscriptionArn = subscriptionArnVal } SnsClient { region = "us-east-1" }.use { snsClient -> snsClient.unsubscribe(request) println("Subscription was removed for ${request.subscriptionArn}") } }

Skenario

Contoh kode berikut menunjukkan cara membuat aplikasi yang mengirimkan data ke tabel Amazon DynamoDB dan memberi tahu Anda saat pengguna memperbarui tabel.

SDKuntuk Kotlin

Menunjukkan cara membuat aplikasi Android asli yang mengirimkan data menggunakan Amazon API DynamoDB Kotlin dan mengirim pesan teks menggunakan Amazon Kotlin. SNS API

Untuk kode sumber lengkap dan instruksi tentang cara mengatur dan menjalankan, lihat contoh lengkapnya di GitHub.

Layanan yang digunakan dalam contoh ini
  • DynamoDB

  • Amazon SNS

Contoh kode berikut menunjukkan cara membuat aplikasi yang memiliki langganan dan mempublikasikan fungsionalitas dan menerjemahkan pesan.

SDKuntuk Kotlin

Menunjukkan cara menggunakan Amazon SNS Kotlin API untuk membuat aplikasi yang memiliki fungsionalitas langganan dan publikasi. Selain itu, contoh aplikasi ini juga menerjemahkan pesan.

Untuk kode sumber lengkap dan petunjuk tentang cara membuat aplikasi web, lihat contoh lengkapnya di GitHub.

Untuk kode sumber lengkap dan petunjuk tentang cara membuat aplikasi Android asli, lihat contoh lengkapnya di GitHub.

Layanan yang digunakan dalam contoh ini
  • Amazon SNS

  • Amazon Translate

Contoh kode berikut menunjukkan cara membuat aplikasi tanpa server yang memungkinkan pengguna mengelola foto menggunakan label.

SDKuntuk Kotlin

Menunjukkan cara mengembangkan aplikasi manajemen aset foto yang mendeteksi label dalam gambar menggunakan Amazon Rekognition dan menyimpannya untuk pengambilan nanti.

Untuk kode sumber lengkap dan instruksi tentang cara mengatur dan menjalankan, lihat contoh lengkapnya di GitHub.

Untuk mendalami tentang asal usul contoh ini, lihat postingan di Komunitas AWS.

Layanan yang digunakan dalam contoh ini
  • APIGerbang

  • DynamoDB

  • Lambda

  • Amazon Rekognition

  • Amazon S3

  • Amazon SNS

Contoh kode berikut menunjukkan cara mempublikasikan SMS pesan menggunakan AmazonSNS.

SDKuntuk Kotlin
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

suspend fun pubTextSMS( messageVal: String?, phoneNumberVal: String?, ) { val request = PublishRequest { message = messageVal phoneNumber = phoneNumberVal } SnsClient { region = "us-east-1" }.use { snsClient -> val result = snsClient.publish(request) println("${result.messageId} message sent.") } }
  • Untuk API detailnya, lihat Publish in AWS SDKuntuk API referensi Kotlin.

Contoh kode berikut ini menunjukkan cara:

  • Buat topik (FIFOatau non-FIFO).

  • Berlangganan beberapa antrian ke topik dengan opsi untuk menerapkan filter.

  • Publikasikan pesan ke topik.

  • Polling antrian untuk pesan yang diterima.

SDKuntuk Kotlin
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

package com.example.sns import aws.sdk.kotlin.services.sns.SnsClient import aws.sdk.kotlin.services.sns.model.CreateTopicRequest import aws.sdk.kotlin.services.sns.model.DeleteTopicRequest import aws.sdk.kotlin.services.sns.model.PublishRequest import aws.sdk.kotlin.services.sns.model.SetSubscriptionAttributesRequest import aws.sdk.kotlin.services.sns.model.SubscribeRequest import aws.sdk.kotlin.services.sns.model.UnsubscribeRequest import aws.sdk.kotlin.services.sqs.SqsClient import aws.sdk.kotlin.services.sqs.model.CreateQueueRequest import aws.sdk.kotlin.services.sqs.model.DeleteMessageBatchRequest import aws.sdk.kotlin.services.sqs.model.DeleteMessageBatchRequestEntry import aws.sdk.kotlin.services.sqs.model.DeleteQueueRequest import aws.sdk.kotlin.services.sqs.model.GetQueueAttributesRequest import aws.sdk.kotlin.services.sqs.model.GetQueueUrlRequest import aws.sdk.kotlin.services.sqs.model.Message import aws.sdk.kotlin.services.sqs.model.QueueAttributeName import aws.sdk.kotlin.services.sqs.model.ReceiveMessageRequest import aws.sdk.kotlin.services.sqs.model.SetQueueAttributesRequest import com.google.gson.Gson import com.google.gson.JsonObject import com.google.gson.JsonPrimitive import java.util.Scanner /** Before running this Kotlin code example, set up your development environment, including your AWS credentials. For more information, see the following documentation topic: https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html This Kotlin example performs the following tasks: 1. Gives the user three options to choose from. 2. Creates an Amazon Simple Notification Service (Amazon SNS) topic. 3. Creates an Amazon Simple Queue Service (Amazon SQS) queue. 4. Gets the SQS queue Amazon Resource Name (ARN) attribute. 5. Attaches an AWS Identity and Access Management (IAM) policy to the queue. 6. Subscribes to the SQS queue. 7. Publishes a message to the topic. 8. Displays the messages. 9. Deletes the received message. 10. Unsubscribes from the topic. 11. Deletes the SNS topic. */ val DASHES: String = String(CharArray(80)).replace("\u0000", "-") suspend fun main() { val input = Scanner(System.`in`) val useFIFO: String var duplication = "n" var topicName: String var deduplicationID: String? = null var groupId: String? = null val topicArn: String? var sqsQueueName: String val sqsQueueUrl: String? val sqsQueueArn: String val subscriptionArn: String? var selectFIFO = false val message: String val messageList: List<Message?>? val filterList = ArrayList<String>() var msgAttValue = "" println(DASHES) println("Welcome to the AWS SDK for Kotlin messaging with topics and queues.") println( """ In this workflow, you will create an SNS topic and subscribe an SQS queue to the topic. You can select from several options for configuring the topic and the subscriptions for the queue. You can then post to the topic and see the results in the queue. """.trimIndent(), ) println(DASHES) println(DASHES) println( """ SNS topics can be configured as FIFO (First-In-First-Out). FIFO topics deliver messages in order and support deduplication and message filtering. Would you like to work with FIFO topics? (y/n) """.trimIndent(), ) useFIFO = input.nextLine() if (useFIFO.compareTo("y") == 0) { selectFIFO = true println("You have selected FIFO") println( """ Because you have chosen a FIFO topic, deduplication is supported. Deduplication IDs are either set in the message or automatically generated from content using a hash function. If a message is successfully published to an SNS FIFO topic, any message published and determined to have the same deduplication ID, within the five-minute deduplication interval, is accepted but not delivered. For more information about deduplication, see https://docs.aws.amazon.com/sns/latest/dg/fifo-message-dedup.html.""", ) println("Would you like to use content-based deduplication instead of entering a deduplication ID? (y/n)") duplication = input.nextLine() if (duplication.compareTo("y") == 0) { println("Enter a group id value") groupId = input.nextLine() } else { println("Enter deduplication Id value") deduplicationID = input.nextLine() println("Enter a group id value") groupId = input.nextLine() } } println(DASHES) println(DASHES) println("2. Create a topic.") println("Enter a name for your SNS topic.") topicName = input.nextLine() if (selectFIFO) { println("Because you have selected a FIFO topic, '.fifo' must be appended to the topic name.") topicName = "$topicName.fifo" println("The name of the topic is $topicName") topicArn = createFIFO(topicName, duplication) println("The ARN of the FIFO topic is $topicArn") } else { println("The name of the topic is $topicName") topicArn = createSNSTopic(topicName) println("The ARN of the non-FIFO topic is $topicArn") } println(DASHES) println(DASHES) println("3. Create an SQS queue.") println("Enter a name for your SQS queue.") sqsQueueName = input.nextLine() if (selectFIFO) { sqsQueueName = "$sqsQueueName.fifo" } sqsQueueUrl = createQueue(sqsQueueName, selectFIFO) println("The queue URL is $sqsQueueUrl") println(DASHES) println(DASHES) println("4. Get the SQS queue ARN attribute.") sqsQueueArn = getSQSQueueAttrs(sqsQueueUrl) println("The ARN of the new queue is $sqsQueueArn") println(DASHES) println(DASHES) println("5. Attach an IAM policy to the queue.") // Define the policy to use. val policy = """{ "Statement": [ { "Effect": "Allow", "Principal": { "Service": "sns.amazonaws.com" }, "Action": "sqs:SendMessage", "Resource": "$sqsQueueArn", "Condition": { "ArnEquals": { "aws:SourceArn": "$topicArn" } } } ] }""" setQueueAttr(sqsQueueUrl, policy) println(DASHES) println(DASHES) println("6. Subscribe to the SQS queue.") if (selectFIFO) { println( """If you add a filter to this subscription, then only the filtered messages will be received in the queue. For information about message filtering, see https://docs.aws.amazon.com/sns/latest/dg/sns-message-filtering.html For this example, you can filter messages by a "tone" attribute.""", ) println("Would you like to filter messages for $sqsQueueName's subscription to the topic $topicName? (y/n)") val filterAns: String = input.nextLine() if (filterAns.compareTo("y") == 0) { var moreAns = false println("You can filter messages by using one or more of the following \"tone\" attributes.") println("1. cheerful") println("2. funny") println("3. serious") println("4. sincere") while (!moreAns) { println("Select a number or choose 0 to end.") val ans: String = input.nextLine() when (ans) { "1" -> filterList.add("cheerful") "2" -> filterList.add("funny") "3" -> filterList.add("serious") "4" -> filterList.add("sincere") else -> moreAns = true } } } } subscriptionArn = subQueue(topicArn, sqsQueueArn, filterList) println(DASHES) println(DASHES) println("7. Publish a message to the topic.") if (selectFIFO) { println("Would you like to add an attribute to this message? (y/n)") val msgAns: String = input.nextLine() if (msgAns.compareTo("y") == 0) { println("You can filter messages by one or more of the following \"tone\" attributes.") println("1. cheerful") println("2. funny") println("3. serious") println("4. sincere") println("Select a number or choose 0 to end.") val ans: String = input.nextLine() msgAttValue = when (ans) { "1" -> "cheerful" "2" -> "funny" "3" -> "serious" else -> "sincere" } println("Selected value is $msgAttValue") } println("Enter a message.") message = input.nextLine() pubMessageFIFO(message, topicArn, msgAttValue, duplication, groupId, deduplicationID) } else { println("Enter a message.") message = input.nextLine() pubMessage(message, topicArn) } println(DASHES) println(DASHES) println("8. Display the message. Press any key to continue.") input.nextLine() messageList = receiveMessages(sqsQueueUrl, msgAttValue) if (messageList != null) { for (mes in messageList) { println("Message Id: ${mes.messageId}") println("Full Message: ${mes.body}") } } println(DASHES) println(DASHES) println("9. Delete the received message. Press any key to continue.") input.nextLine() if (messageList != null) { deleteMessages(sqsQueueUrl, messageList) } println(DASHES) println(DASHES) println("10. Unsubscribe from the topic and delete the queue. Press any key to continue.") input.nextLine() unSub(subscriptionArn) deleteSQSQueue(sqsQueueName) println(DASHES) println(DASHES) println("11. Delete the topic. Press any key to continue.") input.nextLine() deleteSNSTopic(topicArn) println(DASHES) println(DASHES) println("The SNS/SQS workflow has completed successfully.") println(DASHES) } suspend fun deleteSNSTopic(topicArnVal: String?) { val request = DeleteTopicRequest { topicArn = topicArnVal } SnsClient { region = "us-east-1" }.use { snsClient -> snsClient.deleteTopic(request) println("$topicArnVal was deleted") } } suspend fun deleteSQSQueue(queueNameVal: String) { val getQueueRequest = GetQueueUrlRequest { queueName = queueNameVal } SqsClient { region = "us-east-1" }.use { sqsClient -> val queueUrlVal = sqsClient.getQueueUrl(getQueueRequest).queueUrl val deleteQueueRequest = DeleteQueueRequest { queueUrl = queueUrlVal } sqsClient.deleteQueue(deleteQueueRequest) println("$queueNameVal was successfully deleted.") } } suspend fun unSub(subscripArn: String?) { val request = UnsubscribeRequest { subscriptionArn = subscripArn } SnsClient { region = "us-east-1" }.use { snsClient -> snsClient.unsubscribe(request) println("Subscription was removed for $subscripArn") } } suspend fun deleteMessages(queueUrlVal: String?, messages: List<Message>) { val entriesVal: MutableList<DeleteMessageBatchRequestEntry> = mutableListOf() for (msg in messages) { val entry = DeleteMessageBatchRequestEntry { id = msg.messageId } entriesVal.add(entry) } val deleteMessageBatchRequest = DeleteMessageBatchRequest { queueUrl = queueUrlVal entries = entriesVal } SqsClient { region = "us-east-1" }.use { sqsClient -> sqsClient.deleteMessageBatch(deleteMessageBatchRequest) println("The batch delete of messages was successful") } } suspend fun receiveMessages(queueUrlVal: String?, msgAttValue: String): List<Message>? { if (msgAttValue.isEmpty()) { val request = ReceiveMessageRequest { queueUrl = queueUrlVal maxNumberOfMessages = 5 } SqsClient { region = "us-east-1" }.use { sqsClient -> return sqsClient.receiveMessage(request).messages } } else { val receiveRequest = ReceiveMessageRequest { queueUrl = queueUrlVal waitTimeSeconds = 1 maxNumberOfMessages = 5 } SqsClient { region = "us-east-1" }.use { sqsClient -> return sqsClient.receiveMessage(receiveRequest).messages } } } suspend fun pubMessage(messageVal: String?, topicArnVal: String?) { val request = PublishRequest { message = messageVal topicArn = topicArnVal } SnsClient { region = "us-east-1" }.use { snsClient -> val result = snsClient.publish(request) println("${result.messageId} message sent.") } } suspend fun pubMessageFIFO( messageVal: String?, topicArnVal: String?, msgAttValue: String, duplication: String, groupIdVal: String?, deduplicationID: String?, ) { // Means the user did not choose to use a message attribute. if (msgAttValue.isEmpty()) { if (duplication.compareTo("y") == 0) { val request = PublishRequest { message = messageVal messageGroupId = groupIdVal topicArn = topicArnVal } SnsClient { region = "us-east-1" }.use { snsClient -> val result = snsClient.publish(request) println(result.messageId.toString() + " Message sent.") } } else { val request = PublishRequest { message = messageVal messageDeduplicationId = deduplicationID messageGroupId = groupIdVal topicArn = topicArnVal } SnsClient { region = "us-east-1" }.use { snsClient -> val result = snsClient.publish(request) println(result.messageId.toString() + " Message sent.") } } } else { val messAttr = aws.sdk.kotlin.services.sns.model.MessageAttributeValue { dataType = "String" stringValue = "true" } val mapAtt: Map<String, aws.sdk.kotlin.services.sns.model.MessageAttributeValue> = mapOf(msgAttValue to messAttr) if (duplication.compareTo("y") == 0) { val request = PublishRequest { message = messageVal messageGroupId = groupIdVal topicArn = topicArnVal } SnsClient { region = "us-east-1" }.use { snsClient -> val result = snsClient.publish(request) println(result.messageId.toString() + " Message sent.") } } else { // Create a publish request with the message and attributes. val request = PublishRequest { topicArn = topicArnVal message = messageVal messageDeduplicationId = deduplicationID messageGroupId = groupIdVal messageAttributes = mapAtt } SnsClient { region = "us-east-1" }.use { snsClient -> val result = snsClient.publish(request) println(result.messageId.toString() + " Message sent.") } } } } // Subscribe to the SQS queue. suspend fun subQueue(topicArnVal: String?, queueArnVal: String, filterList: List<String?>): String? { val request: SubscribeRequest if (filterList.isEmpty()) { // No filter subscription is added. request = SubscribeRequest { protocol = "sqs" endpoint = queueArnVal returnSubscriptionArn = true topicArn = topicArnVal } SnsClient { region = "us-east-1" }.use { snsClient -> val result = snsClient.subscribe(request) println( "The queue " + queueArnVal + " has been subscribed to the topic " + topicArnVal + "\n" + "with the subscription ARN " + result.subscriptionArn, ) return result.subscriptionArn } } else { request = SubscribeRequest { protocol = "sqs" endpoint = queueArnVal returnSubscriptionArn = true topicArn = topicArnVal } SnsClient { region = "us-east-1" }.use { snsClient -> val result = snsClient.subscribe(request) println("The queue $queueArnVal has been subscribed to the topic $topicArnVal with the subscription ARN ${result.subscriptionArn}") val attributeNameVal = "FilterPolicy" val gson = Gson() val jsonString = "{\"tone\": []}" val jsonObject = gson.fromJson(jsonString, JsonObject::class.java) val toneArray = jsonObject.getAsJsonArray("tone") for (value: String? in filterList) { toneArray.add(JsonPrimitive(value)) } val updatedJsonString: String = gson.toJson(jsonObject) println(updatedJsonString) val attRequest = SetSubscriptionAttributesRequest { subscriptionArn = result.subscriptionArn attributeName = attributeNameVal attributeValue = updatedJsonString } snsClient.setSubscriptionAttributes(attRequest) return result.subscriptionArn } } } suspend fun setQueueAttr(queueUrlVal: String?, policy: String) { val attrMap: MutableMap<String, String> = HashMap() attrMap[QueueAttributeName.Policy.toString()] = policy val attributesRequest = SetQueueAttributesRequest { queueUrl = queueUrlVal attributes = attrMap } SqsClient { region = "us-east-1" }.use { sqsClient -> sqsClient.setQueueAttributes(attributesRequest) println("The policy has been successfully attached.") } } suspend fun getSQSQueueAttrs(queueUrlVal: String?): String { val atts: MutableList<QueueAttributeName> = ArrayList() atts.add(QueueAttributeName.QueueArn) val attributesRequest = GetQueueAttributesRequest { queueUrl = queueUrlVal attributeNames = atts } SqsClient { region = "us-east-1" }.use { sqsClient -> val response = sqsClient.getQueueAttributes(attributesRequest) val mapAtts = response.attributes if (mapAtts != null) { mapAtts.forEach { entry -> println("${entry.key} : ${entry.value}") return entry.value } } } return "" } suspend fun createQueue(queueNameVal: String?, selectFIFO: Boolean): String? { println("\nCreate Queue") if (selectFIFO) { val attrs = mutableMapOf<String, String>() attrs[QueueAttributeName.FifoQueue.toString()] = "true" val createQueueRequest = CreateQueueRequest { queueName = queueNameVal attributes = attrs } SqsClient { region = "us-east-1" }.use { sqsClient -> sqsClient.createQueue(createQueueRequest) println("\nGet queue url") val urlRequest = GetQueueUrlRequest { queueName = queueNameVal } val getQueueUrlResponse = sqsClient.getQueueUrl(urlRequest) return getQueueUrlResponse.queueUrl } } else { val createQueueRequest = CreateQueueRequest { queueName = queueNameVal } SqsClient { region = "us-east-1" }.use { sqsClient -> sqsClient.createQueue(createQueueRequest) println("Get queue url") val urlRequest = GetQueueUrlRequest { queueName = queueNameVal } val getQueueUrlResponse = sqsClient.getQueueUrl(urlRequest) return getQueueUrlResponse.queueUrl } } } suspend fun createSNSTopic(topicName: String?): String? { val request = CreateTopicRequest { name = topicName } SnsClient { region = "us-east-1" }.use { snsClient -> val result = snsClient.createTopic(request) return result.topicArn } } suspend fun createFIFO(topicName: String?, duplication: String): String? { val topicAttributes: MutableMap<String, String> = HashMap() if (duplication.compareTo("n") == 0) { topicAttributes["FifoTopic"] = "true" topicAttributes["ContentBasedDeduplication"] = "false" } else { topicAttributes["FifoTopic"] = "true" topicAttributes["ContentBasedDeduplication"] = "true" } val topicRequest = CreateTopicRequest { name = topicName attributes = topicAttributes } SnsClient { region = "us-east-1" }.use { snsClient -> val response = snsClient.createTopic(topicRequest) return response.topicArn } }