This documentation is for WSO2 Message Broker version 2.1.0. View documentation for the latest release.

Unknown macro: {next_previous_link3}
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 6 Next »

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 "QueuePublisher", which is used to publish messages to a known, created queue in WSO2 Message Broker, and then introduces a sample .NET client named "QueueConsumer" to receive messages and print message content to the console.

Prerequisites

To run this code sample, you must download and add the RabbitMQ.Client.dll file as a reference in your .NET project. You can download the file from http://www.rabbitmq.com/dotnet.html or from the WSO2 repository.

Running the sample

Before running the QueuePublisher class, we must register at least one  QueueConsumer  binding before sending messages to the queue by doing one of the following:

  • 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

/*
*  Copyright (c) 2005-2010, 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 RabbitMQ
{
    class QueuePublisher
    {
        static void Main(string[] args)
        {
            QueuePublisher publisher=new QueuePublisher();
            publisher.PublishMessage("This is a Test " +i);
            Console.WriteLine("Sent Message "+i);
           
            Console.ReadLine();
        }

 
        public void PublishMessage(string message)
        {
        //Setup the connection with the message broker
            ConnectionFactory factory = new ConnectionFactory();
            IProtocol protocol = Protocols.AMQP_0_8_QPID;
            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 the exchange for the publisher.Here the exchange type is direct.
                    ch.ExchangeDeclare("amq.direct", "direct");
                 //Publish the message
                   ch.BasicPublish("amq.direct", "test-queue", null, Encoding.UTF8.GetBytes(message));                                        
                }
            }
        }
    }

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

/*
*  Copyright (c) 2005-2010, 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 QueueConsumer
{
    class QueueConsumer
    {
        static void Main(string[] args)
        {
            QueueConsumer qConsumer = new QueueConsumer();
            qConsumer.getMessage();
        }
 
        public void getMessage()
        {
 
        //Setup the connection with the message broker
            ConnectionFactory factory = new ConnectionFactory();
            IProtocol protocol = Protocols.AMQP_0_8_QPID;
            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.
                    ch.QueueDeclare("test-queue", true, false, false, null);
            //Create the binding between queue and the exchance
                    ch.QueueBind("test-queue", "amq.direct", "test-queue");
                    QueueingBasicConsumer consumer = new QueueingBasicConsumer(ch);
                    ch.BasicConsume("test-queue",false,consumer);
                    
                   
                    while (true)
                    {
                        try
                        {
                            RabbitMQ.Client.Events.BasicDeliverEventArgs e = (RabbitMQ.Client.Events.BasicDeliverEventArgs)consumer.Queue.Dequeue();
                            byte[] body = e.Body;
                            string message = Encoding.UTF8.GetString(body);
                            Console.WriteLine(message);
                            ch.BasicAck(e.DeliveryTag, false);
                        }
                        catch (OperationCanceledException e)
                        {
                            Console.WriteLine(e);
                            break;
                        }
                    }                   
                }
            }
        }        
    }
}


The received messages are printed to the console. 
  • No labels