Microsoft Azure Service Bus: PeekAndLock Messages with JavaScript

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

This post is the third one of my Service Bus JavaScript posts.

Episode I  - Receiving of Messages

Episode II - Sending of Messages

In this post I will describe how Service Bus peek and lock mechanism works. As in previous post I will use my Service Bus SDK.
For all of You who are not familiar with the PeekLock pattern,
it is a way of receiving the message in reliable manner. That means the consumer of the message will receive the message, but the message will remain in the queue as invisibleinvisile for a while. In my first post I showed how to receive the message. If you analyze the HTTP traffic from that
example, You will figure out that on the wire HTTP-verb “DELETE” is in play. This briefly means: “Receive the message and Delete from the queue”.
Unfortunately such pattern cannot be used if Your application need to process the message reliable way. If application crashes, the message is lost.
PeekAndLook makes message invisible as long You explicitly Complete or Abandon it. Moreover message becomes again visible after in PeekAndLockInterval (default 1 minute) is exceeded without of explicate calling of Complete or Abandon.

Following code snippet shows how to peek the message from queue.

     $("#btnPeekLock").click(function () {
                SB.peekLockMessage(queue,
function (messagingResult) {
                    $(
"#result").html(messagingResult.body);
                   
if (messagingResult.properties != null) {
                        $(
"#msgId").html(messagingResult.properties.MessageId);
                        $(
"#lockUri").html(messagingResult.properties.LockUri);
                    }
                });
            });
          

Note that the code above is obtaining the property LockUri: “messagingResult.properties.LockUri”.
This property is some kind of message receipt, which You will need when calling Abandon or Complete.

The next code snippet shows full implementation of receiving of the message with PeekAnLock.  For more information about
getToken() please take a look at this post .

   peekLockMessage: function (entityName, callback) {

           
var securityToken = getToken(entityName);
           
var uri = getUri(entityName, "head");

           
var xmlHttpRequest = new XMLHttpRequest();
            xmlHttpRequest.open(
"POST", uri, true);
            xmlHttpRequest.setRequestHeader(
"Authorization", securityToken);
            xmlHttpRequest.onreadystatechange =
function () {
               
if (this.readyState == 4) {

                  
var messagingResult;

                  
if (this.status == 201) {

                  
var brokerProperties =
                   eval(
'(' + this.getResponseHeader("BrokerProperties") + ')');
                       

                   brokerProperties.LockUri = this.getResponseHeader("Location");
                   brokerProperties.ContentType =
                  
this.getResponseHeader("Content-Type");

                   messagingResult =
                  
new MessagingResult("Success", this.status, 
                   brokerProperties,
this.response);
                 }
                
else if (this.status == 204) 
                 {
                        messagingResult =
                       
new MessagingResult("NULL", this.status, null, 
                        this
.response);
                  }
                 
else
                  {
                        messagingResult =
                       
new MessagingResult("Error",
                       
this.status, null, this.response);
                    }

                   
if (callback != null)
                        callback(messagingResult);
                }
            };

            xmlHttpRequest.send(
null);
        },

After the message is received this way it will appear again in the queue if Abandon or Complete has not been invoked for a time specified by PeekLockInterval. In the next post (episode IV) I will describe how to deal with Abandon and Complete.




Posted Mar 25 2014, 07:30 AM by Damir Dobric
developers.de is a .Net Community Blog powered by daenet GmbH.