Versions Compared

Key

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

Objectives

This sample demonstrates how persistent queues can be created and used in Message Broker using the RabbitMQ .NET/C# client. It first introduces a sample .NET client named "named QueuePublisher", which is used to publish messages to a known, created queue in WSO2 Message Broker, and then . Then it introduces a sample .NET client named "named QueueConsumer" to  to receive messages and print message content to the console.

Table of Contents
maxLevel3
minLevel3

Prerequisites

To In order to run this code sample, you must download sample:

  • Download and add

...

  • the RabbitMQ.Client.dll

...

  •  file as a reference in your .NET project. You can download the file

...

...

Running the sample

...

  • Log into WSO2 Message Broker management console and create a queue named 'test-queue' (from the Main menu, choose Queues -> Add). For more information, see Adding Queues.
    OR
  • Run the QueueConsumer class depicted below, which will register a binding to that queue. When you have run the QueueConsumer class, you will see the queue created by that  class is visible in the management console when you go to Queues -> Browse.  

You are now ready to use the QueuePublisher .NET client to send messages to the queue

...

languagecsharp

...

Building the sample

  1. Create a QueueConsumer .NET client to receive messages from the test-queue queue 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 

...

  1. QueueConsumer
    {
        class 

...

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

...

  1. qConsumer 

...

  1. = new 

...

  1. QueueConsumer();
                

...

  1. qConsumer.

...

  1. GetMessage();
            }
      
    

...

  1.  

...

  1.  

...

  1.       public 

...

  1. void GetMessage()
            

...

  1. {
         

...

  1.  

...

  1.  

...

  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())
                {
                    using (IModel ch = conn.CreateModel())
                    {
              

...

  1.           //Declare a queue to retrieve messages.
                        ch.

...

  1. QueueDeclare("

...

  1. test-queue", true, false, false, null);
                        //

...

  1. Create the binding between queue and the 

...

  1. exchance
                        ch.

...

  1. QueueBind("test-queue", "amq.direct", "test-queue"

...

  1. )

...

  1. ;
                        QueueingBasicConsumer consumer = new QueueingBasicConsumer(ch);
                        ch.BasicConsume("test-queue", false, consumer);
    
           

...

  1.              

...

  1. while (true)
            

...

  1.      

...

Next, execute the following QueueConsumer .NET client, which allows you to receive messages from 'test-queue'.

...

languagecsharp

...

  1.  

...

  1.   

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1. {
                            try
                            {
                                RabbitMQ.Client.Events.BasicDeliverEventArgs e = (RabbitMQ.Client.Events.BasicDeliverEventArgs)consumer.Queue.Dequeue();
                                byte[] body = e.Body;
                                string message = Encoding.UTF8.GetString(body);
                    

...

  1.             

...

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

...

  1.          

...

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

...

  1.  

...

  1.  

...

  1.          

...

  1.            

...

  1. }
    

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.              

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.   catch (OperationCanceledException e)
            

...

  1.  

...

  1.  

...

  1.  

...

  1.              

...

  1. {
    

...

  1.  

...

  1.             

...

  1.  

...

  1.  

...

  1.              

...

  1. Console.WriteLine(e);
                  

...

  1.  

...

  1.  

...

  1.             break;
    

...

  1.  

...

  1.  

...

  1.              

...

  1.  

...

  1.  

...

  1.        }
            

...

  1.   

...

  1.  

...

  1.  

...

  1.  

...

  1.        }
         

...

  1.            }
         

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.    }
            }     

...

  1.    
        }
    }
    Info

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

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

    Code Block
    languagejava
    using System;
    using 

...

  1. System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using RabbitMQ.Client;
      
    namespace RabbitMQ
    {
        class QueuePublisher
        {
       

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  static void Main(string[] args)
            

...

  1. {
    

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.       QueuePublisher publisher=new QueuePublisher();
                

...

  1. publisher.

...

  1. PublishMessage("

...

  1. This is a Test Message");
                Console.WriteLine("Message Sent");
          

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.   
                

...

  1. Console.ReadLine();
            }
     
      
            public void PublishMessage(string message)
            {
                //Setup the connection with the message 

...

  1. broker
         

...

  1.  

...

  1.       ConnectionFactory factory = new ConnectionFactory();
              

...

  1.   IProtocol protocol = Protocols.AMQP_0_9_1;
                factory.VirtualHost = "/carbon";
        

...

  1.         factory.UserName = "admin";
                factory.Password = 

...

  1. "admin";
                factory.HostName = "localhost";
                

...

  1. factory.Port = 

...

  1. 5672;
                factory.Protocol = protocol;
                using 

...

  1. (IConnection conn = 

...

  1. factory.CreateConnection())
                {
                    

...

  1. using (IModel ch = 

...

  1. conn.

...

  1. CreateModel(

...

  1. ))

...

  1. 
                    {
               

...

  1.          IBasicProperties 

...

  1. basicProperties = ch.CreateBasicProperties();
                        //Setting JMS Message ID.
    

...

  1.                     basicProperties.MessageId = "ID:" 

...

  1. + System.Guid.NewGuid().ToString();
                        //Setting content-type for message 

...

  1. as we are sending a text message.
                        basicProperties.ContentType 

...

  1. = "text/plain";
                        //Declare the exchange for the publisher.Here the exchange 

...

  1. type is direct.
                        ch.ExchangeDeclare("amq.direct", "direct");
    

...

  1.                     //Publish 

...

  1. the message
                        ch.BasicPublish("amq.direct", "test-queue", 

...

  1. basicProperties, Encoding.UTF8.GetBytes(message));
             

...

  1.        }
         

...

  1.        }
     

...

  1.        

...

  1. }
        }
    }

...

Executing the sample

Run this sample from your C# project.