-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSimpleExample.cs
More file actions
52 lines (45 loc) · 1.83 KB
/
Copy pathSimpleExample.cs
File metadata and controls
52 lines (45 loc) · 1.83 KB
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
using MerQure.RbMQ.Content;
using System;
using System.Threading.Tasks;
namespace MerQure.Samples;
public class SimpleExample
{
private readonly IMessagingService _messagingService;
public SimpleExample(IMessagingService messagingService)
{
_messagingService = messagingService;
}
public async Task RunAsync()
{
// RabbitMQ init
await _messagingService.DeclareExchangeAsync("simple.exchange");
await _messagingService.DeclareQueueAsync("simple.queue", isQuorum: true);
await _messagingService.DeclareBindingAsync("simple.exchange", "simple.queue", "simple.message.*");
// Get the publisher and declare Exhange where publish messages
await using var publisher = await _messagingService.GetPublisherAsync("simple.exchange");
// publish messages
for (int i = 0; i <= 10; i++)
{
await publisher.PublishAsync(new Message($"simple.message.test{i}", $"Hello world {i} !"));
}
// Get the consumer on the existing queue and consume its messages
var consumer = await _messagingService.GetConsumerAsync("simple.queue");
var random = new Random();
await consumer.ConsumeAsync((object sender, MessagingEvent args) =>
{
// we simulate the delivery success
if (random.Next() % 2 == 0)
{
Console.WriteLine("Retry " + args.Message.GetRoutingKey());
// send NACK: negative acknowlegdment to the queue
return consumer.RejectDeliveredMessageAsync(args).AsTask();
}
else
{
Console.WriteLine(args.Message.GetBody());
// send ACK: acknowlegdment to the queue
return consumer.AcknowlegdeDeliveredMessageAsync(args).AsTask();
}
});
}
}