-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStopExample.cs
More file actions
54 lines (46 loc) · 1.86 KB
/
Copy pathStopExample.cs
File metadata and controls
54 lines (46 loc) · 1.86 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
53
54
using MerQure.RbMQ.Content;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace MerQure.Samples;
public class StopExample
{
private readonly IMessagingService _messagingService;
public StopExample(IMessagingService messagingService)
{
_messagingService = messagingService;
}
public async Task RunAsync()
{
// RabbitMQ init
await _messagingService.DeclareExchangeAsync("stop.exchange");
await _messagingService.DeclareQueueAsync("stop.queue", isQuorum: true);
await _messagingService.DeclareBindingAsync("stop.exchange", "stop.queue", "stop.message.* ");
// Get the publisher and declare Exhange where publish messages
await using var publisher = await _messagingService.GetPublisherAsync("stop.exchange");
// publish messages
for (int i = 0; i <= 20; i++)
{
await publisher.PublishAsync(new Message("stop.message.test" + i, $"Hello world {i} !"));
}
// Get the consumer on the existing queue and consume its messages
var consumer = await _messagingService.GetConsumerAsync("stop.queue");
await consumer.ConsumeAsync((_, args) =>
{
Console.WriteLine("Consumer Working: " + consumer.IsConsuming());
// Process one message max every 10 ms
Thread.Sleep(10);
Console.WriteLine(args.Message.GetBody());
// send ACK: acknowlegdment to the queue
return consumer.AcknowlegdeDeliveredMessageAsync(args).AsTask();
});
// Stop Consuming after 100 ms ~ 10 messages
Thread.Sleep(100);
Console.WriteLine("Consumer Stopping ...");
await consumer.StopConsuming((_, _) =>
{
Console.WriteLine("Consumer Stopped: " + consumer.IsConsuming());
return Task.CompletedTask;
});
}
}