Skip to content

Latest commit

 

History

History
executable file
·
182 lines (127 loc) · 7.03 KB

sqs-examples-using-queues.md

File metadata and controls

executable file
·
182 lines (127 loc) · 7.03 KB

Using Queues in Amazon SQS

[JavaScript code example that applies to Node.js execution]

This Node.js code example shows:

  • How to get a list of all of your message queues

  • How to obtain the URL for a particular queue

  • How to create and delete queues

The Scenario

In this example, a series of Node.js modules are used to work with queues. The Node.js modules use the SDK for JavaScript to use queues using these methods of the AWS.SQS client class:

For more information about Amazon SQS messages, see How Queues Work in the Amazon Simple Queue Service Developer Guide.

Prerequisite Tasks

To set up and run this example, you must first complete these tasks:

Configuring the SDK

Configure the SDK for JavaScript by creating a global configuration object then setting the region for your code. In this example, the region is set to us-west-2.

// Load the SDK for JavaScript
var AWS = require('aws-sdk');
// Set the region 
AWS.config.update({region: 'us-west-2'});

Listing Your Queues

Create a Node.js module with the file name sqs_listqueues.js. Be sure to configure the SDK as previously shown. To access Amazon SQS, create an AWS.SQS service object. Create a JSON object containing the parameters needed to list your queues, which by default is an empty object. Call the listQueues method to retrieve the list of queues. The callback returns the URLs of all queues.

// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region 
AWS.config.update({region: 'REGION'});

// Create an SQS service object
var sqs = new AWS.SQS({apiVersion: '2012-11-05'});

var params = {};

sqs.listQueues(params, function(err, data) {
  if (err) {
    console.log("Error", err);
  } else {
    console.log("Success", data.QueueUrls);
  }
});

To run the example, type the following at the command line.

node sqs_listqueues.js

This sample code can be found here on GitHub.

Creating a Queue

Create a Node.js module with the file name sqs_createqueue.js. Be sure to configure the SDK as previously shown. To access Amazon SQS, create an AWS.SQS service object. Create a JSON object containing the parameters needed to list your queues, which must include the name for the queue created. The parameters can also contain attributes for the queue, such as the number of seconds for which message delivery is delayed or the number of seconds to retain a received message. Call the createQueue method. The callback returns the URL of the created queue.

// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region 
AWS.config.update({region: 'REGION'});

// Create an SQS service object
var sqs = new AWS.SQS({apiVersion: '2012-11-05'});

var params = {
  QueueName: 'SQS_QUEUE_NAME',
  Attributes: {
    'DelaySeconds': '60',
    'MessageRetentionPeriod': '86400'
  }
};

sqs.createQueue(params, function(err, data) {
  if (err) {
    console.log("Error", err);
  } else {
    console.log("Success", data.QueueUrl);
  }
});

To run the example, type the following at the command line.

node sqs_createqueue.js

This sample code can be found here on GitHub.

Getting the URL for a Queue

Create a Node.js module with the file name sqs_getqueueurl.js. Be sure to configure the SDK as previously shown. To access Amazon SQS, create an AWS.SQS service object. Create a JSON object containing the parameters needed to list your queues, which must include the name of the queue whose URL you want. Call the getQueueUrl method. The callback returns the URL of the specified queue.

// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region 
AWS.config.update({region: 'REGION'});

// Create an SQS service object
var sqs = new AWS.SQS({apiVersion: '2012-11-05'});

var params = {
  QueueName: 'SQS_QUEUE_NAME'
};

sqs.getQueueUrl(params, function(err, data) {
  if (err) {
    console.log("Error", err);
  } else {
    console.log("Success", data.QueueUrl);
  }
});

To run the example, type the following at the command line.

node sqs_getqueueurl.js

This sample code can be found here on GitHub.

Deleting a Queue

Create a Node.js module with the file name sqs_deletequeue.js. Be sure to configure the SDK as previously shown. To access Amazon SQS, create an AWS.SQS service object. Create a JSON object containing the parameters needed to delete a queue, which consists of the URL of the queue you want to delete. Call the deleteQueue method.

// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region 
AWS.config.update({region: 'REGION'});

// Create an SQS service object
var sqs = new AWS.SQS({apiVersion: '2012-11-05'});

var params = {
  QueueUrl: 'SQS_QUEUE_URL'
 };

sqs.deleteQueue(params, function(err, data) {
  if (err) {
    console.log("Error", err);
  } else {
    console.log("Success", data);
  }
});

To run the example, type the following at the command line.

node sqs_deletequeue.js

This sample code can be found here on GitHub.