title | description | services | documentationcenter | author | manager | editor | ms.assetid | ms.service | ms.workload | ms.tgt_pltfrm | ms.devlang | ms.topic | ms.date | ms.author |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
How to use Queue storage from Ruby | Microsoft Docs |
Learn how to use the Azure Queue service to create and delete queues, and insert, get, and delete messages. Samples written in Ruby. |
storage |
ruby |
robinsh |
timlt |
tysonn |
59c2d81b-db9c-46ee-ade2-2f0caae6b1e6 |
storage |
storage |
na |
ruby |
article |
12/08/2016 |
robinsh |
[!INCLUDE storage-selector-queue-include]
[!INCLUDE storage-try-azure-tools-queues]
This guide shows you how to perform common scenarios using the Microsoft Azure Queue Storage service. The samples are written using the Ruby Azure API. The scenarios covered include inserting, peeking, getting, and deleting queue messages, as well as creating and deleting queues.
[!INCLUDE storage-queue-concepts-include]
[!INCLUDE storage-create-account-include]
Create a Ruby application. For instructions, see Ruby on Rails Web application on an Azure VM.
To use Azure storage, you need to download and use the Ruby azure package, which includes a set of convenience libraries that communicate with the storage REST services.
- Use a command-line interface such as PowerShell (Windows), Terminal (Mac), or Bash (Unix).
- Type "gem install azure" in the command window to install the gem and dependencies.
Use your favorite text editor, add the following to the top of the Ruby file where you intend to use storage:
require "azure"
The azure module will read the environment variables AZURE_STORAGE_ACCOUNT and AZURE_STORAGE_ACCESS_KEY for information required to connect to your Azure storage account. If these environment variables are not set, you must specify the account information before using Azure::QueueService with the following code:
Azure.config.storage_account_name = "<your azure storage account>"
Azure.config.storage_access_key = "<your Azure storage access key>"
To obtain these values from a classic or Resource Manager storage account in the Azure portal:
- Log in to the Azure portal.
- Navigate to the storage account you want to use.
- In the Settings blade on the right, click Access Keys.
- In the Access keys blade that appears, you'll see the access key 1 and access key 2. You can use either of these.
- Click the copy icon to copy the key to the clipboard.
To obtain these values from a classic storage account in the classic Azure portal:
- Log in to the classic Azure portal.
- Navigate to the storage account you want to use.
- Click MANAGE ACCESS KEYS at the bottom of the navigation pane.
- In the pop up dialog, you'll see the storage account name, primary access key and secondary access key. For access key, you can use either the primary one or the secondary one.
- Click the copy icon to copy the key to the clipboard.
The following code creates a Azure::QueueService object, which enables you to work with queues.
azure_queue_service = Azure::QueueService.new
Use the create_queue() method to create a queue with the specified name.
begin
azure_queue_service.create_queue("test-queue")
rescue
puts $!
end
To insert a message into a queue, use the create_message() method to create a new message and add it to the queue.
azure_queue_service.create_message("test-queue", "test message")
You can peek at the message in the front of a queue without removing it from the queue by calling the peek_messages() method. By default, peek_messages() peeks at a single message. You can also specify how many messages you want to peek.
result = azure_queue_service.peek_messages("test-queue",
{:number_of_messages => 10})
You can remove a message from a queue in two steps.
- When you call list_messages(), you get the next message in a queue by default. You can also specify how many messages you want to get. The messages returned from list_messages() becomes invisible to any other code reading messages from this queue. You pass in the visibility timeout in seconds as a parameter.
- To finish removing the message from the queue, you must also call delete_message().
This two-step process of removing a message assures that when your code fails to process a message due to hardware or software failure, another instance of your code can get the same message and try again. Your code calls delete_message() right after the message has been processed.
messages = azure_queue_service.list_messages("test-queue", 30)
azure_queue_service.delete_message("test-queue",
messages[0].id, messages[0].pop_receipt)
You can change the contents of a message in-place in the queue. The code below uses the update_message() method to update a message. The method will return a tuple which contains the pop receipt of the queue message and a UTC date time value that represents when the message will be visible on the queue.
message = azure_queue_service.list_messages("test-queue", 30)
pop_receipt, time_next_visible = azure_queue_service.update_message(
"test-queue", message.id, message.pop_receipt, "updated test message",
30)
There are two ways you can customize message retrieval from a queue.
- You can get a batch of message.
- You can set a longer or shorter invisibility timeout, allowing your code more or less time to fully process each message.
The following code example uses the list_messages() method to get 15 messages in one call. Then it prints and deletes each message. It also sets the invisibility timeout to five minutes for each message.
azure_queue_service.list_messages("test-queue", 300
{:number_of_messages => 15}).each do |m|
puts m.message_text
azure_queue_service.delete_message("test-queue", m.id, m.pop_receipt)
end
You can get an estimation of the number of messages in the queue. The get_queue_metadata() method asks the queue service to return the approximate message count and metadata about the queue.
message_count, metadata = azure_queue_service.get_queue_metadata(
"test-queue")
To delete a queue and all the messages contained in it, call the delete_queue() method on the queue object.
azure_queue_service.delete_queue("test-queue")
Now that you've learned the basics of queue storage, follow these links to learn about more complex storage tasks.
- Visit the Azure Storage Team Blog
- Visit the Azure SDK for Ruby repository on GitHub
For a comparison between the Azure Queue Service discussed in this article and Azure Service Bus Queues discussed in the How to use Service Bus Queues article, see Azure Queues and Service Bus Queues - Compared and Contrasted