Windows Mobile Support

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Tuesday, 28 August 2012

Service Bus Topics - How to use it (part 2)

Posted on 18:12 by Unknown
In this post I will continue the post about how we can use Service Bus Topics. In the previews topic we saw how we can create and send messages to a topic. In this post we will see how we can consume messages that were sent to a topic.
Each topic can have from one to n subscribers. A subscriber represents a consumer of messages from the topic. Each subscriber can register to all messages that are added to a topic or only to a part for them. Each subscriber can have a filter set; based on this filter only the messages that validate the filter condition will be received by him. We can imagine that for each subscriber a private queue is create where messages are sent for the specific subscriber.
The base subscriber that is the most simple is the one that subscribe to all messages that are sent to a topic. The type of the filter that is used for this case is called “MatchAll” – and is the default filter. Each subscriber has a unique named and similar with creating the topic, we need to check if the subscriber already exists before creating it.
if (!namespaceManager.SubscriptionExists("myFooTopic", "retriveAllMessagesSubscriber"))
{
namespaceManager.CreateSubscription("myFooTopic", "retriveAllMessagesSubscriber");
}
The first parameter specifies the name of the topic and the second one represent the subscriber name. The naming conventions for the subscriber name are the same as for a topic name. Beside the default behavior, we can specify custom filters that can be applied to messages. For example we can define a subscriber that only accepts messages that a property named “value” set to 10.
This custom filter can be specified in two ways – filters and rule description.
The first way, using filters, is using the SQLFilter rules implementation (SQL92). If you have some knowledge about SQL and where clause, that it will be very easy to use this filter. For the above example we need to create a new instance of SQLFilter and to specify the “value” property to be equal to 10.
namespaceManager.CreateSubscription(
"myFooTopic",
"valueIs10Subscriber",
new SqlFilter("value = 10"));
The other way is by using the RuleDescription class. In comparison with a filter, the rule description can have custom actions that can be executed when the filter conditions is true. For example we have the ability to change a property of the BrokeredMessage that is in the topic. For example when the value is 10 we want to set the property isValid to false. For this we will need to create a RuleDescription where the filter condition to be the same as in the above example, but the action will set the isValid property to false.
RuleDescription ruleDescription = new RuleDescription()
{
Action = new SqlRuleAction("set isValid= false"),
Filter = new SqlFilter("value = 10");
}
namespaceManager.CreateSubscription(
"myFooTopic",
"seccondSubscription",
ruleDescription);
We can have only the action or only the filter set. Also each rule can have a name set. In this way it is easier for us to edit a subscription. When we create or retrieve a subscription an object is returned that permit to add/remove/change any custom rule that we defined. In the same manner we can have more than one rule defined on a subscription.
var subscription = namespaceManager.CreateSubscription(
"myFooTopic",
" thirdSubscription");
subscription.Add(ruleDescription1);
subscription.Add(ruleDescription2);
subscription.Add(ruleDescription3);
The rules will be applied in the order that they were registered.
Maybe you have the following question? Does a message will be added to the subscription if one of the rules is not satisfied?
Yes, by default the TrueFilter is added. Any modification that is made to the message by the rule (using Action) will not be persisted to the topic – it will be persisted only to the subscription if the message is added.
Besides TrueFilter, we have also the FalseFilter. In this case, by default all messages are blocked and only messages that respect the rules are added to the subscription. To be able to change this, the first step after creating a subscription is to remove all the rules. After this step we can add our custom rules.
var subscriptionRules = namespaceManager.GetRules(
"myFooTopic",
"thirdSubscription");
SubscriptionClient subscription = messagingFactory.CreateSubscriptionClient(
"myFooTopic",
"thirdSubscription");

foreach (var rule in subscriptionRules)
{
subscription.RemoveRule(rule.Name);
}
In the action of a subscription, it you want to access a value property from the current message you need to use “[sys]”. In the following example, I set a value of a property as sum of another two properties of the message.
RuleDescription ruleDescription = new RuleDescription()
{
Action = new SqlRuleAction("set sum = [sys].ValueA + [sys].ValueB "),
Name = “sumRule”,
}
Each of this subscription can have some custom property that can be set as expiration time, lock duration, the number of maximum delivery and so on. Using this functionality we can create a pretty complicated flow.
In this moment we can talk about how to receive messages from a subscription. Like Service Bus Queue we can receive message in two different ways: PeekLock and ReceiveAndDelete. For the first one a message is not removed until we don’t call the Complete() method of the message. Using the second one, a message is automatically deleted when is send to the received. Because of this if any error appear, the message will be lost. I recommend the first way if you want to consume messages in a safe way. To create a subscription client that is able to receive messages we need to specify the topic name, credentials and the subscription name. After this we can peak message from the subscription and consume them.
SubscriptionClient subscriptionClient = SubscriptionClient.CreateFromConnectionString(
CloudConfigurationManager.GetSetting("ServiceBusConnectionString"),
"myFooTopic",
"thirdSubscription");

