AWS Developer Tools Blog

Subscribing Queues to Topics

Amazon Simple Notification Service (Amazon SNS) is a terrific service for publishing notification messages and having them automatically delivered to all your subscribers. You simply send a message to your SNS topic, and it gets delivered to all the subscribers for that topic. Amazon SNS supports many different types of subscribers for topics:

  • HTTP/HTTPS endpoints
  • Email addresses (with text or JSON format messages)
  • SMS/text-message addresses
  • Amazon SQS queues

Each type of subscriber is useful, but one of the most versatile for building systems is connecting an Amazon SQS queue directly to your Amazon SNS topic. This is a really handy and common architecture pattern when building applications on the AWS platform, and for good reason. The Amazon SNS topic provides you with a common point for sending messages and having them published to a dynamically managed list of subscribers, and the Amazon SQS queue provides you with a scalable, robust storage location for those delivered messages, while your application pulls them off the queue to process them.

Now that we’ve convinced you about the value of this pattern, let’s take a look at how to execute it in code, using the AWS SDK for Java. The first thing we need to do is create our Amazon SQS queue, and our Amazon SNS topic.

AmazonSNS sns = new AmazonSNSClient(credentials);
AmazonSQS sqs = new AmazonSQSClient(credentials);

String myTopicArn = sns.createTopic(new CreateTopicRequest("topicName")).getTopicArn();
String myQueueUrl = sqs.createQueue(new CreateQueueRequest("queueName")).getQueueUrl();

In order for a queue to receive messages from a topic, it needs to be subscribed and also needs a custom security policy to allow the topic to deliver messages to the queue. The following code in the SDK handles both of these for you automatically, without you ever having to deal with the details around building that custom policy.

Topics.subscribeQueue(sns, sqs, myTopicArn, myQueueUrl);

Now that your queue is connected to your topic, you’re ready to send messages to your topic, then pull them off of your queue. Note that it may take a few moments for the queue’s policy to be updated when the queue is initially subscribed to the topic.

sns.publish(new PublishRequest(myTopicArn, "Hello SNS World").withSubject("Subject"));

List<Message> messages = sqs.receiveMessage(new ReceiveMessageRequest(myQueueUrl).getMessages();
if (messages.size() > 0) {
    byte[] decodedBytes = Base64.decodeBase64((messages.get(0)).getBody().getBytes());
    System.out.println("Message: " +  new String(decodedBytes));

For more information on using this new method to subscribe an Amazon SQS queue to an Amazon SNS topic, including an explanation of the policy that is applied to your queue, see the AWS SDK for Java API documentation for Topics.subscribeQueue(…).