Versions Compared

Key

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

...

Table of Contents
maxLevel3
minLevel3

Introduction

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 "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 "QueueConsumer" to receive messages and print message content to the console.

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

...

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
    /*
    *  Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
    *
    *  WSO2 Inc. licenses this file to you under the Apache License,
    *  Version 2.0 (the "License"); you may not use this file except
    *  in compliance with the License.
    *  You may obtain a copy of the License at
    *
    *    http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing,
    * software distributed under the License is distributed on an
    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    * KIND, either express or implied.  See the License for the
    * specific language governing permissions and limitations
    * under the License.
    */
     
    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.GetMessage();
            }

...

  1. 
      
            public void 

...

  1. GetMessage(

...

  1. )
            {
                //Setup the connection with the message broker
                ConnectionFactory factory = new ConnectionFactory();
                IProtocol protocol = Protocols.AMQP_0_9_1;
                factory.VirtualHost = "/carbon";
                factory.UserName = "admin";
                factory.Password = "admin";
                factory.HostName = "localhost";
                factory.Port = 5672;
                factory.Protocol = protocol;
                using (IConnection conn = factory.CreateConnection())
                {
                    using (IModel ch = conn.CreateModel())
                    {
                        //Declare a queue to retrieve messages.
                     

...

  1.  

...

  1.  

...

  1.  ch.

...

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

...

  1. Create the binding between queue and the exchance
                        ch.QueueBind("test-queue", "amq.direct", "test-queue");
                        QueueingBasicConsumer 

...

  1. consumer = 

...

  1. new QueueingBasicConsumer(ch);
                        ch.BasicConsume("test-queue", false, consumer);
    
                        while (true)
                        {
                            try
                            {
                                RabbitMQ.Client.Events.BasicDeliverEventArgs e = (RabbitMQ.Client.Events.BasicDeliverEventArgs)consumer.Queue.Dequeue();
                        

...

  1.         byte[] body = e.Body;
                               

...

  1.  string message = 

...

  1. Encoding.UTF8.GetString(body);
                        

...

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

...

  1. BasicAck(

...

  1. e.

...

  1. DeliveryTag, 

...

  1. false);
                        

...

  1.     }
                            catch 

...

  1. (OperationCanceledException e)
                            {
                                Console.WriteLine(e);
                                break;
                            }
                        }
                    }
                }
            }        
        }
    }

...

  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
    language

...

  1. java
    /*
    *  Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
    *
    *  WSO2 Inc. licenses this file to you under the Apache License,
    *  Version 2.0 (the "License"); you may not use this file except
    *  in compliance with the License.
    *  You may obtain a copy of the License at
    *
    *    http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing,
    * software distributed under the License is distributed on an
    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    * KIND, either express or implied.  See the License for the
    * specific language governing permissions and limitations
    * under the License.
    */

...

  1. 
    

...

  1. using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using RabbitMQ.Client;
      
    

...

  1. namespace 

...

  1. RabbitMQ
    {
        class 

...

  1. QueuePublisher
        {
            static void Main(string[] args)
            {
                QueuePublisher publisher=new QueuePublisher();
                publisher.PublishMessage("This is 

...

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

...

  1.             Console.ReadLine();
            }
     
      
            public void 

...

  1. PublishMessage(string message)
            {
                //Setup the connection with the message broker
                ConnectionFactory factory = new ConnectionFactory();
                IProtocol protocol = Protocols.AMQP_0_9_1;
                factory.VirtualHost = "/carbon";
                factory.UserName = "admin";
                factory.Password = "admin";
                factory.HostName = "localhost";
                factory.Port = 5672;
                factory.Protocol = protocol;
                using (IConnection conn = factory.CreateConnection())
                {
                    using (IModel ch = conn.CreateModel())
                    {
                        

...

  1. IBasicProperties 

...

  1. basicProperties 

...

  1. = ch.CreateBasicProperties();
                        //

...

  1. Setting 

...

  1. JMS 

...

  1. Message 

...

  1. ID.
    

...

  1.  

...

  1.  

...

  1.  

...

  1.                  

...

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

...

  1. a 

...

  1. text 

...

  1. message.

...

  1. 
                        

...

  1. basicProperties.ContentType = "text/plain";
                        //Declare the exchange for the 

...

  1. publisher.Here the exchange type is direct.
                        

...

  1. ch.

...

  1. ExchangeDeclare(

...

  1. "amq.

...

  1. direct", 

...

  1. "direct");
                        //Publish the message
     

...

  1.                    

...

  1. ch.BasicPublish("amq.direct", "test-queue", basicProperties, Encoding.UTF8.GetBytes(message));
                    }

...

  1. 
                

...

  1. }
            

...

  1. }

...

  1. 
        

...

  1. }
    

...

  1. }

...

Executing the sample

Run this sample from your C# project.