Nick's .NET Travels

Continually looking for the yellow brick road so I can catch me a wizard....

Windows Azure, Microsoft .NET Services – Working with Queues (III)

[This post is based on the Windows Azure Tools for Microsoft Visual Studio July 2009 CTP]

In my previous posts, Working With Queues (I) & Working with Queues (II), I showed how you can create, query, renew and delete queues with the Windows Azure, Microsoft .NET Services. Now for the interesting stuff – let’s enqueue and dequeue messages.

As you can imagine enqueuing equates to Post(ing) a message to the tail of the queue, whilst dequeuing equates to delete(ing) from the head of the queue. In code, this is:

private static string EnqueueMessage(string token, string queue, string message)
{
    string queueUri = "
https://blogsample.servicebus.windows.net/" + queue;

    // send
    HttpWebRequest request = HttpWebRequest.Create(queueUri) as HttpWebRequest;
    request.Method = "POST";
    request.Headers.Add("X-MS-Identity-Token", token);
    request.ContentType = "text/plain;charset=utf-8";

    using (var requestStream = request.GetRequestStream())
    using (var writer = new System.IO.StreamWriter(requestStream))
    {
        writer.Write(message);
        writer.Flush();
    }

    using (var response = request.GetResponse())
    {
        return (response as HttpWebResponse).StatusCode.ToString();
    }
}

private static string DequeueMessage(string token, string queue)
{
    string queueUri = "
https://blogsample.servicebus.windows.net/" + queue + "/!(queue/head)?encoding=asreply&maxmessages=1&timeout=30";

    HttpWebRequest request = HttpWebRequest.Create(queueUri) as HttpWebRequest;
    request.ConnectionGroupName = "dequeue";
    request.Method = "DELETE";
    request.Headers.Add("X-MS-Identity-Token", token);
    request.ContentLength = 0;

    using (var response = request.GetResponse())
    using (var responseStream = response.GetResponseStream())
    using (var reader = new StreamReader(responseStream))
    {
        return reader.ReadToEnd();
    }
}

To enqueue a message you send a Http Post to the alternate link (otherwise known as the tail of the queue), ie https://blogsample.servicebus.windows.net/myqueue. You should get back a 202, Accepted response. When you are dequeuing a message you send a Http Delete to the queuehead link (otherwise known as the head of the queue), ie https://blogsample.servicebus.windows.net/myqueue/!(queue/head). You should get back a 200, Ok response, with the message in the content of the response.

As part of the Http Delete you can specify a timeout, in this case 30 seconds. This determines how long the request will wait for a message to appear on the queue before returning.  If a message appears within this timeout you will get the message and a 200 response.  If not, you will get a 204, No Content response.

And now to cover enqueuing and dequeuing in javascript:

function httpEnqueueMessage(token, queue, message) {
    http = new XMLHttpRequest();
    http.open("POST", "
https://blogsample.servicebus.windows.net/" + queue, false);
    http.setRequestHeader("X-MS-Identity-Token", token);
    http.setRequestHeader("Content-type", "text/plain;charset=utf-8");
    http.onreadystatechange = function() {
        if (http.readyState == 4 && http.status == 202) {
            var output = http.responseText;
            // Do something…
        }
    }
    http.send(message);
}

function httpDequeueMessage(token, queue) {
    http = new XMLHttpRequest();
    http.open("DELETE", "
https://blogsample.servicebus.windows.net/" + queue + "/!(queue/head)?encoding=asreply&maxmessages=1&timeout=20", false);
    http.setRequestHeader("X-MS-Identity-Token", token);
    http.onreadystatechange = function() {
        if (http.readyState == 4 && http.status == 200) {
            var output = http.responseText;
            // Do something...
        }
    }
    http.send();
}

Comments are closed