Skip to content

Instantly share code, notes, and snippets.

@ryhanen
Last active September 4, 2015 14:27
Show Gist options
  • Select an option

  • Save ryhanen/df921bac9d4fd7b8e8aa to your computer and use it in GitHub Desktop.

Select an option

Save ryhanen/df921bac9d4fd7b8e8aa to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Microsoft.ServiceBus.Messaging;
using Newtonsoft.Json;
namespace EventHubTester
{
class MonitoringEventsTest
{
static string eventHubName = "monitoringsource";
static string connectionString = "Add your Event HUB connection string here";
static void Main(string[] args)
{
Console.WriteLine("Press Ctrl-C to stop the sender process");
Console.WriteLine("Press Enter to start now");
Console.ReadLine();
SendEvents();
}
static void SendEvents()
{
var eventHubClient = EventHubClient.CreateFromConnectionString(connectionString, eventHubName);
while (true)
{
try
{
// Process starts when new invoice is received, initial event is sent to monitoring
// Invoice id is used as unique identifier for the process instance, this way it is possible to track whole process from sending the invoice to the payment
var invoiceId = "ABCD1234" + Guid.NewGuid().ToString();
var eventmsg_1 = new Event();
eventmsg_1.Id = Guid.NewGuid();
eventmsg_1.EventId = "event-1";
eventmsg_1.InstanceId = invoiceId;
eventmsg_1.Timestamp = DateTime.Now;
eventmsg_1.Metadatas = new List<EventMetaData>();
// Invoice id is also stored as event metadata
var meta_1 = new EventMetaData();
meta_1.Id = Guid.NewGuid();
meta_1.Key = "InvoiceId";
meta_1.Type = "string";
meta_1.Value = invoiceId;
eventmsg_1.Metadatas.Add(meta_1);
var message = JsonConvert.SerializeObject(eventmsg_1);
eventHubClient.Send(new EventData(Encoding.UTF8.GetBytes(message)));
Thread.Sleep(500);
// Next step in the process is to transform incoive data to Finvoice format, start of the transformation is logged as an event
var eventmsg_2 = new Event();
eventmsg_2.Id = Guid.NewGuid();
eventmsg_2.EventId = "event-2";
eventmsg_2.InstanceId = invoiceId;
eventmsg_2.Timestamp = DateTime.Now;
message = JsonConvert.SerializeObject(eventmsg_2);
eventHubClient.Send(new EventData(Encoding.UTF8.GetBytes(message)));
// Transformation logic goes here
Thread.Sleep(1000);
// Once transformation is done, process state is logged as an event
var eventmsg_3 = new Event();
eventmsg_3.Id = Guid.NewGuid();
eventmsg_3.EventId = "event-3";
eventmsg_3.InstanceId = invoiceId;
eventmsg_3.Timestamp = DateTime.Now;
eventmsg_3.Metadatas = new List<EventMetaData>();
// Another metadata as an example
var meta_2 = new EventMetaData();
meta_2.Id = Guid.NewGuid();
meta_2.Key = "VeryImportantInfo";
meta_2.Type = "string";
meta_2.Value = "FooBar_OMG!";
eventmsg_3.Metadatas.Add(meta_2);
message = JsonConvert.SerializeObject(eventmsg_3);
eventHubClient.Send(new EventData(Encoding.UTF8.GetBytes(message)));
Thread.Sleep(500);
// Transformed invoice data is sent to operator and 1st phase of the process is complete
var eventmsg_4 = new Event();
eventmsg_4.Id = Guid.NewGuid();
eventmsg_4.EventId = "event-4";
eventmsg_4.InstanceId = invoiceId;
eventmsg_4.Timestamp = DateTime.Now;
message = JsonConvert.SerializeObject(eventmsg_4);
eventHubClient.Send(new EventData(Encoding.UTF8.GetBytes(message)));
}
catch (Exception exception)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("{0} > Exception: {1}", DateTime.Now, exception.Message);
Console.ResetColor();
}
Thread.Sleep(4000);
}
}
}
public class Event
{
public Guid Id { get; set; }
public string EventId { get; set; }
public string InstanceId { get; set; }
public DateTime Timestamp { get; set; }
public List<EventMetaData> Metadatas { get; set; }
}
public class EventMetaData
{
public Guid Id { get; set; }
public string Key { get; set; }
public string Type { get; set; }
public string Value { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment