Aashish Mangal
Aashish Mangal

Reputation: 13

How to send message to a particular subscription using Azure brokered messaging

I want to send message to a particular subscription using brokered service(rest), the how can i do this.

If there are lots of subscriptions on a topic and i want to send message to the particular subscription.

Upvotes: 0

Views: 1853

Answers (2)

Aashish Mangal
Aashish Mangal

Reputation: 13

Finally I got the answer for this question. Answer having the following steps.

  1. Pass SQL filters node in the xml of subscription while creating subscription at client end like below.

@"<entry xmlns=""http://www.w3.org/2005/Atom"">

  <title type=""text"">" + SubscriptionName + @"</title>

   <content type=""application/xml"">

    <SubscriptionDescription xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.microsoft.com/netservices/2010/10/servicebus/connect"" >
                                        	<DefaultRuleDescription>
            <Filter i:type=""SqlFilter"">
                <SqlExpression>" + "VenueId='" + venueId + "' or CustomerId='" + customerId + @"'</SqlExpression>
                 <CompatibilityLevel> 20 </CompatibilityLevel>
             </Filter>
             <Action i:type = ""EmptyRuleAction""/>
             <Name>$Default</Name>
             </DefaultRuleDescription>
      </SubscriptionDescription>
     </content>
            
</entry>";

  1. Add headers in webClient with same name used in sql filters like below
webClient.Headers.Add("CustomerId", customerId);
webClient.Headers.Add("VenueId", venueId);
  1. Create the subscription. This subscription will be created and have some filters.

  2. While retrieving message at client end also add these 2 headers in webClient. So that it will receive messages only having filters for CustomerId and VenueId

webClient.Headers.Add("CustomerId", customerId);
webClient.Headers.Add("VenueId", venueId);
  1. At the service side while sending the message also add these two headers. so it will send message to the only subscriptions having filters with these names.
webClient.Headers.Add("CustomerId", customerId);
webClient.Headers.Add("VenueId", venueId);

Upvotes: 0

Erik Oppedijk
Erik Oppedijk

Reputation: 3551

Each subscription on a topic should have its own rules (subscription), a client sending a message to a topic normally doesn't want to know to which subscription to send to.

If you do need this, try something like this:

Client -> Topic  | Subscription 1   |  *
                 | Subscription 2   |  properties.customername = "A"
                 | Subscription 3   |  properties.customername = "B"
                 | Subscription 4   |  properties.special = "123"

To send a message to only one subscription, make sure all subscriptions have a unique subscription. In the above example, subscription 1 receives all messages, change this to something like this:

Client -> Topic  | Subscription 1   |  properties.customername EXISTS
                 | Subscription 2   |  properties.customername = "A"
                 | Subscription 3   |  properties.customername = "B"
                 | Subscription 4   |  properties.special = "123"

more info: https://msdn.microsoft.com/library/azure/microsoft.servicebus.messaging.sqlfilter.sqlexpression.aspx

Another solutions could be to create a seperate topic to handle this, and this topic can "forward" all other request to your regular topic. (topics can be chained to create this behaviour) https://azure.microsoft.com/en-us/documentation/articles/service-bus-auto-forwarding/

Upvotes: 1

Related Questions