subscriptionClient.Receive();
BrokeredMessage brokeredMessage = subscriptionClient.Receive();

if (message != null)
{
try
{
...
message.Complete();
}
catch (Exception)
{
message.Abandon();
}
}
}
In the above example we consume messages in PeekLock mode. At this level, all the action that we could do with Service Bus Queues can be made also with Service Bus Topics – they have the same base class MessageClientEntity and are using the same message (BrokeredMessage).
At the end we should know that: if we have two clients that we want to receive the same message with the same rules, that we need to specify to subscribers – one for each client. If we have a subscription, that can be consume by 2, 3 or n instances, that we should define only one subscription.
In conclusion receiving message from a Service Bus Topic can be a simple thing but in the same way a complicate one (defining the rules). Depends very much what we want to do. The good part of these rules is that they use the Sql92 standard implementation. Because of this we don’t need to learn how we need to specify each rule.
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in service bus, Windows Azure | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • Service Bus Topic - Automatic forward messages from a subscription to a topic
    Windows Azure Service Bus Topic is a service that enables us to distribute the same messages to different consumers without having to know e...
  • Patterns in Windows Azure Service Bus - Message Splitter Pattern
    In one of my post about Service Bus Topics from Windows Azure I told you that I will write about a post that describe how we can design an a...
  • CDN is not the only solution to improve the page speed - Reverse Caching Proxy
    I heard more and more often think like this: “If your website is to slow, you should use a CDN.” Great, CDN is THE solution for any kind of ...
  • E-Learning Vendors Attempt to Morph Mobile
    The sign should read: " Don't touch! Wet Paint !" I had a good chuckle today after receiving my latest emailed copy of the eLe...
  • Content Types - Level 6: Rich Media
    Level 6: Rich Media NOTE: This is part 7 of 7 and the conclusion of this continuing series; please see earlier posts for more background inf...
  • Publishing our CellCast Widget for iPad
    The rush has been on this week as our development team worked to design a new version of our CellCast Widget specifically for Apple's up...
  • Content Types - Level 5: Courseware
    Level 5: Content and Courseware NOTE: This is part 6 of 7 in a continuing series; please see earlier posts for more background information. ...
  • SQL - UNION and UNION ALL
    I think that all of us used until now UNION in a SQLstatement. Using this operator we can combine the result of 2 queries. For example we wa...
  • Cum sa salvezi un stream direct intr-un fisier
    Cred ca este a 2-a oara când întâlnesc aceasta cerința in decurs de câteva săptămâni. Se da un stream și o locație unde trebuie salvat, se c...
  • Content Types - Level 4: Reference
    Level 4: Reference Materials & Static Content NOTE: This is part 5 of 7 in a continuing series; please see earlier posts for more backgr...

