Azure Combat: How to create a cloud Queue

Source: Internet
Author: User
Keywords Xml name
Tags access account information application application integration applications cloud cloud applications code

Azure Queue provides a simple asynchronous job-scheduling mechanism to connect different components of cloud applications via Azure queue, with the advantages of high availability, durability, and performance, which provides a rest interface that allows non-C # Language-written applications access the Queue from anywhere, making cloud applications and local application integration no longer difficult, azure,queue can be used in both cloud applications and local applications, with two main uses:

· Message Communication Bus

· Component or Function module decoupling

You can create a queue from the W orker role or Web role, in order to experience collaboration between the worker role and the web role, we create a queue from the worker role.

Add a worker role to the cloud services solution

As shown in Figure 1, adding a worker role and associating with the service, this worker role defines responsibilities including:

· Retrieving account information from configuration

· Create a queue storage container from cloud storage

Creating a queue in the queue store


Figure 1 Inserting a worker role for queue initialization



Create a queue storage container and queue from the worker role's startup handler

Figure 2 shows the data object model for the Azure queue store, which is used to map the queue name to a unique queue ID, which is a foreign key to the Queuecontainer and the message table (ACCTQUEUECONTAINERMAP).


Figure 2 Azure queue data object model



Note that the name of the queue can only be letters and numbers, and is case-sensitive, accepting only lowercase names.

Before creating a queue, you must instantiate the Queuestorage, Queuestorage accept an account information as a parameter, in the development environment, account information can be hard-coded into the configuration file, and the following code shows the contents of the configuration file for queue storage. Include account number and HTTP port:

<appSettings>


<add key = "AccountName" value= "Devstoreaccount1"/>


<add key = "Accountsharedkey" value= "<ACCOUNT_KEY>"/>


<add key= "Queuestorageendpoint" value= http://127.0.0.1:10001 "/>"


</appSettings>

Create a queue using account information from the configuration file

The following code intercepts the implementation of the worker role project, where the bold section shows how to create a queue.

using System;


using System.Threading;


using Microsoft.ServiceHosting.ServiceRuntime;


using Microsoft.Samples.ServiceHosting.StorageClient;


using System.IO;


using System.Configuration;


using System.Net;


using System.Xml;


namespace Cloudtablestorageservice_workerrole


{


public class Workerrole:roleentrypoint


{


Public Const string xml_payload_queue_name = "Createxmlmessagequeue";


Public Const string xml_container_name = "Xmlpayload";


Private Stream Createxmlstreamblob (Byte [] bytedata)


{


return new MemoryStream (bytedata);


}


public override void Start ()


{


queuestorage queuestorage =


queuestorage.create (Storageaccountinfo


. Getdefaultqueuestorageaccountfromconfiguration ());


MessageQueue queue = Queuestorage.getqueue (xml_payload_queue_name);


bool containerandqueuecreated = false;


while (!containerandqueuecreated)


{


Try


{


queue. Createqueue ();


containerandqueuecreated = true;


}


catch (WebException e)


{


if (e.status = = webexceptionstatus.connectfailure)


{


Rolemanager.writetolog (


"Error",


String. Format ("Connect failure! The most likely cited is that


The local Development Storage tool are not running or your Storage account revisit is


incorrect. " +


"message: ' {0} '", E.message)


);


System.Threading.Thread.Sleep (5000);


}


Else


{


throw;


}


}


}


while (true)


{


Try


{


Message msg = queue. GetMessage ();


if (msg!= null)


{


string path = Msg. Contentasstring ();


Rolemanager.writetolog ("Information",


string. Format ("done with ' {0} '", path));


}


Else


{


Thread.Sleep (1000);


}


}


catch (storageexception e)


{


Rolemanager.writetolog (


"Error",


string. Format ("Exception when 處理 queue item. Message: ' {0} ',


e.message)


);


}


}


}


public override Rolestatus Gethealthstatus ()


{


//This is a sample worker implementation. Replace with your logic.


return rolestatus.healthy;


}


}


}

Creating a queue by programming

The following code shows the use of the C # class instead of reading the information in the configuration file to create a queue, which can replace the previous bold part of the code.

string accountname = "Devstoreaccount1";


string accountkey = "<ACCOUNT_KEY>";


string address = "http://127.0.0.1:10001";


Storageaccountinfo accountinfo =


New Storageaccountinfo (New Uri (address), NULL, AccountName, Accountkey);


queuestorage queuestorage = queuestorage.create (AccountInfo);


MessageQueue MessageQueue = Queuestorage.getqueue (xml_payload_queue_name);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.