Subscriptions
To receive messages from a Topic Queue, you would create a Subscription. A Subscription resembles a virtual Queue that receives a copy of the message sent to a topic queue.
Note
Note that a single subscription cannot be used for multiple topics. Instead, you need to create multiple subscriptions.
Typically the following code would be duplicated for each interested consumer, specifying the Topic and Subscription names in the constructor:
MessagingFactory factory = MessagingFactory.Create(uri, token); //create a consumer for the subscription SubscriptionClient subscriber1 = factory.CreateSubscriptionClient(topicName, SubscriptionName,ReceiveMode.PeekLock);
Just like with normal queues, you can use either ReceiveAndDelete
or PeekLock
(receive modes) to read the message of the virtual Queue.
Once the client has created the Subscription, the messages can be read off the virtual queue as follows:
while ((receivedMessage = subscriber1.Receive(TimeSpan.FromSeconds(5))) != null) { try ...