Windows Azure Cloud Service (PAAs) Web Role, Worker Role, Azure Storage Queue (bottom)

Source: Internet
Author: User
Tags getmessage

Windows Azure Platform Family of articles Catalog

This chapter of the demo part of the source code, please download here .

  In the previous chapter, I described how we can use the web role and worker role of Azure PAAs to handle complex business logic

-web role can quickly respond to front-end business requests and save input to the Azure Storage queue

-worker role reads data from the queue and can handle complex business logic on the backend

-As you can see, the Azure Storage queue is a bridge for front-end business logic and back-end business processing

The schema diagram can be represented by:

  

For knowledge of the Azure Storage queue, you can refer to Windows Azure Storage (1) Windows Azure Storage Service Storage Service

Next, we simulate a scenario:

1. The front-end user adds the contents of the input box to the Azure Storage queue via the Web role's ASP.

2. The back-end worker Role, through the run () function in WorkerRole.cs, gets the message content from the Azure Storage queue and enters it. After processing is complete, delete the message.

  Note: In this chapter, Web role responds only to front-end page requests. Worker role handles complex business processing at the backend.

Web role and worker role are computed separate (note that compute is detached, not multithreaded).

The worker role is not affected by excessive CPU pressure due to user access to Web Role. Because web role and worker role are deployed on different compute nodes.

The following is the source Code explanation section:

1. First, we create a new cloud project, renamed to Azureworkerrole. Entries

2. In the project file, add Web role and worker role. Such as:

  

Template we select Web Form. Entries

3. In WebRole1, Add the Default.aspx page, adding a TextBox and Button control . Add the following code:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingMicrosoft.WindowsAzure.ServiceRuntime;usingMicrosoft.WindowsAzure.Storage;usingMicrosoft.WindowsAzure.Storage.Queue;namespacewebrole1{ Public Partial classDefault:System.Web.UI.Page {protected voidPage_Load (Objectsender, EventArgs e) {        }        protected voidbtnSubmit_Click (Objectsender, EventArgs e)            {AddMessage (TxbInput.Text.Trim ()); Txbinput.text=""; }        /// <summary>        ///join the message to the Azure Storage Queue/// </summary>        /// <param name= "InputMessage" ></param>        Private voidAddMessage (stringinputmessage) {            varAccount = Cloudstorageaccount.parse (Roleenvironment.getconfigurationsettingvalue ("storageconnection")); varQueueclient =Account .            Createcloudqueueclient (); varQueue = Queueclient.getqueuereference ("Taskqueue"); Queue.            Createifnotexists (); Cloudqueuemessage message=Newcloudqueuemessage (inputmessage); Queue.        AddMessage (message); }    }}

The core code is queue. AddMessage (). Add the message content to the Azure Storage queue.

4. Add the following code to WorkerRole.cs:

  /// <summary>        ///Editor:lei Zhang///Create Azure Storage Queue/// </summary>        Private voidCreateazurestoragequeue () {varAccount = Cloudstorageaccount.parse (Roleenvironment.getconfigurationsettingvalue ("storageconnection")); varQueueclient =Account .            Createcloudqueueclient (); //the Azure Storage queue name must be lowercase            varQueue = Queueclient.getqueuereference ("Taskqueue"); Queue.        Createifnotexists (); }        /// <summary>        ///reading data from the Azure Storage queue/// </summary>        Private voidGetqueue () {varAccount = Cloudstorageaccount.parse (Roleenvironment.getconfigurationsettingvalue ("storageconnection")); varQueueclient =Account .            Createcloudqueueclient (); //the Azure Storage queue name must be lowercase            varQueue = Queueclient.getqueuereference ("Taskqueue"); Queue.            Createifnotexists (); //dequeue the message and lock message in secondsCloudqueuemessage retrievedmessage = queue. GetMessage (Timespan.fromseconds ( -)); if(Retrievedmessage = =NULL)return; Trace.traceinformation ("retrieved message with content ' {0} '", retrievedmessage.asstring); //Async Delete the messagequeue.            DeleteMessage (Retrievedmessage); Trace.traceinformation ("Deleted Message"); }

  The core code for the Worker role is Queue.getmessage (Timespan.fromseconds () above) and the queue. DeleteMessage ().

When a worker role instance uses Queue.getmessage (Timespan.fromseconds (30)) to read to a queue message when there are multiple worker role instances, By default, a lock is added to this message.

Other worker Role instance does not read this message to prevent the message from being read repeatedly.

  

We can also bulk read 20 messages (Queue message) and read up to 32 messages with the following API. Set the lock of the message to 5 minutes at the same time

foreach  in queue. Getmessages (timespan.fromminutes (5))) {    //  Process all messages in Less than 5 minutes, deleting each message after processing.     queue. DeleteMessage (message);}

  

5. Finally we add the corresponding Azure Storage Connection string Connection string to the settings of Web role and worker role.

  

6. We run the program locally, through Visual Studio 2013. In the Default.aspx page, enter the message content as follows:

  

7. Then we open the local emulator and we can see the output of the worker role.

  

  

8. We repeatedly enter multiple values in the Default.aspx page. In the local emulator, you can see multiple outputs of the worker role.

  

  

9. In WorkerRole.cs's code, we can also handle other complex business logic asynchronously, such as sending messages asynchronously, processing data asynchronously, and so on.

Windows Azure Cloud Service (PAAs) Web Role, Worker Role, Azure Storage Queue (bottom)

Related Article

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.