|
1 | 1 | using System; |
2 | 2 | using RabbitMQ.Client; |
3 | 3 | using RabbitMQ.Client.Events; |
| 4 | +using System.Text; |
4 | 5 |
|
5 | | -class ReceiveLogsTopic { |
6 | | - public static void Main(string[] args) { |
7 | | - ConnectionFactory factory = new ConnectionFactory(); |
8 | | - factory.HostName = "localhost"; |
9 | | - using (IConnection connection = factory.CreateConnection()) |
10 | | - using (IModel channel = connection.CreateModel()) { |
11 | | - channel.ExchangeDeclare("topic_logs", "topic"); |
12 | | - string queue_name = channel.QueueDeclare(); |
| 6 | +class ReceiveLogsTopic |
| 7 | +{ |
| 8 | + public static void Main(string[] args) |
| 9 | + { |
| 10 | + var factory = new ConnectionFactory() { HostName = "localhost" }; |
| 11 | + using (var connection = factory.CreateConnection()) |
| 12 | + { |
| 13 | + using (var channel = connection.CreateModel()) |
| 14 | + { |
| 15 | + channel.ExchangeDeclare("topic_logs", "topic"); |
| 16 | + var queueName = channel.QueueDeclare(); |
13 | 17 |
|
14 | | - if (args.Length < 1) { |
15 | | - Console.Error.WriteLine("Usage: {0} [binding_key...]", |
16 | | - Environment.GetCommandLineArgs()[0]); |
17 | | - Environment.ExitCode = 1; |
18 | | - return; |
19 | | - } |
20 | | - |
21 | | - foreach (string bindingKey in args) { |
22 | | - channel.QueueBind(queue_name, "topic_logs", bindingKey); |
23 | | - } |
| 18 | + if (args.Length < 1) |
| 19 | + { |
| 20 | + Console.Error.WriteLine("Usage: {0} [binding_key...]", |
| 21 | + Environment.GetCommandLineArgs()[0]); |
| 22 | + Environment.ExitCode = 1; |
| 23 | + return; |
| 24 | + } |
24 | 25 |
|
25 | | - Console.WriteLine(" [*] Waiting for messages. " + |
26 | | - "To exit press CTRL+C"); |
| 26 | + foreach (var bindingKey in args) |
| 27 | + { |
| 28 | + channel.QueueBind(queueName, "topic_logs", bindingKey); |
| 29 | + } |
27 | 30 |
|
28 | | - QueueingBasicConsumer consumer = |
29 | | - new QueueingBasicConsumer(channel); |
30 | | - channel.BasicConsume(queue_name, true, consumer); |
| 31 | + Console.WriteLine(" [*] Waiting for messages. " + |
| 32 | + "To exit press CTRL+C"); |
31 | 33 |
|
32 | | - while(true) { |
33 | | - BasicDeliverEventArgs ea = |
34 | | - (BasicDeliverEventArgs)consumer.Queue.Dequeue(); |
| 34 | + var consumer = new QueueingBasicConsumer(channel); |
| 35 | + channel.BasicConsume(queueName, true, consumer); |
35 | 36 |
|
36 | | - byte[] body = ea.Body; |
37 | | - string message = System.Text.Encoding.UTF8.GetString(body); |
38 | | - string routingKey = ea.RoutingKey; |
39 | | - Console.WriteLine(" [x] Received '{0}':'{1}'", |
40 | | - routingKey, message); |
| 37 | + while (true) |
| 38 | + { |
| 39 | + var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue(); |
| 40 | + var body = ea.Body; |
| 41 | + var message = Encoding.UTF8.GetString(body); |
| 42 | + var routingKey = ea.RoutingKey; |
| 43 | + Console.WriteLine(" [x] Received '{0}':'{1}'", |
| 44 | + routingKey, message); |
| 45 | + } |
41 | 46 | } |
42 | 47 | } |
43 | 48 | } |
|
0 commit comments