예제 #1
0
        public static void SetupEventHubs()
        {
            Console.WriteLine("Create 'Entry' Event Hub");
            EventHubHelper.CreateEventHubIfNotExists(
                EventHubConnectionString,
                EntryEventHubPath);

            Console.WriteLine("Create 'Exit' Event Hub");
            EventHubHelper.CreateEventHubIfNotExists(
                EventHubConnectionString,
                ExitEventHubPath);
        }
예제 #2
0
 public static void Cleanup()
 {
     EventHubHelper.DeleteAllEventHubs(EventHubConnectionString);
 }
예제 #3
0
        public static void SendData(string serviceBusConnectionString, string entryHubName, string exitHubName, bool addPolicy = false)
        {
            var entryEventHub = EventHubClient.CreateFromConnectionString(serviceBusConnectionString, entryHubName);
            var exitEventHub  = EventHubClient.CreateFromConnectionString(serviceBusConnectionString, exitHubName);

            var timerInterval = TimeSpan.FromSeconds(1);
            var generator     = TollDataGenerator.Generator();

            if (addPolicy)
            {
                EventHubHelper.AddManagementPolicy(serviceBusConnectionString, entryHubName);
                EventHubHelper.AddManagementPolicy(serviceBusConnectionString, exitHubName);
            }

            TimerCallback timerCallback = state =>
            {
                var startTime = DateTime.UtcNow;
                generator.Next(startTime, timerInterval, 5);

                foreach (var e in generator.GetEvents(startTime))
                {
                    if (e is EntryEvent)
                    {
                        entryEventHub.Send(
                            new EventData(Encoding.UTF8.GetBytes(e.Format()))
                        {
                            PartitionKey = e.TollId.ToString()
                        });
                    }
                    else
                    {
                        exitEventHub.Send(
                            new EventData(Encoding.UTF8.GetBytes(e.Format()))
                        {
                            PartitionKey = e.TollId.ToString()
                        });
                    }
                }

                timer.Change((int)timerInterval.TotalMilliseconds, Timeout.Infinite);
            };

            timer = new Timer(timerCallback, null, Timeout.Infinite, Timeout.Infinite);
            timer.Change(0, Timeout.Infinite);

            Console.WriteLine("Sending event hub data... Press Ctrl+c to stop.");

            var exitEvent = new ManualResetEvent(false);

            Console.CancelKeyPress += (sender, eventArgs) =>
            {
                eventArgs.Cancel = true;
                exitEvent.Set();
            };

            exitEvent.WaitOne();

            timer.Change(Timeout.Infinite, Timeout.Infinite);
            Thread.Sleep(timerInterval);
            timer.Dispose();
            entryEventHub.Close();
            exitEventHub.Close();
        }