Categories

  • .NET
  • .NET nice to have
  • #if DEBUG
  • 15 iunie 2011
  • 15 octombrie 2011
  • 2011
  • abstracta
  • action
  • adaugare
  • ajax
  • Amsterdam
  • Android
  • aplicatii
  • App Fabric
  • Apple iSlate
  • array
  • as
  • ASP.NET
  • AsReadOnly
  • Assembly comun
  • async
  • Asynchronous programming
  • asyncron
  • Autofac
  • AutoMapper
  • az
  • Azure
  • Azure AppFabric Cache
  • Azure backup solution
  • Azure Storage Explorer
  • azure. cloud
  • backup
  • BCP utility
  • bing maps v7
  • BitArray
  • BlackBerry
  • blob
  • BlobContainerPublicAccessType
  • breakpoint
  • bucuresti
  • C#
  • cache
  • CallerMemberName
  • CellCast
  • Certificate
  • CES
  • change
  • ChannelFactory
  • clasa
  • classinitialize
  • clean code
  • click event
  • close
  • Cloud
  • Cluj
  • cluj-napoca
  • Code contracts
  • code retrat
  • codecamp
  • CollectionAssert
  • Compact Edition
  • compara
  • Comparer T .Default
  • CompareTo
  • comparison
  • comunitate
  • concurs
  • Conditional attribute
  • configurare
  • connection string
  • container
  • content type
  • control
  • Convert
  • convertAll
  • convertor
  • cross platform
  • CRUD
  • css
  • custom properties
  • custom request
  • DACPAC
  • Daniel Andres
  • data sync service
  • database
  • date time
  • datetime
  • debug
  • default
  • delegate
  • dependency injection
  • deploy
  • DeploymentItem
  • design patterns
  • Dev de Amsterdam
  • development stoage
  • dictionary
  • diferente
  • digging
  • director
  • Directory.Exist
  • disable
  • dispatcher
  • dispose
  • dropdown
  • dynamic
  • EF
  • email
  • encoding
  • entity framework
  • enum
  • enumerable
  • Environment.NewLine
  • error
  • error 404
  • error handling
  • eveniment
  • event
  • ews
  • excel
  • exception
  • exchange
  • exita
  • explicit
  • export
  • extension
  • field
  • File.Exist
  • finalize
  • fire and forget
  • Fluent interface pattern
  • format
  • func
  • GC.SuppressFinalize
  • generic
  • getdirectoryname
  • globalization
  • gmail
  • hackathon
  • Hadoop
  • handle
  • HTML
  • html 5
  • Html.ActionLink
  • http://www.blogger.com/img/blank.gif
  • HttpModule
  • IComparable
  • IE
  • ienumerable
  • IIS
  • image
  • implicit
  • import
  • int
  • internationalization
  • Internet Explorer
  • interop
  • Ioc
  • IP Filter
  • iPhone
  • iQuest
  • IStructuralEquatable
  • ITCamp
  • itspark
  • java script
  • javascript
  • July 2012
  • KeyedByTypeCollection
  • KeyNotFoundException
  • Kinect SDK
  • lambda expression
  • LightSwitch Microsoft Silverlight
  • linq
  • list
  • lista
  • lista servicii
  • liste
  • Live Connect
  • Live ID
  • load
  • localization
  • lock
  • m-learning
  • MAC
  • Mango
  • map
  • mapare
  • mapare propietati
  • messagequeue
  • meta properties
  • method
  • MethodImpl
  • Metro App
  • Microsoft
  • Microsoft Sync Framework
  • mlearning
  • mlearning devices
  • Mobile Apps
  • mobile in the cloud
  • mobile learning
  • mobile services
  • Mobile Web
  • mongoDb
  • monitorizare
  • msmq
  • multitasking
  • MVC
  • MVC 3
  • MVVM
  • namespace
  • nextpartitionkey
  • nextrowkey
  • Ninject
  • nivel acces
  • no result
  • normalize
  • nosql
  • null expcetion
  • null object pattern
  • NullReferenceException
  • OAuth API
  • office
  • offline
  • Open ID
  • openhackeu2011
  • operations
  • operator
  • optimization
  • option
  • outputcache
  • OutputCacheProvider
  • override
  • paginare
  • pagination
  • path
  • persistare
  • Portable Library tool
  • Post event – CodeCamp Cluj-Napoca
  • predicate
  • predictions
  • prezentare
  • process
  • proiect
  • property
  • propietati
  • query
  • ReadOnlyCollection
  • ReadOnlyDictionary
  • referinta
  • reflection
  • remote
  • reply command
  • request
  • request response
  • resouce
  • REST
  • REST Client
  • RESTSharp
  • ronua
  • rss
  • rulare
  • salvare in fisier
  • sc
  • schimbare timp
  • select
  • select nodes
  • send
  • serializare
  • serialization
  • Server.Transfer. Resposen.Redirect
  • service bus
  • ServiceBase
  • servicecontroller
  • sesiune
  • session
  • Session_End
  • Session_Start
  • setup
  • Sibiu
  • signalR
  • Silverlight
  • sincronizare
  • Single Responsibility Principle
  • SkyDrive
  • skype
  • smartphones
  • smtp
  • Snapguide
  • sniffer
  • socket
  • solid
  • spec#
  • sql
  • Sql Azure
  • SQL CE
  • sql server 2008 RC
  • SRP
  • startuptype
  • stateful
  • stateless
  • static
  • stergere
  • store
  • store procedure
  • stream
  • string
  • string.join
  • struct
  • StructuralEqualityComparer
  • submit
  • switch
  • Symbian
  • Synchronized
  • system
  • tabele
  • table
  • techEd 2012
  • tempdata
  • test
  • testcleanup
  • testinitialize
  • testmethod
  • thread
  • timer
  • ToLower
  • tool
  • tostring
  • Total Cost Calculator
  • trace ASP.NET
  • transcoding
  • tuplu
  • tutorial
  • TWmLearning
  • type
  • unit test
  • unittest
  • UrlParameter.Optional
  • Validate
  • validation
  • verificare
  • video
  • view
  • ViewBag
  • virtual
  • visual studio
  • VM role
  • Vunvulea Radu
  • wallpaper
  • WCF
  • WebBrower
  • WebRequest
  • where clause
  • Windows
  • windows 8
  • Windows Azure
  • Windows Azure Service Management CmdLets
  • windows live messenger
  • Windows Mobile
  • Windows Phone
  • windows service
  • windows store application
  • Windows Task
  • WinRT
  • word
  • workaround
  • XBox
  • xml
  • xmlns
  • XNA
  • xpath
  • YMesseger
  • Yonder
  • Zip

