Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
rabbitmq-tutorials/dotnet/ReceiveLogsTopic/ReceiveLogsTopic.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
46 lines (38 sloc)
1.34 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Text; | |
using RabbitMQ.Client; | |
using RabbitMQ.Client.Events; | |
var factory = new ConnectionFactory { HostName = "localhost" }; | |
using var connection = factory.CreateConnection(); | |
using var channel = connection.CreateModel(); | |
channel.ExchangeDeclare(exchange: "topic_logs", type: ExchangeType.Topic); | |
// declare a server-named queue | |
var queueName = channel.QueueDeclare().QueueName; | |
if (args.Length < 1) | |
{ | |
Console.Error.WriteLine("Usage: {0} [binding_key...]", | |
Environment.GetCommandLineArgs()[0]); | |
Console.WriteLine(" Press [enter] to exit."); | |
Console.ReadLine(); | |
Environment.ExitCode = 1; | |
return; | |
} | |
foreach (var bindingKey in args) | |
{ | |
channel.QueueBind(queue: queueName, | |
exchange: "topic_logs", | |
routingKey: bindingKey); | |
} | |
Console.WriteLine(" [*] Waiting for messages. To exit press CTRL+C"); | |
var consumer = new EventingBasicConsumer(channel); | |
consumer.Received += (model, ea) => | |
{ | |
var body = ea.Body.ToArray(); | |
var message = Encoding.UTF8.GetString(body); | |
var routingKey = ea.RoutingKey; | |
Console.WriteLine($" [x] Received '{routingKey}':'{message}'"); | |
}; | |
channel.BasicConsume(queue: queueName, | |
autoAck: true, | |
consumer: consumer); | |
Console.WriteLine(" Press [enter] to exit."); | |
Console.ReadLine(); |