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

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

Prerequisites

In order to run this code sample, you need to download and add RabbitMQ.Client.dll file as a reference in your .net project. You can download that dll file from this website http://www.rabbitmq.com/dotnet.html or here.

Running the Sample

Prior to running following "QueuePublisherclass we need to register at least one " QueueConsumer " binding prior sending messages to the queue  This can be done by either,

  1. Logging into WSO2 Message Broker management console and create a queue named 'test-queue' ("Queues -> Add" menu in the "Main" menu). A quick guide on creating queues can be found in section Adding Queues. 
  2. Run " QueueConsumer" class depicted below. It will register a binding to that queue. When you have run the QueueConsumer  code you will see queue created by the  QueueConsumer class is visible in the management console ("Queues -> Browse").  

Now using the following "QueuePublisher" .Net client, messages can be sent to 'test-queue' created earlier. 

 

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));                                        
                }
            }
        }
    }
  • No labels