Blog Archive

  • ►  2013 (139)
    • ►  November (17)
    • ►  October (12)
    • ►  September (10)
    • ►  August (7)
    • ►  July (8)
    • ►  June (15)
    • ►  May (12)
    • ►  April (17)
    • ►  March (16)
    • ►  February (9)
    • ►  January (16)
  • ▼  2012 (251)
    • ►  December (9)
    • ►  November (19)
    • ►  October (26)
    • ►  September (13)
    • ▼  August (35)
      • Service Bus Topic – How we can migrate from Servic...
      • Service Bus Topic - Different ways to create and m...
      • Service Bus Topics - Using with WCF services
      • Java Script code refactoring - hands on code
      • C# error at compile time challenge
      • Service Bus Topics - Limitations
      • Service Bus Topics - How to use it (part 2)
      • Service Bus Topics - How to use it (part 1)
      • Service Bus Topics - Overview
      • Where we can add resjson files in a Metro Application
      • Service Bus Queues blog post series – a comprehens...
      • Service Bus Queues from Windows Azure - Business ...
      • Service Bus Queues from Windows Azure - Integratio...
      • Service Bus Queues from Windows Azure - Scheduling
      • Promises and Asynchron calls in Metro Application ...
      • Service Bus Queues from Windows Azure - BrokeredMe...
      • Service Bus Queues from Windows Azure - How to ret...
      • Service Bus Queues from Windows Azure - Death lett...
      • Service Bus Queues vs Windows Azure Queues
      • Don't name your class "XXXManager"
      • Metro Apps on Window 8 - What to use? XAML/HTML? J...
      • Service Bus Queues from Windows Azure - How to sep...
      • Windows 8 Metro App - How to debug JS that was loa...
      • How to use Diagnostic Monitor on Windows Azure
      • Solution - A challenge with Promises from Java Script
      • Service Bus Queues from Windows Azure - How to ite...
      • A challenge with Promises from Java Script
      • Today Software Magazine – I had the honor to write...
      • How should we use Command/Query Segreration (part 2)
      • Service Bus Queues from Windows Azure - How to use it
      • How to define promises on Metro App for Windows 8 ...
      • How should we use Command/Query Segreration (part 1)
      • Service Bus Queues from Windows Azure - Introduction
      • Service Bus - Introduction to Service Bus of Windo...
      • Metro App - Call a Java Script method by name and ...
    • ►  July (28)
    • ►  June (27)
    • ►  May (24)
    • ►  April (18)
    • ►  March (17)
    • ►  February (20)
    • ►  January (15)
  • ►  2011 (127)
    • ►  December (11)
    • ►  November (20)
    • ►  October (8)
    • ►  September (8)
    • ►  August (8)
    • ►  July (10)
    • ►  June (5)
    • ►  May (8)
    • ►  April (9)
    • ►  March (14)
    • ►  February (20)
    • ►  January (6)
  • ►  2010 (26)
    • ►  December (1)
    • ►  November (1)
    • ►  October (1)
    • ►  June (2)
    • ►  May (1)
    • ►  April (4)
    • ►  March (1)
    • ►  February (1)
    • ►  January (14)
Powered by Blogger.

About Me

Unknown
View my complete profile