Skip to content
This repository has been archived by the owner on Nov 9, 2024. It is now read-only.

Queue Storage

pofallon edited this page Aug 11, 2012 · 12 revisions

The queue storage API functionality can be put into two groups: (1) methods provided with 'storage' that allow manipulation of the queues themselves and (2) methods on the 'queue' object used to manipulate messages on the queue.

Queue-related 'storage' methods

These methods are accessible via the storage object. In reality, the functionality is found in static methods of the Table object -- these proxy methods are provided for convenience.

storage.createQueue(queueName[, callback])

Creates a new queue with the supplied queueName on this storage account. Optional callback is invoked with a reference to the newly created queue as the second argument.

storage.listQueues([callback])

Lists all queues in this storage account. Optional 'prefix' will return only those queues starting with prefix. Passing the option {limit:n} will return only the first n queues. Optional callback is invoked with an array of container names as the second argument.

With no callback, an EventEmitter is immediately returned which will emit a 'data' event with each queue name and an 'end' event with a total count of queues.

For example, a simple way to return a count of queues:

storage.listQueues().on('end', function(err, count) {
  console.log(count);
});

storage.removeQueue(queueName[, callback])

Removes the queue specified by queueName. Optional callback is invoked with any errors.

storage.queue(queueName)

Returns a reference to a queue (the same as returned by storage.createQueue).

Queue instance methods

The createQueue and queue methods above return an instance of the Queue object. This object provides the following methods:

queue.put(message[, callback])

Puts a message on the queue. Optional callback is invoked with any errors.

queue.peek(callback)

Checks whether there is a message waiting on the queue. If there is a message, it is passed as the second parameter to callback without being removed from the queue. The contents of the message are in message.body.

queue.get(callback)

Checks whether there is a message waiting on the queue. If there is a message, it is passed to callback as the second parameter and marked 'invisible' for the default period of time. A 'done' function is passed as the third parameter to callback. Invoking this function will remove the message from the queue. This 'done' function obeys the 'error' parameter semantics and will only remove the message from the queue if the first parameter to the function is falsy. This means you can pass the 'done' function as a callback to a function you invoke within the queue.get callback. For example:

queue.get(function(err, msg, done) {
  doSomethingWithMessage(msg, done);
}

queue.del(id, receipt[, callback])

queue.del(message[, callback])

Removes the message (identified by the id and receipt properties of the message, or simply the message itself) from the queue. Optional callback is invoked with any errors.

queue.poll([options, ]t)

This will check the queue for a new message every t milliseconds. If a message is found, the queue will emit a message event that contains the message and a 'done' function. This 'done' function works exactly like the one provided by queue.get above. To stop polling for new messages, call poll(0).

queue.clear([callback])

Clears the queue of all messages. Optional callback is invoked with any errors.

queue.service()

Returns the underlying BlobService object from the Azure SDK.