Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Objectives

This sample demonstrates how durable or non-durable topics can be created and used in WSO2 Message Broker using the RabbitMQ .NET/C# client. It first introduces a sample .NET named client named TopicPublisher, which is used to publish that publishes messages to a known, created topic in Message Broker, and then . Then it introduces a sample .NET client named named TopicConsumer to listen  that listens for messages and print prints message contents to the console.

Table of Contents
maxLevel3
minLevel3

Prerequisites

To run this code sample, you must download sample:

  • Download and add

...

  • the RabbitMQ.Client.dll

...

...

Running the sample

Before running the TopicPublisher class, you must register at least one TopicConsumer binding before sending messages to the topic by doing one of the following:

  • Log into WSO2 Message Broker management console and create a topic named 'test-topic' (from the Main menu, choose Topics -> Add).
    OR 
  • Run the TopicConsumer class depicted below, which will register a binding to that topic. When you have run the TopicConsumer class, you will see the topic subscription it created is visible in the management console when you choose Topics -> Browse.

...

Building the sample

...

  1. Create a TopicConsumer .NET client to receive messages from the test-topic topic by adding a class with the following code.

    Code Block
    language

...

  1. java
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using RabbitMQ.Client;
     
    namespace MB_

...

  1. TopicClient
    

...

  1. {

...

  1. 
        class 

...

  1. TopicConsumer
        {
            static void Main(string[] args)

...

  1. 
            {
                

...

  1. TopicConsumer 

...

  1. topicConsumer = new 

...

  1. TopicConsumer();
                

...

  1. topicConsumer.

...

  1. GetMessages(

...

  1. );
            }
     
      

...

  1.  

...

  1.      public 

...

  1. void GetMessages()

...

  1. 
            

...

  1. {
               

...

  1.  //Setup the connection with the message broker
                ConnectionFactory factory = new ConnectionFactory();
                IProtocol protocol = Protocols.AMQP_0_

...

  1. 9_

...

  1. 1;
                factory.VirtualHost = "/carbon";
                factory.UserName = "admin";
                factory.Password = "admin";
                factory.HostName = "localhost";
                factory.Port = 5672;
                factory.Protocol = protocol;

...

  1. 
                using (IConnection conn = factory.CreateConnection())

...

  1. 
                {
                    using (IModel ch = conn.CreateModel())
                    {
                        // Declare a topic exchange to

...

  1.  be bound to retrieve messages, here we have used the default topic exchange of WSO2 MB
                        ch.ExchangeDeclare("amq.topic", "topic");
          

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1. // 

...

  1. Declare 

...

  1. a 

...

  1. topic name

...

  1. , here we use a non-durable topic. To make it durable use the 2nd parameter as 'true'  
                        ch.

...

  1. QueueDeclare("

...

  1. test-topic", false, 

...

  1. false, 

...

  1. false, 

...

  1. null);
                     

...

  1.    // Bind the Topic in to the exchange
         

...

  1.          

...

  1.      

...

Next, execute the following TopicConsumer .NET client, which receives messages from 'test-topic'.

...

languagecsharp

...

  1.  ch.QueueBind("test-topic", "amq.topic", "test-topic");
                        // Declare a consumer which listens on the messages published to 'test-topic' topic, we need to declare an exclusive subscriber, in order to get this work.
              

...

  1.  

...

  1.      

...

  1.     // The syntax is 

...

  1. BasicConsume(<queuename>, <noAck>,<consumerTag>, <noLocal>, <exclusive>, <arguments>, <Consumer>)
           

...

  1.              

...

  1. QueueingBasicConsumer 

...

  1. consumer = new 

...

  1. QueueingBasicConsumer(ch);
                  

...

  1.       ch.BasicConsume("test-topic", false, "1", false, true, null, consumer);
            

...

  1.            

...

  1.  

...

  1. while 

...

  1. (true)
                     

...

  1.    {
                            try
                            {
                                

...

  1. RabbitMQ.Client.Events.BasicDeliverEventArgs e = (RabbitMQ.Client.Events.BasicDeliverEventArgs)consumer.Queue.Dequeue();
                  

...

  1.               byte[] body = 

...

  1. e.Body;
                    

...

  1.             string message = Encoding.UTF8.GetString(body);
                  

...

  1.               Console.WriteLine("Received Message : " + message);
                 

...

  1.  

...

  1.  

...

  1.              

...

  1. ch.BasicAck(e.DeliveryTag, false);
                   

...

  1.  

...

  1.  

...

  1.        }
         

...

  1.  

...

  1.  

...

  1.               

...

  1.  

...

  1.  

...

  1.  

...

  1. catch 

...

  1. (

...

  1. OperationCanceledException e)
                            {
                        

...

  1.         Console.WriteLine(e);
                     

...

  1.   

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1. break;
    

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.              }
           

...

  1.  

...

  1.             }
     

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1. }
    

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.         }
            }
        

...

  1. }
    }
    Info

    At least one TopicConsumer binding should exist before sending messages to the topic. Therefore, this TopicConsumer class should be run before the TopicPublisher class. Alternatively, you can manually create the test-topic topic in the MB Management Console. See Adding Topics for detailed instructions.

  2. Create a TopicPublisher .NET client to send messages to the test-topic topic by adding a class with the following code.

    Code Block
    languagejava
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using RabbitMQ.Client;
     
    namespace MB_Topic_Publisher
    {
        class TopicPublisher
        {
            static void Main(string[] args)
            {
                TopicPublisher topicPublisher = new TopicPublisher();
          

...

  1.       topicPublisher.PublishMessage("Test Message");
                Console.WriteLine("Message Sent..");
                Console.ReadLine();
            }
     
           

...

  1.  

...

  1. public 

...

  1. void 

...

  1. PublishMessage(string message)
            {
                //Setup the connection with the message broker
                ConnectionFactory factory = new ConnectionFactory();
                IProtocol protocol = Protocols.AMQP_0_9_1;
              

...

  1.  

...

  1.  factory.VirtualHost = "/carbon";
                factory.UserName = "admin";
            

...

  1.     factory.Password = "admin";
                factory.HostName = "localhost";
                factory.Port = 5672;
                factory.Protocol = protocol;
         

...

  1.        using (IConnection conn = factory.CreateConnection())
                {
           

...

  1.          using (IModel ch = conn.CreateModel())
           

...

  1.          {
               

...

  1.          // Declare a topic exchange to publish messages, here we have used the default 

...

  1. topic exchange of WSO2 MB
                     

...

  1.  

...

  1.  

...

  1.  ch.ExchangeDeclare("amq.topic", "topic");
                      

...

  1.   IBasicProperties basicProperties = 

...

  1. ch.

...

  1. CreateBasicProperties(

...

  1. );
                      

...

  1.   //Setting JMS Message ID.
                        basicProperties.MessageId 

...

  1. = "ID:" + System.Guid.NewGuid().ToString();
                        //Setting content-type for message as we are sending 

...

  1. a text message.
                   

...

  1.  

...

  1.  

...

  1.    basicProperties.ContentType = "text/plain";
                

...

  1.         // Publish the message to the exchange, it will 

...

  1. send it to the routing key which is our name 'myTopic'. 
            

...

  1.             // The syntax is ch.BasicPublish(<exchange_name>, 

...

  1. <topic_name>, <message_properties>,<message_body>)
                        ch.BasicPublish("amq.topic", 

...

  1. "test-topic", basicProperties, Encoding.UTF8.GetBytes(message));
                    }
                }
            }
        }
    }

...

 

...

  1. Add a Main.java class defining the method to call both the classes mentioned above.

Executing the sample

Run this sample from your C# project.