Skip to content

Instantly share code, notes, and snippets.

@profesor79
Created February 19, 2023 02:45
Show Gist options
  • Save profesor79/bf276a0a547b3de1fc7e401f9b32c08f to your computer and use it in GitHub Desktop.
Save profesor79/bf276a0a547b3de1fc7e401f9b32c08f to your computer and use it in GitHub Desktop.
start messaging today
void Main()
{
// we need to create a consumer class instance - or we will have no listener
new ConsumerOurDatabaseAccess();
new WarehouseOurBusinessLogicStuff();
var p = new ProducerOurWebApiEndpoint();
Console.WriteLine("Hey buddy provide order details - and don't play with me!");
while (true)
{
Console.WriteLine("order nr");
if (int.TryParse(Console.ReadLine(), out var orderNr))
{
Console.WriteLine("customer id");
if (int.TryParse(Console.ReadLine(), out var customerId))
{
Console.WriteLine("pushing details to process");
p.AddOrderToProcess(orderNr, customerId);
}
};
Console.WriteLine("Done - ready for new order!");
}
}
// You can define other methods, fields, classes and namespaces here
internal class ConsumerOurDatabaseAccess
{
private Random _r;
public ConsumerOurDatabaseAccess()
{
// we need to run our listener in a new thread as we don't want to block the app
Task.Run(ListenForNewOrders);
_r = new Random();
}
public void ListenForNewOrders()
{
//https://learn.microsoft.com/es-es/dotnet/api/system.collections.concurrent.concurrentqueue-1?view=netcore-3.1
while (true)
{
if (Queues.FirstSetOfMessages.TryDequeue(out var order))
{
Console.WriteLine($"DISPATCHER: order n: {order.OrderNumber}, customer id: {order.CustomerId}, time of entry: {order.TimeCreated}");
Console.WriteLine("DISPATCHER: Getting order details from storage");
Thread.Sleep(_r.Next(3500, 5000));
var items = _r.Next(2, 12);
var msg = new SecondMessagePayload
{
CustomerId = order.CustomerId,
OrderDetails = order,
OrderNumber = order.OrderNumber
};
for (int i = 0; i < items; i++)
{
msg.ProductAmountList.Add(Tuple.Create(_r.Next(1, 9999), _r.NextDouble() * 367));
}
Console.WriteLine("DISPATCHER: Processed order details from storage");
Queues.OrderDetailsQueue.Enqueue(msg);
}
else
{
Thread.Sleep(2000);
}
}
}
}
internal class ProducerOurWebApiEndpoint
{
public ProducerOurWebApiEndpoint()
{
}
public void AddOrderToProcess(int orderNr, int customerId)
{
Queues.FirstSetOfMessages.Enqueue(new FirstMessagePayload
{
OrderNumber = orderNr,
CustomerId = customerId,
});
}
}
public class WarehouseOurBusinessLogicStuff
{
public WarehouseOurBusinessLogicStuff()
{
Task.Run(ListenForNewOrders);
}
private void ListenForNewOrders()
{
while (true)
{
if (Queues.OrderDetailsQueue.TryDequeue(out var order))
{
Console.WriteLine($"WAREHOUSE: order n: {order.OrderNumber}, customer id: {order.CustomerId}, time of entry: {order.TimeOfEntry}");
Console.WriteLine($"WAREHOUSE: order details: {string.Join(", ", order.ProductAmountList)}");
}
}
}
}
internal static class Queues
{
public static ConcurrentQueue<FirstMessagePayload> FirstSetOfMessages { get; private set; } = new ConcurrentQueue<FirstMessagePayload>();
public static ConcurrentQueue<SecondMessagePayload> OrderDetailsQueue { get; private set; } = new ConcurrentQueue<SecondMessagePayload>();
}
public class FirstMessagePayload
{
public int CustomerId { get; set; }
public int OrderNumber { get; set; }
public DateTime TimeCreated { get; set; } = DateTime.UtcNow;
// more stuff here as needed
}
public class SecondMessagePayload
{
public FirstMessagePayload OrderDetails { get; set; }
public List<Tuple<int, double>> ProductAmountList { get; set; } = new List<Tuple<int, double>>();
public int OrderNumber { get; set; }
public int CustomerId { get; set; }
public DateTime TimeOfEntry { get; set; } = DateTime.UtcNow;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment