Azure ServiceBus Topic using REST API – Part 3

11 במרץ 2012

9 comments

In the last two posts we showed how to use the Azure ServiceBus service bus REST API to send a message to a topic.
In this post we will see how to listen on a topic and receive a message.


There are two options:


1. Receive a message and delete it from the topic.




Receive and Delete



  1. public static T ReceiveAndDeleteMessage<T>(string serviceNamespace, string topicName,

  2.     string subscriptionName, string token) where T : class

  3. {

  4.     var address = string.Format("https://{0}.{1}/{2}/subscriptions/{3}/messages/head?timeout=10",

  5.         serviceNamespace, sbHostName, topicName, subscriptionName);


  6.     WebClient webClient = new WebClient();

  7.     webClient.Headers[HttpRequestHeader.Authorization] = token;


  8.     byte[] response = webClient.UploadData(address, "DELETE", new byte[0]);


  9.     return Deserialize<T>(response);            

  10. }

2. Receive the message but keep it locked on the queue until we decide to delete it (for example after message processing succeeded).




Receive and Peek



  1. public static T ReceiveAndPeekMessage<T>(string serviceNamespace, string topicName,

  2.     string subscriptionName, string token) where T : class

  3. {

  4.     var address = string.Format("https://{0}.{1}/{2}/subscriptions/{3}/messages/head?timeout=10",

  5.         serviceNamespace, sbHostName, topicName, subscriptionName);


  6.     WebClient webClient = new WebClient();

  7.     webClient.Headers[HttpRequestHeader.Authorization] = token;

  8.     byte[] response = (webClient.UploadData(address, "POST", new byte[0]));


  9.     //use the information in the BrokerProperties to unlock the message

  10.     var serilaizedBrokeredProperties = webClient.ResponseHeaders["BrokerProperties"];

  11.     BrokerProperties brokerProperties = BrokerProperties.Deserialize(serilaizedBrokeredProperties);            

  12.    

  13.     return Deserialize<T>(response);

  14. }

The following table describe the information that will be placed in the brokerProperties object 
















Response Header


Description


BrokerProperties{MessageLocation}


The URI of the locked message. This URI is needed to unlock or delete the message.


BrokerProperties{LockTocken}


The lock ID for the locked message. This ID is needed to delete the locked message.


BrokerProperties{LockLocation}


The lock URI for the locked message. This URI is needed to unlock the message for processing by other receivers. The lock URI will be the following format: https://{serviceNamespace}.appfabric.Windows.net/{topic-path}/subscriptions/{subscription-name}/messages/{message-id}/{lock-id}.


To delete a lock or delete message (after the lock is deleted) I will use a general method for deleting service bus resources




Delete Resources



  1. private static void DeleteResource(string address, string token)

  2. {


  3.     WebClient webClient = new WebClient();

  4.     webClient.Headers[HttpRequestHeader.Authorization] = token;


  5.     webClient.UploadData(address, "DELETE", new byte[0]);

  6. }

That's all we received a message. On the next post we will learn how to create a rule for a subscription.


Enjoy


manu

Add comment
facebook linkedin twitter email

Leave a Reply to RaraZoown Cancel Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

9 comments

  1. Abdul Majid16 באפריל 2012 ב 18:15

    Your articles are very good, but how can I get the body of the Message please?

    Reply
  2. kitayshinakom9 ביוני 2012 ב 18:47

    грузовые шины Oktyre , SmartStone , Fenglun.
    kitayshina.com

    Reply
  3. RaraZoown25 בינואר 2013 ב 1:05

    Срочный выкуп авто Kia Ceed[url=http://automsk.net].[/url]

    Reply
  4. grwastqz6 באפריל 2013 ב 5:00

    u1hetwth

    p7w7h329

    f54yewr4t536

    lkwuqdjj

    ezgqvcoy

    Reply
  5. grwastqz19 באפריל 2013 ב 0:07

    tv2g3rg6

    fssfa9wo

    f54yewr4t536

    dpfep95y

    k88xbdlb

    Reply
  6. grwastqz21 באפריל 2013 ב 11:22

    x7muhe9t

    g79k5ws6

    f54yewr4t536

    dkbmcrrj

    vbcut0jr

    Reply
  7. grwastqz21 באפריל 2013 ב 15:06

    nain02kk

    vz8sihfm

    f54yewr4t536

    bvjlu2yg

    i9roeb9v

    Reply
  8. BajaHobbies8 בספטמבר 2013 ב 1:24

    السلام عليكم ورحمة الله وبركاتةرمضان مبارك للجميع

    يسعدنا ان نقدم لكم احدث السيارات والطائرات اللاسلكية وقطع الغيار والتزويد والخدمات المتميزة بافضل الاسعار بمناسبة حلول الموسم الجديد. قومو بزيارة موقعنا على الانترنت عبر الرابط التالي

    [url=http://www.uaebaja.com]Baja Hobbies[/url]

    السيارات المتوفرة :
    Nanda NRB 3 RTR Offroad-Buggy
    Baja 5b 30.5cc
    Baja 5b 29cc
    Baja 5T 29cc
    Baja 5sc 30.5cc

    وجميع قطع الغيار والتزويد

    الطائرات المتوفرة:
    Big Stik 60
    Big Stik 40
    Super Trainer 60
    Sport Trainer 40
    Senior Kadet 90
    Cessena
    Yak

    وجميع قطع الغيار والتزويد

    عنوان المحل
    المصفح الشعبية شرق 10
    بجانب سوبر ماركت فريش اند مور و نجوم تيتانيك لزينة السيارات
    بناية رقم 138, محل رقم S01
    رقم التلفون : 025536031
    موبايل : 0505113101
    أبوظبي, الامارات العربية المتحدة
    تويتر : uaebaja
    instagram: bajahobbies
    facebook: [url= https://www.facebook.com/pages/Baja-Hobbies/369937139684086%5DBaja Hobbies [/url]

    Reply
  9. http://www.e-tires.ca/ckeditor/images/default.aspx?430 בספטמבר 2013 ב 2:01

    That will be viewed with a golden glow, whenever Peyton and Eli retire with their Super Bowls rings and their records. This game, though, revealed the stark disparity in the brothers' current fortunes in a much harsher light.
    nike football jerseys

    Reply