Integrate Amazon SQS with SNS

In my previous posts(Quartz Scheduler with MySQL Database and Amazon Simple Notification Service (SNS) via SMS) I shared information for integrating Quartz and Amazon SNS.

In this post I will tell you how to integrate Amazon SNS with Amazon SQS.

To do this we should know what is the use of Amazon SQS.

Amazon SQS is basically used for storing the unprocessed message in a queue. It provides a reliable, highly-scalable, hosted queue for storing messages in transit between computers.


Steps :

  • Add dependencies : check it here
  • Create Queue
  • Get Queue URL
  • Subscribe Queue with the Topic (Subscription Topic)
  • Send SMS to Topic

In my previous post I have shown you how to create Topic and subscribe phone numbers to that. So, I will directly continue with SQS and its related things.

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.sns.AmazonSNSClient;
import com.amazonaws.services.sns.model.*;
import com.amazonaws.services.sns.util.Topics;
import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.AmazonSQSClient;
import com.amazonaws.services.sqs.model.CreateQueueRequest;

import java.util.List;

/**
* Created by anuragdhunna
*/
public class AmazonSNS {
public static void main(String[] args) {

// Your Credentials
String ACCESS_KEY = "YOUR_ACCESS_KEY";
String SECRET_KEY = "YOUR_SECRET_KEY";
String topicName = "myTopic";
String message = "YOUR MESSAGE";

// Populate the list of phoneNumbers
List<String> phoneNumbers = null; // Ex: +919384374XX
AmazonSNSClient snsClient =
new AmazonSNSClient(new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY));

// Create SMS Topic
String topicArn = createSNSTopic(snsClient, topicName);

// Subcribe Phone Numbers to Topic
subscribeToTopic
(snsClient, topicArn, "sms", phoneNumbers);

String queueName = "QUEUE_NAME";

AmazonSQS sqs =
new AmazonSQSClient(new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY));

// Create Queue
String queueUrl =
sqs.createQueue(new CreateQueueRequest(queueName)).getQueueUrl();

// Subscribe Topic with Queue
Topics.subscribeQueue(snsClient, sqs, topicArn, queueUrl);

// Publish Message to Topic
sendSMSMessageToTopic
(snsClient, topicArn, message);

}

Get Queue URL of existing queue

/**
* Get Queue URL of existing Queue
*
@param queueName
* @return
*/
public String getQueueUrl(String queueName){
AmazonSQS sqs =
new AmazonSQSClient(new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY));
GetQueueUrlRequest getQueueUrlRequest = new GetQueueUrlRequest(queueName);
return sqs.getQueueUrl(getQueueUrlRequest).getQueueUrl();
}

Get List of Queues

/**
* Get List of Existing Queues
*
@return
*/
public ListQueuesResult listQueues(){
AmazonSQS sqs =
new AmazonSQSClient(new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY));
return sqs.listQueues();
}

Delete a Queue

/**
* Delete Queue
*
@param queueUrl
* @return
*/
public DeleteQueueResult deleteQueue(String queueUrl){
AmazonSQS sqs =
new AmazonSQSClient(new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY));
DeleteQueueResult asfdasdf = sqs.deleteQueue(new DeleteQueueRequest(queueUrl));
return sqs.deleteQueue(new DeleteQueueRequest(queueUrl));
}

By using Amazon SQS, you can send sms to customers in an ordered and effective way as it requires less administration and is cost-effective.

If you liked this article, give it a few claps. I would greatly appreciate it.

If you have any questions or comments, feel free to comment below or message me on linkedin.

A single golf clap? Or a long standing ovation?

By clapping more or less, you can signal to us which stories really stand out.