private static EventHubSender CreatePartitionedSender() { // EventHubClient model (uses implicit factory instance, so all links on same connection) EventHubClient eventHubClient = EventHubClient.Create("contoso-trucks"); // return sender for partition '1' return(eventHubClient.CreatePartitionedSender("1")); }
public void InitializeEventHub() { var builder = new ServiceBusConnectionStringBuilder(); builder.Endpoints.Add(new Uri("sb://" + this.appConfig.EventHubNamespace + "." + this.appConfig.EventHubFqnAddress)); builder.EntityPath = this.appConfig.EventHubEntityPath; builder.SharedAccessKeyName = this.appConfig.EventHubUsername; builder.SharedAccessKey = this.appConfig.EventHubPassword; builder.TransportType = TransportType.Amqp; Context.Logger.Info("EventHubWriter: ConnectionString = {0} ParitionId = {1}", builder.ToString(), Context.TopologyContext.GetThisTaskIndex()); eventHubClient = EventHubClient.CreateFromConnectionString(builder.ToString()); //TODO: Implement a distribution strategy of partitions in case number of bolt tasks is less than partitions in EventHub eventHubSender = eventHubClient.CreatePartitionedSender(Context.TopologyContext.GetThisTaskIndex().ToString()); }
public void Scenario2_EventHubSendToPartition(string eventHubEntity, string partitionId) { ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(this.ConnectionString); builder.TransportType = TransportType.Amqp; MessagingFactory factory = MessagingFactory.CreateFromConnectionString(this.ConnectionString); EventHubClient client = factory.CreateEventHubClient(eventHubEntity); EventHubSender sender = client.CreatePartitionedSender(partitionId); EventData data = new EventData(Encoding.UTF8.GetBytes("Body")); data.Properties["time"] = DateTime.UtcNow; sender.Send(data); sender.Close(); client.Close(); factory.Close(); }
/// <summary> /// Publish 10 Event Hub messages /// </summary> /// <param name="eventHubClient">EventHubClient object</param> /// <returns>Task object</returns> private async Task Publish10MessagesToEventHub(EventHubClient eventHubClient) { int maxMessagesToPublish = 10; int successMessageSentCounter = 0; Console.WriteLine($"About to publish [{maxMessagesToPublish}] message !\n"); for (var counter = 0; counter < maxMessagesToPublish; counter++) { try { var message = $"Sample event message {counter + 1}"; Console.WriteLine($"\tPublishing message content of [{message}]"); var eventData = new EventData(Encoding.UTF8.GetBytes(message)); EventHubSender eventHubSender = eventHubClient.CreatePartitionedSender(PARTITION_ID); await eventHubSender.SendAsync(eventData); Console.WriteLine($"\tPublishing message [{counter + 1}] is completed !\n"); successMessageSentCounter++; } catch (Exception exception) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Error occurred !"); Console.WriteLine(exception); } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Delay 1 seconds for the publish the next message !\n"); await Task.Delay(1000); // Delay 1 seconds for the publish the next message } if (successMessageSentCounter > 0) { Console.WriteLine($"[{successMessageSentCounter}] messages published successfully !\n"); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("No message published !"); } }
internal static void SendMessageToEventHub(string payload) { string connectionString = "Endpoint=sb://kioskeventhub.servicebus.windows.net/;SharedAccessKeyName=AllowSend;SharedAccessKey=tvzonqLkFUm+9AXlT/rq8Fmh0HlzND7MwOiGW6r0TNo="; ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(connectionString); builder.TransportType = TransportType.Amqp; if (factory == null) { factory = MessagingFactory.CreateFromConnectionString(connectionString); } if (client == null) { client = factory.CreateEventHubClient("hub1"); } EventHubSender sender = client.CreatePartitionedSender("partition"); EventData data = new EventData(Encoding.UTF8.GetBytes(payload)); data.Properties["time"] = DateTime.UtcNow; sender.Send(data); sender.Close(); }
public void InitializeEventHub() { Context.Logger.Info("Initializing EventHubClient and EventHubSender..."); var builder = new ServiceBusConnectionStringBuilder(); builder.Endpoints.Add(new Uri("sb://" + this.appConfig.EventHubNamespace + "." + this.appConfig.EventHubFqnAddress)); builder.EntityPath = this.appConfig.EventHubEntityPath; builder.SharedAccessKeyName = this.appConfig.EventHubSharedAccessKeyName; builder.SharedAccessKey = this.appConfig.EventHubSharedAccessKey; builder.TransportType = TransportType.Amqp; Context.Logger.Info("EventHubWriter: ConnectionString = {0} ParitionId = {1}", builder.ToString(), Context.TopologyContext.GetThisTaskIndex()); eventHubClient = EventHubClient.CreateFromConnectionString(builder.ToString()); //TODO: Implement a distribution strategy of partitions in case number of bolt tasks is less than partitions in EventHub eventHubSender = eventHubClient.CreatePartitionedSender(Context.TopologyContext.GetThisTaskIndex().ToString()); }
public void InitializeEventHub() { Context.Logger.Info("Current AppConfig File: " + ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)); Context.Logger.Info("Current AppSettings: " + String.Join(Environment.NewLine, ConfigurationManager.AppSettings.AllKeys)); this.EventHubNamespace = ConfigurationManager.AppSettings["EventHubNamespace"]; if (String.IsNullOrWhiteSpace(this.EventHubNamespace)) { throw new ArgumentException("A required AppSetting cannot be null or empty", "EventHubNamespace"); } this.EventHubEntityPath = ConfigurationManager.AppSettings["EventHubEntityPath"]; if (String.IsNullOrWhiteSpace(this.EventHubEntityPath)) { throw new ArgumentException("A required AppSetting cannot be null or empty", "EventHubEntityPath"); } this.EventHubSharedAccessKeyName = ConfigurationManager.AppSettings["EventHubSharedAccessKeyName"]; if (String.IsNullOrWhiteSpace(this.EventHubSharedAccessKeyName)) { throw new ArgumentException("A required AppSetting cannot be null or empty", "EventHubSharedAccessKeyName"); } this.EventHubSharedAccessKey = ConfigurationManager.AppSettings["EventHubSharedAccessKey"]; if (String.IsNullOrWhiteSpace(this.EventHubSharedAccessKey)) { throw new ArgumentException("A required AppSetting cannot be null or empty", "EventHubSharedAccessKey"); } this.EventHubPartitions = ConfigurationManager.AppSettings["EventHubPartitions"]; if (String.IsNullOrWhiteSpace(this.EventHubPartitions)) { throw new ArgumentException("A required AppSetting cannot be null or empty", "EventHubPartitions"); } var builder = new ServiceBusConnectionStringBuilder(); builder.Endpoints.Add(new Uri("sb://" + this.EventHubNamespace + "." + EventHubFqnAddress)); builder.EntityPath = this.EventHubEntityPath; builder.SharedAccessKeyName = this.EventHubSharedAccessKeyName; builder.SharedAccessKey = this.EventHubSharedAccessKey; builder.TransportType = TransportType.Amqp; var partitionCount = int.Parse(this.EventHubPartitions); TopologyContext topologyContext = Context.TopologyContext; Context.Logger.Info(this.GetType().Name + " TopologyContext info:"); Context.Logger.Info("TaskId: {0}", topologyContext.GetThisTaskId()); var taskIndex = topologyContext.GetThisTaskIndex(); Context.Logger.Info("TaskIndex: {0}", taskIndex); string componentId = topologyContext.GetThisComponentId(); Context.Logger.Info("ComponentId: {0}", componentId); List<int> componentTasks = topologyContext.GetComponentTasks(componentId); Context.Logger.Info("ComponentTasks: {0}", componentTasks.Count); if (partitionCount != componentTasks.Count) { throw new Exception( String.Format("Component task count does not match partition count. Component: {0}, Tasks: {1}, Partition: {2}", componentId, componentTasks.Count, partitionCount)); } partitionId = taskIndex.ToString(); Context.Logger.Info(this.GetType().Name + " ConnectionString = {0}, ParitionId = {1}", builder.ToString(), partitionId); eventHubClient = EventHubClient.CreateFromConnectionString(builder.ToString()); eventHubSender = eventHubClient.CreatePartitionedSender(partitionId); }
public void InitializeEventHub() { Context.Logger.Info("Current AppConfig File: " + ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)); Context.Logger.Info("Current AppSettings: " + String.Join(Environment.NewLine, ConfigurationManager.AppSettings.AllKeys)); this.EventHubNamespace = ConfigurationManager.AppSettings["EventHubNamespace"]; if (String.IsNullOrWhiteSpace(this.EventHubNamespace)) { throw new ArgumentException("A required AppSetting cannot be null or empty", "EventHubNamespace"); } this.EventHubEntityPath = ConfigurationManager.AppSettings["EventHubEntityPath"]; if (String.IsNullOrWhiteSpace(this.EventHubEntityPath)) { throw new ArgumentException("A required AppSetting cannot be null or empty", "EventHubEntityPath"); } this.EventHubSharedAccessKeyName = ConfigurationManager.AppSettings["EventHubSharedAccessKeyName"]; if (String.IsNullOrWhiteSpace(this.EventHubSharedAccessKeyName)) { throw new ArgumentException("A required AppSetting cannot be null or empty", "EventHubSharedAccessKeyName"); } this.EventHubSharedAccessKey = ConfigurationManager.AppSettings["EventHubSharedAccessKey"]; if (String.IsNullOrWhiteSpace(this.EventHubSharedAccessKey)) { throw new ArgumentException("A required AppSetting cannot be null or empty", "EventHubSharedAccessKey"); } this.EventHubPartitions = ConfigurationManager.AppSettings["EventHubPartitions"]; if (String.IsNullOrWhiteSpace(this.EventHubPartitions)) { throw new ArgumentException("A required AppSetting cannot be null or empty", "EventHubPartitions"); } var builder = new ServiceBusConnectionStringBuilder(); builder.Endpoints.Add(new Uri("sb://" + this.EventHubNamespace + "." + EventHubFqnAddress)); builder.EntityPath = this.EventHubEntityPath; builder.SharedAccessKeyName = this.EventHubSharedAccessKeyName; builder.SharedAccessKey = this.EventHubSharedAccessKey; builder.TransportType = TransportType.Amqp; var partitionCount = int.Parse(this.EventHubPartitions); TopologyContext topologyContext = Context.TopologyContext; Context.Logger.Info(this.GetType().Name + " TopologyContext info:"); Context.Logger.Info("TaskId: {0}", topologyContext.GetThisTaskId()); var taskIndex = topologyContext.GetThisTaskIndex(); Context.Logger.Info("TaskIndex: {0}", taskIndex); string componentId = topologyContext.GetThisComponentId(); Context.Logger.Info("ComponentId: {0}", componentId); List <int> componentTasks = topologyContext.GetComponentTasks(componentId); Context.Logger.Info("ComponentTasks: {0}", componentTasks.Count); if (partitionCount != componentTasks.Count) { throw new Exception( String.Format("Component task count does not match partition count. Component: {0}, Tasks: {1}, Partition: {2}", componentId, componentTasks.Count, partitionCount)); } partitionId = taskIndex.ToString(); Context.Logger.Info(this.GetType().Name + " ConnectionString = {0}, ParitionId = {1}", builder.ToString(), partitionId); eventHubClient = EventHubClient.CreateFromConnectionString(builder.ToString()); eventHubSender = eventHubClient.CreatePartitionedSender(partitionId); }