-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
83 lines (75 loc) · 3.08 KB
/
Program.cs
File metadata and controls
83 lines (75 loc) · 3.08 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using Microsoft.Azure.ServiceBus;
using Models;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Text;
using System.Threading;
namespace EventGenerator
{
class Program
{
// How many duplicate messages the emulator
// should emit
private const double duplicationFactor = 0.01;
private const double MaxWalkDurationInMinutes = 60.0;
static string eventHubName;
static string connectionString;
static Random rnd = new Random();
static void Main(string[] args)
{
var config = File.ReadAllText("secrets.json");
dynamic configObject = JsonConvert.DeserializeObject(config);
eventHubName = configObject.EventHubName;
connectionString = configObject.ConnectionString;
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine("Press Ctrl-C to stop the sender process");
Console.WriteLine("Press Enter to start now");
Console.ReadLine();
SendRandomMessages();
}
static void SendRandomMessages()
{
// Create a Service Bus queue client to send messages to the Service Bus queue.
var eventHubClient = new QueueClient(connectionString, eventHubName);
Int64 count = 0;
while (true)
{
try
{
var generatedMessage = GenerateMessage();
var jsonmessage = JsonConvert.SerializeObject(generatedMessage);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, jsonmessage);
eventHubClient.SendAsync(new Message(Encoding.UTF8.GetBytes(jsonmessage))).GetAwaiter().GetResult(); // Use synchronous send to preserve message ordering.
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("{0} messages sent!", ++count);
}
catch (Exception exception)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("{0} > Exception: {1}", DateTime.Now, exception.Message);
Console.ResetColor();
}
Thread.Sleep(200);
}
}
private static Guid lastId = Guid.NewGuid();
public static DogWalk GenerateMessage()
{
// If not send duplicate, change the outgoing ID
if (rnd.NextDouble() > duplicationFactor)
{
lastId = Guid.NewGuid();
}
// Allow missing dog ids and pet sitter ids
return new DogWalk()
{
Id = lastId,
DogId = rnd.Next((int)Dog.GENERATION_MIN_ID, (int)Dog.GENERATION_MAX_ID + 100),
PetSitterId = rnd.Next((int)PetSitter.GENERATION_MIN_ID, (int)PetSitter.GENERATION_MAX_ID + 100),
WalkDurationInMinutes = MaxWalkDurationInMinutes * rnd.NextDouble()
};
}
}
}