-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
69 lines (56 loc) · 2.09 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Threading.Channels;
using RabbitMQ;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using Newtonsoft.Json;
using System.Runtime.InteropServices;
namespace Read {
class Reading {
private ConnectionFactory? factory; private IConnection? connection;
private IModel? channel;
public string? queue_name {get; set;} = "Test";
public string? message {get; set;}
public string? routing_key {get; set;}
public void ConnectServer()
{
try {
factory = new ConnectionFactory {HostName = "localhost"};
connection = factory.CreateConnection();
channel = connection.CreateModel();
}
catch (Exception ex) {
Console.WriteLine($"Failed connection to RabbitMQ. {ex.Message}");
throw;
}
}
public void ReadData()
{
channel.QueueDeclare(queue: queue_name, durable: true, exclusive: false, arguments: null, autoDelete: false);
Console.WriteLine("Waiting for messages...");
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var data = new Reading();
var body = ea.Body.ToArray();
data.message = Encoding.UTF8.GetString(body);
data.routing_key = ea.RoutingKey;
data.queue_name = queue_name;
string json = JsonConvert.SerializeObject(data);
Console.WriteLine(json);
Console.WriteLine($"queue: {data.queue_name}\nrouting-key: {data.routing_key}\nmessage: {data.message}\n");
};
channel.BasicConsume(queue: queue_name, autoAck: true, consumer: consumer);
Console.ReadLine();
}
public static void Main()
{
Reading Start = new Reading();
Start.ConnectServer();
Start.ReadData();
}
}
}