Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
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 SendOptions".PadRight(windowWith - 1));
Console.BackgroundColor = ConsoleColor.Gray;
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine(" [1] Send a random customer message".PadRight(windowWith - 1));
Console.WriteLine($" [2] Send {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 customerId = customers[random.Next(customers.Length)];
var destination = Customers.GetPriorityCustomers().Contains(customerId) ? "StrategicReceiver" : "RegularReceiver";
var message = new SubmitOrder
{
CustomerId = customerId
};
var sendOptions = new SendOptions();
sendOptions.SetDestination(destination);
await endpointInstance.Send(message, sendOptions).ConfigureAwait(false);
}
}
}