One of the previous explorations was about the file system storage of azure services, we saw localstorage and ad hoc, and also in that article we covered two more important models and roles in the Azure programming model, WEB role and worker Role, to understand the relationship between the web and the worker, is the key to using and mastering queue storage.
According to the official MSDN definition:
Web role:a Web role are A Web creator accessible via an HTTP and/or A HTTPS endpoint. A web role was hosted in a environnement designed to support a subset of ASP.net and Windows communication Foundation (WCF) Technologies.
Worker role:a worker role is A background 處理 creator. A worker role could communicate with storage services and the other internet-based services. It does not expose any external endpoints. A worker role can read requests from a \ Tabbed in the queue service.
Simply put, a web role is a Web application that can be accessed via Http/htts, while the worker role is a background processing application that cannot be accessed through HTTPS. Generally according to the definition of Azure an azure service has at least one web role, while the worker role is optional, not necessarily. You can also understand that web role is a front-Office application, such as a familiar online store, where web role is responsible for displaying store items, searching for products, and accepting user subscriptions. When the order application is produced, it is accepted and processed, and it is processed by the background application such as worker role, such as checking whether the stock has goods, the credit condition of the user, and sending the goods to the logistics company's status update.
As the following picture shows, Webrole is responsible for selling coffee, and the worker role is the one responsible for making the coffee. In Windows Azure's vision, the coffee and the coffee maker could be two vendors in different production segments, which were hosted on Windows Azure, and for collaboration and message communication between the applications, Windows Azure can provide a good solution, such as service bus and Workflow service in. NET Services, which is for two separate applications (perhaps two cloud applications located on different clouds), between a separate coffee application , the message transfer between coffee-making and coffee can be considered using queue services or queue storage, a very typical producer-consumer model.
This is a relatively complex scenario where a single azure service requires both Web and worker roles, or even a web role, multiple background worker roles, and queue storage solves the problem of message sharing and delivery of foreground roles and background worker roles. To resolve message sharing, the message store is responsible for managing messages, and for message delivery, message reliability is the responsibility of the messaging service.
In addition to the communication between components and roles within an azure service, there is also a scenario in which you can consider using Azure queue storage and services in the context of an application mix. For example, the following example:
Pictures Source:http://blog.smarx.com/posts/windows-azure-worker-role-to-deal-with-spam
The example of Steve Marx is that he now wants to do a cloud version of the weblog application, but he also needs to address the problem of spam comments. And TypePad Antispam has already implemented a good solution for spam reviews, and TypePad Antispam also provides a public API and interface, so Steve Marx's weblog application will be able to aggregate TypePad's spam comment ability into his weblog application.
The approximate process is as follows:
1. A reader visits Steve Marx's cloud version of weblog application (Web role) and writes his own comments.
2. WEB role first saves the reader's comments in the Azure table store and sets its status to not appear in the audit.
3. Then the web role then passes the azure queue, placing a message informing a numbered comment that requires approval to see if it is malicious or spam comments
4. Worker role generally begins to work shortly after it starts, it checks the message in the queue and, once it is checked, it can find specific comments, user information, and comments about which article to use in the Azure table store, with the number in the message.
5. After that, the worker role takes this information (TypePad antispam service needs) and invokes TypePad's spam comment detection service interface to determine the validity of this comment for this user.
6. With the results of the TypePad antispam return, the Worker role updates the status of this comment in the Azure table store, such as setting, auditing through to be displayed, or auditing not showing through.
Understand the design level and use of the scene, the technical and code level is relatively simple. I modified the project in the previous article to design a simple scenario to simulate and explore the functions of azure queues.
The application interface is as follows:
Send at the front desk, responsible for generating requirements and input, and putting it in the queue, refresh reads the queue, see what requirements and inputs are not processed in the queue, and the worker role in the background reads the messages in the queue and processes them (removed from the queue)
This time we'll just create a project that contains both Web role and Worker role:
After creating the project, we need to do two things before writing the code, one is to configure and define the Azure service, and the other is to determine the components that access/Access Azure queue storage.
In the current azure environment, queue storage, table storage, and BLOB storage services provide access to rest protocols, and basically if you want to use these services, there is a component/client that accesses and accesses Azure queue storage, as well as access to the user and password.
Access to the user and password we can configure in the Azure service configuration file, while an Access/Access Azure Queue Storage component/client, now popular is directly using the SDK in the Storageclient class library, this class library in Azure Storageclientlib directory of the SDK, I like to compile it, put it in a separate directory, and add a file reference directly to the project later. The namespace is: Microsoft.Samples.ServiceHosting.StorageClient
The first is to define queue-related username and password in service definition file Servicedefinition.csdef, queue access address, because web role and work role have to access and access queues, so you need to define
<?xml version= "1.0" encoding= "Utf-8" "
<servicedefinition name=" workingwithqueues "xmlns=" http ://schemas.microsoft.com/servicehosting/2008/10/servicedefinition "
<webrole name=" WebRole ">
<configurationsettings>
<setting name= "Queuestorageendpoint"/>
<setting Name= "AccountName"/>
<setting name= "Accountsharedkey"/>
</ConfigurationSettings>
<inputendpoints>
<!--moment-in use port to HTTP and port 443 for HTTPS when running in the cloud-->
<inputendpoint name= "httpin" protocol= "http" port= "n"/>
</inputendpoints>
< /webrole>
<workerrole name= "Workerrole"
<configurationsettings>
< Setting name= "Queuestorageendpoint"/>
<setting name= "AccountName"/>
<setting name= " Accountsharedkey "/>
</configurationsettings>
</WorkerRole>
</servicedefinition>
After the service profile serviceconfiguration.cscfg, define the values for each parameter in the service definition file
<?xml version= "1.0"
<serviceconfiguration servicename= workingwithqueues "xmlns=" http:// Schemas.microsoft.com/servicehosting/2008/10/serviceconfiguration "
<role name=" WebRole "
<instances count= "1"/>
<configurationsettings>
<setting name= " Queuestorageendpoint "value=" http://127.0.0.1:10001/>
<setting name= "AccountName" Devstoreaccount1 "/>
<setting name=" Accountsharedkey "value=" eby8vdm02xnocqflquwjpllmetlcdxj1ouzft50usrz6ifsufq2uvercz4i6tq/k1szfptotr/kbhbeksogmgw== "/>
</ Configurationsettings>
</role>
<role name= "Workerrole"
<instances count= "1"/>
<configurationsettings>
<setting name= "Queuestorageendpoint value=" http:// 127.0.0.1:10001 "/>
<setting name=" AccountName "value=" Devstoreaccount1 "/>
<setting Name = "Accountsharedkey" value= "eby8vdm02xnocqflquwjpllmetlcdxj1ouzft50usrz6ifsufq2uvercz4i6tq/k1szfptotr/kbhbeksogmgw== "/>
</configurationsettings>
</role>
</serviceconfiguration>
Then add the Storageclient references to the web role and worker role projects separately
Here you can look at the main code
To create a message queue and code to add to a message:
1:storageaccountinfo account = Storageaccountinfo.getdefaultqueuestorageaccountfromconfiguration ();
2:
3:queuesvc = queuestorage.create (account);
4:
5:queue = Queuesvc.getqueue ("mymessage090120");
6:
7:if (!queue. Doesqueueexist ())
8:
9: {
10:
11:queue. Createqueue ();
12:
13:}
14:
15:if (queue!= null)
16:
17: {
18:
19:message msg = new Message (Inputbox.text);
20:
21:queue. Putmessage (msg);
22:
23:inputbox.text = "";
24:
25:}
26:
Refresh function, mainly by using the Peekmessages method to read message queues:
1:msgbox.items.clear ();
2:
3:ienumerable<message> msgs = queue. Peekmessages (10);
4:
5:if (msgs!= null)
6:
7: {
8:
9:foreach (Message item in msgs)
10:
11: {
12:
13:msgbox.items.add (item. Contentasstring ());
14:
15:}
16:
17:}
Worker role's code for processing messages:
1:public override void Start ()
2:
3: {
4:
5://Initialize the account information
6:
7:u RI BaseUri = new Uri (rolemanager.getconfigurationsetting ("Queuestorageendpoint"));
8:
9:string accountname = rolemanager.getconfigurationsetting ("AccountName");
Ten:
11:string Accountkey = rolemanager.getconfigurationsetting ("Accountsharedkey");
A:
13:storageaccountinfo account = new Storageaccountinfo (
:
15:baseuri,
:
17:null,
18:
19:accountname
:
21:accountkey)
:
//retrieve A reference to the messages queue.
:
25:queuestorage Service = queuestorage.create (account);
:
27:messagequeue queue = service. Getqueue ("");
:
29:while (True)
:
: {
:
33:thread.sleep (10000);
:
35:if (queue. Doesqueueexist ())
:
: {
:
39:message msg = queue. GetMessage ();
:
41:if (msg!= null)
:
: {
:
45:rolemanager.writetolog ("Information",
:
47:string.) Format (' message ' {0} ' processed. ', MSG. Contentasstring ());
:
49:queue. Deletemessage (msg);
:
"
":
:
:
[+:]
:
:
The last simple rule about queues
See "mymessage090120" This queue name? Here's another note I'd like to tell you, which is the naming convention for Azure Container, the queue name, the table name, which is a container (Container) label in Azure, and the container name must be a valid DNS (Domain name System) The name and observe the following rules
Rule 1: Naming rules for queues, blob containers, and table names
· Names must begin with numbers or letters, and have letters, numbers,. Symbols (period) and-symbols (dash) composition
· All letters must be lowercase
· The length of the name is between 3-63 characters
· After the. Symbol cannot be directly followed-symbol
Simply put, you must conform to this regular expression ^ ([A-z]|d) {1} ([A-z]|-|d) {1,61} ([a-z]|d) {1}$, and the other is the constraint of the table name Validtablenameregex = @ "^" ([a-z]|[ A-z]) {1} ([a-z]|[ A-Z]|D) {2,62}$ "
I didn't notice it at first, I used a capital letter, and it turned out to be a loop to find the mistake.
Rule 2: A queue can contain countless messages, each of which cannot be larger than 8K (maxmessagesize = 8 * 1024), and the maximum surviving time for messages in the queue is 7 days (maxtimetolive = 7 * 24 * 60 * 60)
Related information can be found in Queue.cs and RESThelpers.cs.
Rule 3: Queue storage is a message that follows and binds to an account in an Azure platform that contains an unlimited number of messages in the queue store. From the rest path you can see http://<account>.queue.core.windows.net/<queuename>
The
Rule 4:azure Queue Service provides a reliable, queued messaging service that ensures that messages are received at least once, is suitable for asynchronous task distribution and query scenarios, and that the queue storage itself is reliable and persistent as long as the Azure platform account is unchanged (durable Storage).