AWS Developer Tools Blog

AWS Resource APIs for SNS and SQS

Last week we released version 0.0.3 of the AWS Resource APIs for Java, adding support for the Amazon Simple Queue Service (SQS) and Amazon Simple Notification Service (SNS). SNS and SQS are similar services that together provide a fully-managed cloud messaging platform. These services expose two powerful primitives — Topics and Queues — which let you decouple message producers from message consumers. The Resource APIs for SNS and SQS make it easier than ever to use these two services. Enough chit-chat, let’s see some code!

Amazon SNS — Topics

SNS is used for multicast messaging. Consumers subscribe to a “Topic,” and messages published to the Topic are pushed to all current subscribers. The resource API for SNS exposes a resource object representing a Topic, giving you convenient methods for managing subscriptions to the topic and publishing messages to the topic. It also exposes resource objects for PlatformApplications and PlatformEndpoints, which are used to integrate with various mobile push services. This example demonstrates creating a new Topic, adding a couple of subscribers, and publishing a message to the topic.

SNS sns = ServiceBuilder.forService(SNS.class).build();

// Create a new topic.
Topic topic = sns.createTopic("MyTestTopic");
try {

    // Subscribe an email address.
    topic.subscribe("david@example.com", "email");

    // Subscribe an HTTPS endpoint.
    topic.subscribe("https://api.example.com/notify?user=david", "https");

    // Subscribe all of the endpoints from a previously-created
    // mobile platform application.
    PlatformApplication myMobileApp =
            sns.getPlatformApplication("arn:aws:...");
    for (PlatformEndpoint endpoint : myMobileApp.getEndpoints()) {
        topic.subscribe(endpoint.getArn(), "application");
    }

    // Publish a message to all of the subscribers.
    topic.publish("Hello from Amazon SNS!");

} finally {
    // Clean up after ourselves.
    topic.delete();
}

Amazon SQS — Queues

SQS is used for reliable anycast messaging. Producers write messages to a “Queue,” and consumers pull messages from the queue; each message is delivered to a single consumer[1]. The resource API for SQS exposes a Queue resource object, giving you convenient methods for sending messages to a queue and receiving messages from the queue. This example demonstrates creating a queue, sending a couple of messages to it, and then reading those messages back out.

SQS sqs = ServiceBuilder.forService(SQS.class).build();

// Create a new queue.
Queue queue = sqs.createQueue("MyTestQueue");
try {

    // Configure the queue for more efficient long-polling.
    queue.setAttributes(Collections.singletonMap(
            "ReceiveMessageWaitTimeSeconds",
            "20"));

    // Send it a couple messages.
    for (int i = 0; i < 10; ++i) {
        queue.sendMessage("Hello from Amazon SQS: " + i);
    }

    while (true) {
        // Pull a batch of messages from the queue for processing.
        List<Message> messages = queue.receiveMessages();
        for (Message message : messages) {
            System.out.println(message.getBody());

            // Delete the message from the queue to acknowledge that
            // we've successfully processed it.
            message.delete();
        }
    }

} finally {
    // Clean up after ourselves.
    queue.delete();
}

Conclusion

Using SNS or SQS, or interested in getting started with them? Give these new resource APIs a try and let us know what you think, either here or via GitHub issues!

[1] To be precise, it’s delivered to at least one consumer; if the first consumer who reads it does not delete the message from the queue in time (whether due to failure or just being slow), it’ll eventually be delivered to another consumer.