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 | ||||
---|---|---|---|---|
|
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
...
...
- or from the WSO2 repository.
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.
...
language | csharp |
---|
...
- See Prerequisites to Run the MB Samples for a list of other prerequisites.
Building the sample
Create a QueueConsumer .NET client to receive messages from the
test-queue
queue by adding a class with the following code.Code Block language java 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_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.
...
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
...
...
...
Next, execute the following QueueConsumer .NET client, which allows you to receive messages from 'test-queue'.
...
language | csharp |
---|
...
{ RabbitMQ.Client.Events.BasicDeliverEventArgs e = (RabbitMQ.Client.Events.BasicDeliverEventArgs)consumer.Queue.Dequeue(); byte[] body = e.Body; string message = Encoding.UTF8.GetString(body); Console.WriteLine("Received Message : " + message);
...
...
...
...
...
...
...
ch.BasicAck(e.DeliveryTag, false);
...
...
...
...
...
}
...
...
...
catch (OperationCanceledException e)
...
...
...
...
...
{
...
...
...
...
...
...
...
...
...
...
...
...
Console.WriteLine(e);
...
...
...
...
...
break;
...
...
...
}
...
...
...
...
...
...
}
...
...
...
}
...
...
...
} }
...
...
...
...
...
} }
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.Create a QueuePublisher .NET client to send messages to the
test-queue
queue by adding a class with the following code.Code Block language java 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 Message"); Console.WriteLine("Message Sent");
...
...
...
...
...
...
...
...
Console.ReadLine(); }
...
...
...
public
...
void
...
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(
...
))
...
{
...
...
...
...
...
...
IBasicProperties basicProperties = ch.CreateBasicProperties();
...
//Setting JMS Message ID. basicProperties.MessageId = "ID:"
...
+ System.Guid.NewGuid().ToString(); //Setting content-type for
...
message
...
as
...
we are sending a text message.
...
basicProperties.ContentType = "text/plain"; //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",
...
basicProperties, Encoding.UTF8.GetBytes(message)); } } }
...
} }
...
Executing the sample
Run this sample from your C# project.