-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
67 lines (49 loc) · 1.49 KB
/
Copy pathProgram.cs
File metadata and controls
67 lines (49 loc) · 1.49 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
55
56
57
58
59
60
61
62
63
64
65
66
67
using SquidStd.Messaging.Abstractions.Interfaces;
using SquidStd.Messaging.Extensions;
using SquidStd.Services.Core.Services.Bootstrap;
var bootstrap = SquidStdBootstrap.Create(
new()
{
ConfigName = "squidstd",
RootDirectory = AppContext.BaseDirectory
}
);
#region step-1
bootstrap.ConfigureServices(container => container.AddInMemoryMessaging());
#endregion
await bootstrap.StartAsync();
#region step-2
var queue = bootstrap.Resolve<IMessageQueue>();
using (queue.Subscribe("orders", new OrderListener()))
{
await queue.PublishAsync("orders", new OrderPlaced("order-1"));
await Task.Delay(200);
}
#endregion
#region step-3
var topic = bootstrap.Resolve<IMessageTopic>();
using (topic.Subscribe<OrderPlaced>(
"order-events",
(order, _) =>
{
Console.WriteLine($"topic saw {order.Id}");
return Task.CompletedTask;
}
))
{
await topic.PublishAsync("order-events", new OrderPlaced("order-2"));
await Task.Delay(200);
}
#endregion
await bootstrap.StopAsync();
/// <summary>A sample message.</summary>
public sealed record OrderPlaced(string Id);
/// <summary>Competing-consumers listener for the orders queue.</summary>
public sealed class OrderListener : IQueueMessageListenerAsync<OrderPlaced>
{
public Task HandleAsync(OrderPlaced message, CancellationToken cancellationToken)
{
Console.WriteLine($"queue handled {message.Id}");
return Task.CompletedTask;
}
}