-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
55 lines (49 loc) · 1.28 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
using Example;
var connectionString = "Host=localhost;Username=cfurano;Password=cfurano;Database=pg_mq_poc;";
using var consumer = new EventingConsumer(connectionString);
consumer.OnMessageReceived += HandleMessage;
var queueName = "Default Queue";
consumer.OpenChannel(queueName);
var keepRunning = true;
Console.CancelKeyPress += (sender, args) =>
{
args.Cancel = true;
keepRunning = false;
};
var timer = new Timer((consumer) =>
{
((EventingConsumer)consumer).SweepWaitingMessage(queueName);
}, consumer, TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(30));
try
{
while (keepRunning)
{
consumer.Wait(); // Thread will block here
}
}
finally
{
Thread.Sleep(250);
consumer.CloseChannel();
}
static void HandleMessage(Message message, Action ack, Action<TimeSpan?> nack)
{
try
{
Console.WriteLine(message.Body);
var deliveryId = message.DeliveryId;
Console.WriteLine($"Delivery ID: {deliveryId}");
Thread.Sleep(250);
if (Random.Shared.NextDouble() < 0.1)
{
nack(TimeSpan.FromSeconds(60));
Console.WriteLine("Message nacked.");
}
ack();
Console.WriteLine("Message acked.");
}
catch (Exception e)
{
Console.Error.WriteLine(e.Message);
}
}