In the previous post i described how to send messages to Service Bus queues and topic by using Java Script. In this post I will describe how to receive messages from queue and topic subscription.
To illustrate this, take a look on following example. Assuming that ’queue’ is a valid queue path like ‘myorg/myqueue/’ the function below will call receiveMessage on module SB. After the function has completed
the callback will be invoked and message result (messagingResult.body) will be injected as html ($("#result").html(..).
This is how the API behind SB module is designed. Think about SB module as a Java Script SDK for Microsoft Azure Service Bus.
$("#btnReceive").click(function () { SB.receiveMessage(queue, function (messagingResult) { $("#result").html(messagingResult.body); if (messagingResult.properties != null) { $("#msgId").html(messagingResult.properties.MessageId); } }); });
|
More over, after the message is received, you can grab several message properties also called BrokerProperties. These properties are returned from Service Bus as
Response Header “BrokerProperties”. The value of the named header is serialized as JSON as shown in following example:
BrokerProperties: { "DeliveryCount":1, "EnqueuedSequenceNumber":0, "EnqueuedTimeUtc":"Wed, 26 Mar 2014 21:06:17 GMT", "MessageId":"d738041df39c401e89b18dc335f5af24", "SequenceNumber":38, "SessionId":"MySession", "State":"Active", "TimeToLive":922337203685.47754 } |
If you compare retrieved properties with the property set defined on BrokeredMessage class in .NET you fill note that Java Script in the case above
contains a subset of properties of the .NET class.
Remember the class BrokeredMessage has also the property called “Properties”, which can be used to attach an custom property to the message instance.
If such properties are used in the message they will be attached as extra response headers when receiving the message.
Content-Type: application/xml; charset=utf-8 Server: Microsoft-HTTPAPI/2.0 BrokerProperties: {"DeliveryCount":1,"EnqueuedSequenceNumber":0,"EnqueuedTimeUtc":"Wed, 26 Mar 2014 21:06:17 GMT","MessageId":"d738041df39c401e89b18dc335f5af24","SequenceNumber":38,"SessionId":"MySession","State":"Active","TimeToLive":922337203685.47754} Prop1: 100 Prop2: "Halli Hallo" Access-Control-Expose-Headers: Date,BrokerProperties,Prop1,Prop2 Access-Control-Allow-Origin: http://localhost Access-Control-Allow-Credentials: true Date: Wed, 26 Mar 2014 21:06:54 GMT Content-Length: 29 "i = 0, payload = 2072651487"
|
Following snippet shows the original response a some message. You can notice two properties: Prop1 and Prop2. The response shown above is a result
of following C# code, which sends the message:
var msg = new BrokeredMessage("i = 0, payload = 2072651487"), new DataContractJsonSerializer(typeof(string))) { SessionId = "MySession" }; msg.Properties.Add("Prop1", 100); msg.Properties.Add("Prop2", "Halli Hallo"); client.Send(msg); |
Note that code above uses none default serializer. For more information about message serialization take a look here.
The Java script code in SB.receiveMessage looks like shown below:
// Receives the message from the queue and deletes it in queue. receiveMessage: function (entityName, callback) { var securityToken = getToken(entityName); var xmlHttpRequest = new XMLHttpRequest(); var receiveUri = getUri(entityName, "head"); xmlHttpRequest.open("DELETE", receiveUri, true); xmlHttpRequest.setRequestHeader("Authorization", securityToken); xmlHttpRequest.onreadystatechange = function () { if (this.readyState == 4) { var messagingResult; if (this.status == 200) { var brokerProperties = eval('(' + this.getResponseHeader("BrokerProperties") + ')'); brokerProperties.ContentType = this.getResponseHeader("Content-Type"); messagingResult = new MessagingResult("Success", this.status, brokerProperties, this.response); } else if (this.status == 204) { messagingResult = new MessagingResult("Empty", this.status, null, this.response); } else { messagingResult = new MessagingResult("Failure", this.status, null, this.response); } callback(messagingResult); } }; xmlHttpRequest.send(null); } |
For more information about missing functions like getToken and getUri please refer to previous post.
Posted
Mar 27 2014, 08:47 AM
by
Damir Dobric