Overview Of Microsoft Azure Storage – Part Five

Azure queue storage

Queues are serially accessed messages of requests, which allow Web roles and worker roles to interact. It means whenever a new message will be added in a queue, the roles interact with that queue and pull the messages from in that queue one by one and ones the message has deliver to the roles that message will delete from the Queue.

Basically queue is the list of messages, which flows from the bottom from the queue to the top of the queue. They were added to the queue. Azure queues are located in Microsoft Cloud, which you can use for exchanging messages between the components either in the Cloud or in Premise.

Queues can store a large number of messages and a single queue can be up to 64 kb in size. Azure queue is used for passing the messages to an Azure worker role from an Azure Web role.

Out of the some advantages, queue has some limitations also such as the sent message should be in either text or binary format. Along with this, the individual message size can’t be more than 8kb in size.

I have a storage account. In the storage account, I have a queue and in this queue, we have multiple messages. We can have more than one message in a queue.

Azure

Let’s understand storage accounts practically. You have to be ready with the setup procedure given below.

  • Visual Studio 2015.
  • Microsoft Azure account.

With Azure storage account, you need to create a queue, which is a set of messages. Thus, let’s follow the steps given below.

Step 1

Create ASP.NET Web Project and a give a name to your project.

Step 2

Select MVC template.

Step 3

Right click on Project and add on Connected Services to connect with your Azure Storage account.

Let’s create an Empty Controller class in to your solution by right clicking Controllers folder.

Azure

Create a Method under your Controller and parse the connection string to retrieve your account detail from the key, which is in Web.Config file.

Create a client for your queue by passing the reference of storage account. As you have retrieved the reference of your queue by passing a queue name, createIfNotExists method will create your queue, if it does not exist and hold it in ViewBag.

  1. public class QueueStorageController : Controller
  2.     {
  3.         // GET: QueueStorage
  4.         public ActionResult CreateQueue()
  5.         {
  6.             CloudStorageAccount yourStorageAccount = CloudStorageAccount.Parse(
  7.         CloudConfigurationManager.GetSetting(“mystoragecontainer002_AzureStorageConnectionString”));
  8.             CloudQueueClient myQueueClient = yourStorageAccount.CreateCloudQueueClient();
  9.             CloudQueue myQueue = myQueueClient.GetQueueReference(“storagequeue”);
  10.             ViewBag.CreateSucess = myQueue.CreateIfNotExists();
  11.             ViewBag.YourQueueName = myQueue.Name;
  12.             return View();
  13.         }

Add namespaces given below into your Controller.

  1. using Microsoft.Azure;
  2. using Microsoft.WindowsAzure.Storage;
  3. using Microsoft.WindowsAzure.Storage.Table;

Add a View for this Method.

Azure

Give a name to it and click Add.

Azure

Write the script given below to show a message on the Browser.

  1. @{
  2.     ViewBag.Title = “CreateQueue”;
  3.     Layout = “~/Views/Shared/_Layout.cshtml”;
  4. }
  5. <h2>Creating Queue</h2>
  6. <h3>
  7.     Hey Your Queue <b style=“color:red”“@ViewBag.YourQueueName” </b>
  8.     @(ViewBag.CreateSuccess == true ? “Has Been Successfully Created.”“Already Exist Into Your Storage Account.☻”)
  9. </h3><br />

Afterwards, go to _Layout.cshtml and create a list item with Action Link to create queue.

Azure

Click Queues on Azure portal.

Azure

When you open it, there will not be any queue.

Azure

Click Action link to create a queue on Azure storage, which you have retrieved in your code.

Azure

The screenshot given below shows that the queue has been created into the storage account.

Azure

Check in Visual Studio, if it is created or not. Refresh Queue folder under storage account and you’ll get queue.

Azure

There is no message in the queue.

Azure

If you try to create once again with the same name, then it shows a message that queue already exists.

Azure

You can check your queue in Azure portal.

Azure

Insert a message into a queue

After creating queue, I want to insert a message into it. To insert a message into queue, create a method and write all the code of CreateQueue method because in the current method, I need the reference of the queue.

Now, create a message and add this message into your queue. AddMessage method will insert your message, which you write.

  1. public ActionResult insertMessageinToQueue()
  2.         {
  3.             CloudStorageAccount yourStorageAccount = CloudStorageAccount.Parse(
  4.         CloudConfigurationManager.GetSetting(“mystoragecontainer002_AzureStorageConnectionString”));
  5.             CloudQueueClient myQueueClient = yourStorageAccount.CreateCloudQueueClient();
  6.             CloudQueue myQueue = myQueueClient.GetQueueReference(“storagequeue”);
  7.             ViewBag.CreateSuccess = myQueue.CreateIfNotExists();
  8.             ViewBag.YourQueueName = myQueue.Name;
  9.             CloudQueueMessage insertMessage = new CloudQueueMessage(“Hello, Azure Storage Queue”);
  10.             myQueue.AddMessage(insertMessage);
  11.             return View();
  12.         }

Add View for this method and write the script given below to show the users that the message is inserted.

  1. @{
  2.     ViewBag.Title = “insertMessageinToQueue”;
  3.     Layout = “~/Views/Shared/_Layout.cshtml”;
  4. }
  5. <h2>Insert Your Message Into Queue</h2>
  6. <h3>The Message has Successfully Inserted into <b style=“color:red”>“@ViewBag.YourQueueName”</b> Table </h3>

Create an action link in CreateQueue.Chtml.

Azure

Run your Application and when you click Create Queue, the queue will not create because it is already inserted. Click on the link.

Azure

The message has successfully inserted into your queue.

Azure
Check it into your queue in Visual Studio. A message will be inserted and by default, it is an Id.

Azure

You can delete your queue by calling a myQueue.Delete().

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: