rabbitmq-tutorials/dotnet/ReceiveLogsTopic.cs
2011-06-13 15:46:58 +01:00

45 lines
1.6 KiB
C#

using System;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
class ReceiveLogsTopic {
public static void Main(string[] args) {
ConnectionFactory factory = new ConnectionFactory();
factory.HostName = "localhost";
using (IConnection connection = factory.CreateConnection())
using (IModel channel = connection.CreateModel()) {
channel.ExchangeDeclare("topic_logs", "topic");
string queue_name = channel.QueueDeclare();
if (args.Length < 1) {
Console.Error.WriteLine("Usage: {0} [binding_key...]",
Environment.GetCommandLineArgs()[0]);
Environment.ExitCode = 1;
return;
}
foreach (string bindingKey in args) {
channel.QueueBind(queue_name, "topic_logs", bindingKey);
}
Console.WriteLine(" [*] Waiting for messages. " +
"To exit press CTRL+C");
QueueingBasicConsumer consumer =
new QueueingBasicConsumer(channel);
channel.BasicConsume(queue_name, true, consumer);
while(true) {
BasicDeliverEventArgs ea =
(BasicDeliverEventArgs)consumer.Queue.Dequeue();
byte[] body = ea.Body;
string message = System.Text.Encoding.UTF8.GetString(body);
string routingKey = ea.RoutingKey;
Console.WriteLine(" [x] Received '{0}':'{1}'",
routingKey, message);
}
}
}
}