-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
96 lines (79 loc) · 3.12 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NServiceBus;
using Shared;
using Shared.Configuration;
using Shared.Messages;
namespace Sender
{
class Program
{
const int BatchSize = 250;
private static IEndpointInstance endpointInstance;
private static readonly Random random = new Random();
private static readonly Guid[] customers = Customers.GetAllCustomers().ToArray();
static async Task Main(string[] args)
{
DisplayHeader();
var endpointConfiguration = new EndpointConfiguration("Sender")
.ApplyDefaultConfiguration();
endpointInstance = await Endpoint.Start(endpointConfiguration);
Console.ForegroundColor = ConsoleColor.White;
while (true)
{
var key = Console.ReadKey(true);
Console.WriteLine();
switch (key.Key)
{
case ConsoleKey.D1:
await SendMessage();
Console.WriteLine($"Messages sent");
break;
case ConsoleKey.D2:
await SendBatch();
Console.WriteLine($"{BatchSize} messages sent");
break;
case ConsoleKey.Q:
Environment.Exit(0);
break;
}
}
}
private static void DisplayHeader()
{
Console.Title = "Sender - Priority Queues";
var backgroundColor = Console.BackgroundColor;
var foregroundColor = Console.ForegroundColor;
var windowWith = Console.WindowWidth;
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Priority Queues via Publish/Subscribe".PadRight(windowWith - 1));
Console.BackgroundColor = ConsoleColor.Gray;
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine(" [1] Publish a random customer message".PadRight(windowWith - 1));
Console.WriteLine($" [2] Publish {BatchSize} random customer messages".PadRight(windowWith - 1));
Console.WriteLine(" [q] To quit".PadRight(windowWith - 1));
Console.BackgroundColor = backgroundColor;
Console.ForegroundColor = foregroundColor;
}
static async Task SendBatch()
{
var tasks = new List<Task>();
for (int i = 0; i < BatchSize; i++)
{
tasks.Add(SendMessage());
}
await Task.WhenAll(tasks);
}
private static async Task SendMessage()
{
var message = new OrderSubmitted
{
CustomerId = customers[random.Next(customers.Length)]
};
await endpointInstance.Publish(message).ConfigureAwait(false);
}
}
}