static void Main(string[] args) { Console.WriteLine("Usage: \r\n" + "ConsoleMonitor <iotHubOwner> [<consumerGroup>] [<device>] "); string connection = args[0]; string consumerGroupName = "$Default"; if (args.Length > 1) { consumerGroupName = args[1]; } string deviceName = ""; if (args.Length > 2) { deviceName = args[2]; } EventHubClient eventHubClient = null; EventHubReceiver eventHubReceiver = null; eventHubClient = EventHubClient.CreateFromConnectionString(connection, "messages/events"); var ri = eventHubClient.GetRuntimeInformation(); if (deviceName != "") { string partition = EventHubPartitionKeyResolver.ResolveToPartition(deviceName, ri.PartitionCount); eventHubReceiver = eventHubClient.GetConsumerGroup(consumerGroupName). CreateReceiver(partition, DateTime.Now); Task.Run(() => EventLoopAsync(eventHubReceiver)); } else { EventHubReceiver[] eventHubReceivers = new EventHubReceiver[ri.PartitionCount]; int i = 0; foreach (var partition in ri.PartitionIds) { eventHubReceivers[i] = eventHubClient.GetConsumerGroup(consumerGroupName).CreateReceiver(partition, DateTime.Now); //Task.Run(() => eventLoop(eventHubReceivers[i])); <- very common bug! var r = eventHubReceivers[i]; Task.Run(() => EventLoopAsync(r)); i++; } } Console.ReadLine(); }
public async Task Setup(NetworkParameters parameters) { eventHubClient = EventHubClient.CreateFromConnectionString(parameters.ConnectionString, "messages/events"); Console.WriteLine("Created client"); var runtimeinfo = eventHubClient.GetRuntimeInformation(); partitionsCount = runtimeinfo.PartitionCount; var partition = EventHubPartitionKeyResolver.ResolveToPartition(parameters.DeviceName, partitionsCount); Console.WriteLine("Got partition"); if (parameters.StartTime == null) { parameters.StartTime = DateTime.Now; } eventHubReceiver = eventHubClient.GetConsumerGroup(parameters.ConsumerGroupName).CreateReceiver(partition, parameters.StartTime); Console.WriteLine("Created reciever"); var pastEvents = await eventHubReceiver.ReceiveAsync(int.MaxValue, TimeSpan.FromSeconds(3)); int cnt = 0; foreach (var ev in pastEvents) { cnt++; } Console.WriteLine("Got {0} events from past", cnt); }
private EventHubReceiver CreateEventHubReceiver(string deviceName, out EventHubClient eventHubClient) { EventHubReceiver eventHubReceiver = null; Stopwatch sw = new Stopwatch(); sw.Start(); eventHubClient = EventHubClient.CreateFromConnectionString(Configuration.IoTHub.ConnectionString, "messages/events"); var eventHubPartitionsCount = eventHubClient.GetRuntimeInformation().PartitionCount; string partition = EventHubPartitionKeyResolver.ResolveToPartition(deviceName, eventHubPartitionsCount); string consumerGroupName = Configuration.IoTHub.EventHubConsumerGroup; while (eventHubReceiver == null && sw.Elapsed.Minutes < 1) { try { eventHubReceiver = eventHubClient.GetConsumerGroup(consumerGroupName).CreateReceiver(partition, DateTime.Now, FaultInjection.EventHubEpoch++); } catch (QuotaExceededException ex) { Debug.WriteLine(ex); } } sw.Stop(); return(eventHubReceiver); }
private static void ReceiveDirectFromPartition( EventHubClient eventHubClient, string partitionId, string consumerGroup) { try { var group = eventHubClient.GetConsumerGroup(consumerGroup); EventHubReceiver receiver = null; receiver = group.CreateReceiver(partitionId, DateTime.UtcNow); LogEngine.TraceInformation($"Direct Receiver created. Partition {partitionId}"); while (true) { var message = receiver?.Receive(); if (message != null) { BubblingObject bubblingObject = BubblingObject.DeserializeMessage(message.GetBytes()); MessageIngestor.IngestMessagge(bubblingObject); } } } catch (Exception ex) { LogEngine.TraceError($"Error in {MethodBase.GetCurrentMethod().Name} - Error {ex.Message}"); } }
/// <summary> /// Execute the Command /// </summary> /// <returns></returns> public Task Execute() { var t = Task.Run(() => { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Message Receiving ...\n"); var d2cPartitions = m_EventHubClient.GetRuntimeInformation().PartitionIds; foreach (string partition in d2cPartitions) { try { var eventHubReceiver = m_EventHubClient.GetConsumerGroup(m_ConsumerGroup).CreateReceiver(partition, m_StartTime); Console.WriteLine($"Connected to partition {partition}"); var n = receiveMessagesAsync(eventHubReceiver, partition); } catch (Exception) { throw; } //partitionTask.ContinueWith((task) => //{ // Console.WriteLine(task.Exception.Message); //},TaskContinuationOptions.OnlyOnFaulted); //Console.WriteLine($"Connected to partition {partition}"); } }); //m_Event.WaitOne(); return(t); }
public void Run(string connectionString, string hubName) { NamespaceManager nsmgr = NamespaceManager.CreateFromConnectionString(connectionString); EventHubDescription desc = nsmgr.GetEventHub(hubName); string consumerGroupName = _consumerGroupPrefix + DateTime.UtcNow.Ticks.ToString(); ConsumerGroupDescription consumerGroupDesc = nsmgr.CreateConsumerGroupIfNotExists(new ConsumerGroupDescription(hubName, consumerGroupName)); EventHubClient client = EventHubClient.CreateFromConnectionString(connectionString, hubName); int numPartitions = desc.PartitionCount; _receivers = new EventHubReceiver[numPartitions]; //_receiversLastUpdate = new DateTime[numPartitions]; //for (int i = 0; i < numPartitions; i++) //{ // _receiversLastUpdate[i] = DateTime.UtcNow; //} _tasks = new Task[numPartitions]; _buffers = new Dictionary <string, CircularBuffer <SensorDataContract> >(); for (int iPart = 0; iPart < desc.PartitionCount; iPart++) { EventHubReceiver receiver = client.GetConsumerGroup(consumerGroupName).CreateReceiver( desc.PartitionIds[iPart], DateTime.UtcNow - TimeSpan.FromMinutes(2)); _receivers[iPart] = receiver; Task <IEnumerable <EventData> > task = receiver.ReceiveAsync(1000, TimeSpan.FromSeconds(1)); int thisPart = iPart; task.ContinueWith(new Action <Task <IEnumerable <EventData> > >((t) => OnTaskComplete(t, thisPart))); _tasks[iPart] = task; } }
public async Task<string> OpenAsync(CancellationToken cancellationToken) { var builder = new ServiceBusConnectionStringBuilder(_connectionString) { TransportType = TransportType.Amqp }; _messagingFactory = MessagingFactory.CreateFromConnectionString(builder.ToString()); _eventHubClient = _messagingFactory.CreateEventHubClient(_eventHubName); _consumerGroup = !string.IsNullOrEmpty(_consumerGroupName) ? _eventHubClient.GetConsumerGroup(_consumerGroupName) : _eventHubClient.GetDefaultConsumerGroup(); _eventProcessorFactory = new EventProcessorFactory(); _leaseRepository = new ReliableStateLeaseRepository(_reliableStateManager); _checkpointManager = new CheckpointManager(_leaseRepository); var allocatedPartitions = await new EventHubPartitionPartitionAllocationStrategy(_serviceName, _partitionId) .AllocateAsync(_eventHubClient, new FabricClient()); foreach (var partition in allocatedPartitions) { var lease = await _leaseRepository.GetOrCreateAsync(_connectionString, _consumerGroupName, _eventHubName, partition); await _consumerGroup.RegisterProcessorFactoryAsync(lease, _checkpointManager, _eventProcessorFactory); } return string.Concat(_eventHubName, " @ ", _connectionString); }
private EventHubReceiver CreateEventHubReceiver(string deviceName, out EventHubClient eventHubClient) { EventHubReceiver eventHubReceiver = null; Stopwatch sw = new Stopwatch(); sw.Start(); eventHubClient = EventHubClient.CreateFromConnectionString(hubConnectionString, "messages/events"); var eventHubPartitionsCount = eventHubClient.GetRuntimeInformation().PartitionCount; string partition = EventHubPartitionKeyResolver.ResolveToPartition(deviceName, eventHubPartitionsCount); string consumerGroupName = Environment.GetEnvironmentVariable("IOTHUB_EVENTHUB_CONSUMER_GROUP") ?? "$Default"; while (eventHubReceiver == null && sw.Elapsed.Minutes < 1) { try { eventHubReceiver = eventHubClient.GetConsumerGroup(consumerGroupName).CreateReceiver(partition, DateTime.Now); } catch (QuotaExceededException ex) { } } sw.Stop(); return(eventHubReceiver); }
private static async Task <EventHubReceiver> CreateReceiver(EventHubPartitionSettings partitionSettings, string offset, Logger logger) { bool offsetInclusive = true; EventHubClient client = EventHubClient.CreateFromConnectionString(partitionSettings.Hub.ConnectionString, partitionSettings.Hub.Path); EventHubConsumerGroup consumerGroup = client.GetConsumerGroup(partitionSettings.Hub.ConsumerGroup); if (partitionSettings.Hub.PrefetchCount.HasValue) { consumerGroup.PrefetchCount = partitionSettings.Hub.PrefetchCount.Value; } // if we have a starting offset or if we're not configured to start reading from utc now, read from offset if (!partitionSettings.Hub.StartFromNow || offset != EventHubConsumerGroup.StartOfStream) { logger.Info("Starting to read from EventHub partition {0}-{1} at offset {2}", partitionSettings.Hub.Path, partitionSettings.Partition, offset); } else { // to start reading from most recent data, we get the latest offset from the partition. PartitionRuntimeInformation patitionInfo = await client.GetPartitionRuntimeInformationAsync(partitionSettings.Partition); offset = patitionInfo.LastEnqueuedOffset; offsetInclusive = false; logger.Info("Starting to read latest messages from EventHub partition {0}-{1} at offset {2}", partitionSettings.Hub.Path, partitionSettings.Partition, offset); } return(await consumerGroup.CreateReceiverAsync(partitionSettings.Partition, offset, offsetInclusive)); }
async public Task <string> Get(string id) { var iot = new MessageHub(); CancellationTokenSource tokenSource = new CancellationTokenSource(); CancellationToken token = tokenSource.Token; var context = GlobalHost.ConnectionManager.GetHubContext <MessageHub>(); string partition = EventHubPartitionKeyResolver.ResolveToPartition("testDevice180221", eventHubPartitionsCount); var eventHubReceiver = eventHubClient.GetConsumerGroup(consumerGroupName).CreateReceiver(partition, DateTime.UtcNow); while (true) { var eventData = await eventHubReceiver.ReceiveAsync(TimeSpan.FromSeconds(1)); if (eventData != null) { var data = Encoding.UTF8.GetString(eventData.GetBytes()); var enqueuedTime = eventData.EnqueuedTimeUtc.ToLocalTime(); // Display only data from the selected device; otherwise, skip. var connectionDeviceId = eventData.SystemProperties["iothub-connection-device-id"].ToString(); if (string.CompareOrdinal("testDevice180221", connectionDeviceId) == 0) { context.Clients.Client(id).broadcastMessage(enqueuedTime.ToShortDateString(), connectionDeviceId, data); //context.Clients.Client.Send(enqueuedTime.ToShortDateString(), connectionDeviceId, data); } } } return(""); }
private async void MonitorEventHubAsync(DateTime startTime, CancellationToken ct, string consumerGroupName) { EventHubClient eventHubClient = null; EventHubReceiver eventHubReceiver = null; try { eventHubClient = EventHubClient.CreateFromConnectionString(activeIoTHubConnectionString, "messages/events"); eventHubTextBox.Text = String.Format("Receiving events...\r\n"); eventHubPartitionsCount = eventHubClient.GetRuntimeInformation().PartitionCount; string partition = EventHubPartitionKeyResolver.ResolveToPartition(deviceIDsComboBoxForEvent.SelectedItem.ToString(), eventHubPartitionsCount); eventHubReceiver = eventHubClient.GetConsumerGroup(consumerGroupName).CreateReceiver(partition, startTime); while (true) { ct.ThrowIfCancellationRequested(); EventData eventData = await eventHubReceiver.ReceiveAsync(TimeSpan.FromSeconds(1)); if (eventData != null) { string data = Encoding.UTF8.GetString(eventData.GetBytes()); DateTime enqueuedTime = eventData.EnqueuedTimeUtc.ToLocalTime(); eventHubTextBox.Text += String.Format("{0}> Data:[{1}]", enqueuedTime, data); if (eventData.Properties.Count > 0) { eventHubTextBox.Text += "Properties:\r\n"; foreach (var property in eventData.Properties) { eventHubTextBox.Text += String.Format("'{0}': '{1}'\r\n", property.Key, property.Value); } } eventHubTextBox.Text += "\r\n"; } } } catch (Exception ex) { if (ct.IsCancellationRequested) { eventHubTextBox.Text += String.Format("Stopped Monitoring events. {0}\r\n", ex.Message); } else { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); eventHubTextBox.Text += String.Format("Stopped Monitoring events. {0}\r\n", ex.Message); } if (eventHubReceiver != null) { eventHubReceiver.Close(); } if (eventHubClient != null) { eventHubClient.Close(); } dataMonitorButton.Enabled = true; deviceIDsComboBoxForEvent.Enabled = true; cancelMonitoringButton.Enabled = false; } }
public static Task <EventHubTestListener> CreateListenerPal(string deviceName) { EventHubReceiver receiver = null; Stopwatch sw = new Stopwatch(); sw.Start(); EventHubClient eventHubClient = EventHubClient.CreateFromConnectionString(Configuration.IoTHub.ConnectionString, "messages/events"); var eventHubPartitionsCount = eventHubClient.GetRuntimeInformation().PartitionCount; string partition = EventHubPartitionKeyResolver.ResolveToPartition(deviceName, eventHubPartitionsCount); string consumerGroupName = Configuration.IoTHub.EventHubConsumerGroup; while (receiver == null && sw.Elapsed.TotalMinutes < MaximumWaitTimeInMinutes) { try { receiver = eventHubClient.GetConsumerGroup(consumerGroupName).CreateReceiver(partition, DateTime.Now.AddMinutes(-LookbackTimeInMinutes)); } catch (QuotaExceededException ex) { s_log.WriteLine($"{nameof(EventHubTestListener)}.{nameof(CreateListener)}: Cannot create receiver: {ex}"); } } sw.Stop(); return(Task.FromResult(new EventHubTestListener(receiver))); }
public static void CreateListenerPalAndReceiveMessages() { EventHubClient eventHubClient = EventHubClient.CreateFromConnectionString(Configuration.IoTHub.ConnectionString, "messages/events"); var eventRuntimeInformation = eventHubClient.GetRuntimeInformation(); string consumerGroupName = Configuration.IoTHub.EventHubConsumerGroup; foreach (string partitionId in eventRuntimeInformation.PartitionIds) { try { EventHubReceiver receiver = eventHubClient.GetConsumerGroup(consumerGroupName).CreateReceiver(partitionId, DateTime.Now.AddMinutes(-LookbackTimeInMinutes)); s_log.WriteLine($"EventHub receiver created for partition {partitionId}, listening from {LookbackTimeInMinutes}"); new Task(async() => { while (true) { IEnumerable <EventData> eventDatas = await receiver.ReceiveAsync(int.MaxValue, TimeSpan.FromSeconds(OperationTimeoutInSeconds)).ConfigureAwait(false); ProcessEventData(eventDatas); } }).Start(); } catch (QuotaExceededException ex) { s_log.WriteLine($"{nameof(EventHubTestListener)}.{nameof(CreateListenerPalAndReceiveMessages)}: Cannot create receiver for partitionID {partitionId}: {ex}"); } } }
public static async Task ReadTelemetry(string deviceId, CancellationToken ct) { EventHubClient eventHubClient = null; EventHubReceiver eventHubReceiver = null; var consumerGroup = ConfigurationManager.AppSettings["ConsumerGroup"]; try { var ioTHubConnectionString = ConfigurationManager.AppSettings["IOTHubConnectionString"]; eventHubClient = EventHubClient.CreateFromConnectionString(ioTHubConnectionString, "messages/events"); //var ioTHubConnectionString = // "Endpoint=sb://ihsuprodhkres004dednamespace.servicebus.windows.net/;SharedAccessKeyName=iothubowner;SharedAccessKey=Rte+iSJejSjnT4pXbdbGoRd786APJGiX/5pEkk1mAU8="; //eventHubClient = EventHubClient.CreateFromConnectionString(ioTHubConnectionString, // "iothub-ehub-raghuhub-1063-f06377c774"); Console.WriteLine("Receiving events...\r\n"); var eventHubPartitionsCount = eventHubClient.GetRuntimeInformation().PartitionCount; string partition = EventHubPartitionKeyResolver.ResolveToPartition(deviceId, eventHubPartitionsCount); eventHubReceiver = eventHubClient.GetConsumerGroup(consumerGroup).CreateReceiver(partition); while (!ct.IsCancellationRequested) { EventData eventData = await eventHubReceiver.ReceiveAsync(TimeSpan.FromSeconds(10)); if (eventData != null) { Console.WriteLine("Received Message : "); Console.WriteLine(Encoding.UTF8.GetString(eventData.GetBytes())); } } } catch (Exception exception) { Console.WriteLine(exception); } }
/// <summary> /// Receive 10 Event Hub messages /// </summary> /// <param name="eventHubClient">EventHubClient object</param> /// <returns>Task object</returns> private async Task Receive10EventHubMessage(EventHubClient eventHubClient) { int maxMessagesToReceive = 10; int successMessageReceiveCounter = 0; int recentPublishedMessageTimeIntervalInMinutes = 10; // Helps to get only the past 10 minutes published messages Console.WriteLine($"About to receive [{maxMessagesToReceive}] message published the past [{recentPublishedMessageTimeIntervalInMinutes}] minutes !\n"); try { EventHubConsumerGroup eventHubConsumerGroup = eventHubClient.GetConsumerGroup(ConfigurationManager.AppSettings.Get("EventHubConsumerGroup")); EventHubReceiver eventHubReceiver = eventHubConsumerGroup.CreateReceiver(PARTITION_ID, DateTime.UtcNow.AddMinutes(-recentPublishedMessageTimeIntervalInMinutes)); IEnumerable <EventData> eventDatum = eventHubReceiver.Receive(maxMessagesToReceive); if (eventDatum == null) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Error occurred !"); Console.WriteLine("Event data is null !"); } else { foreach (EventData eventData in eventDatum) { successMessageReceiveCounter++; string offset = eventData.Offset; string content = Encoding.UTF8.GetString(eventData.GetBytes()); if (!string.IsNullOrWhiteSpace(offset) && !string.IsNullOrWhiteSpace(content)) { Console.WriteLine($"\tReceived message offset - [{offset}]"); Console.WriteLine($"\tReceived message content - [{content}]\n"); Console.WriteLine("Delay 2 seconds to receive the next message !\n"); await Task.Delay(2000); // Delay 2 seconds to receive the next message } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Event data has no message content !"); } } if (successMessageReceiveCounter > 0) { Console.WriteLine($"[{successMessageReceiveCounter}] messages received successfully !\n"); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("No message received !"); } } } catch (Exception exception) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Error occurred !"); Console.WriteLine(exception); } }
private async Task<EventHubReceiver> CreateEventHubReceiver(string deviceName) { EventHubClient eventHubClient = EventHubClient.CreateFromConnectionString(hubConnectionString, "messages/events"); var eventHubPartitions = await eventHubClient.GetRuntimeInformationAsync(); var eventHubPartitionsCount = eventHubPartitions.PartitionCount; string partition = EventHubPartitionKeyResolver.ResolveToPartition(deviceName, eventHubPartitionsCount); string consumerGroupName = Configuration.IoTHub.ConsumerGroup; return eventHubClient.GetConsumerGroup(consumerGroupName).CreateReceiver(partition, DateTime.Now, TestUtil.EventHubEpoch++); }
private EventHubReceiver CreateEventHubReceiver(string deviceName, out EventHubClient eventHubClient) { eventHubClient = EventHubClient.CreateFromConnectionString(hubConnectionString, "messages/events"); var eventHubPartitionsCount = eventHubClient.GetRuntimeInformation().PartitionCount; string partition = EventHubPartitionKeyResolver.ResolveToPartition(deviceName, eventHubPartitionsCount); string consumerGroupName = Environment.GetEnvironmentVariable("IOTHUB_EVENTHUB_CONSUMER_GROUP") ?? "$Default"; return(eventHubClient.GetConsumerGroup(consumerGroupName).CreateReceiver(partition, DateTime.Now)); }
private async void MonitorEventHubAsync(DateTime startTime, CancellationToken ct, string consumerGroupName) { EventHubClient eventHubClient = null; EventHubReceiver eventHubReceiver = null; try { eventHubClient = EventHubClient.CreateFromConnectionString(activeIoTHubConnectionString, "messages/events"); int eventHubPartitionsCount = eventHubClient.GetRuntimeInformation().PartitionCount; string partition = EventHubPartitionKeyResolver.ResolveToPartition(deviceID, eventHubPartitionsCount); eventHubReceiver = eventHubClient.GetConsumerGroup(consumerGroupName).CreateReceiver(partition, startTime); //receive the events from startTime until current time in a single call and process them var events = await eventHubReceiver.ReceiveAsync(int.MaxValue, TimeSpan.FromSeconds(20)); foreach (var eventData in events) { byte[] rxbuff = eventData.GetBytes(); makeCommDataForLogData("Rx", rxbuff); } //having already received past events, monitor current events in a loop while (true) { ct.ThrowIfCancellationRequested(); var eventData = await eventHubReceiver.ReceiveAsync(TimeSpan.FromSeconds(0.5)); if (eventData != null) { byte[] rxbuff = eventData.GetBytes(); makeCommDataForLogData("Rx", rxbuff); } } } catch (Exception ex) { if (ct.IsCancellationRequested) { //Stopped Monitoring events by user action. } else { //Something happen. } if (eventHubReceiver != null) { eventHubReceiver.Close(); } if (eventHubClient != null) { eventHubClient.Close(); } } }
private static Task <EventHubReceiver> CreateReceiver(EventHubPartitionConfig partitionConfig) { EventHubClient client = EventHubClient.CreateFromConnectionString(partitionConfig.Hub.ConnectionString, partitionConfig.Hub.Path); EventHubConsumerGroup consumerGroup = client.GetConsumerGroup(partitionConfig.Hub.ConsumerGroup); if (partitionConfig.Hub.PrefetchCount.HasValue) { consumerGroup.PrefetchCount = partitionConfig.Hub.PrefetchCount.Value; } return(consumerGroup.CreateReceiverAsync(partitionConfig.Partition, DateTime.UtcNow)); }
private async Task ReceiveMessagesFromDeviceAsync(string partition, CancellationToken ct) { var eventHubReceiver = _eventHubClient.GetConsumerGroup(ConfigurationManager.AppSettings["ConsumerGroup"]) .CreateReceiver(partition, DateTime.UtcNow); while (_hubContext != null && !ct.IsCancellationRequested) { try { var eventData = await eventHubReceiver.ReceiveAsync(); if (eventData == null) { continue; } var data = Encoding.UTF8.GetString(eventData.GetBytes()); var deviceId = eventData.SystemProperties["iothub-connection-device-id"].ToString(); var fromDeviceObject = JsonConvert.DeserializeObject <MxPayload>(data); var gForce = CalculateGForce(Convert.ToDouble(fromDeviceObject.AccelX), Convert.ToDouble(fromDeviceObject.AccelY), Convert.ToDouble(fromDeviceObject.AccelZ)); var mgauss = CalculateSingleMGauss(fromDeviceObject.MagX, fromDeviceObject.MagY, fromDeviceObject.MagZ); // We are just going to simulate a DB reading as the sound file is too large to be sent in a message var decibel = 0.0; if (fromDeviceObject.SoundRecorded) { decibel = GetRandomSpeakingDecibel(Constants.LowSpeakingDbValue, Constants.HighSpeakingDbValue); } var metricsPayload = new MetricsPayload { Id = deviceId, Gravity = Math.Round(gForce, 3), Temperature = Math.Round(fromDeviceObject.Temperature, 2), Humidity = Math.Round(fromDeviceObject.Humidity, 2), Pressure = Math.Round(fromDeviceObject.Pressure, 2), Magnetometer = mgauss, Decibels = decibel }; var msg = JsonConvert.SerializeObject(metricsPayload); Trace.TraceInformation($"Publishing message: {msg}"); _hubContext.Clients.All.Message(msg); } catch (Exception e) { Trace.TraceError($"An error occurred while receiving messages: {e}"); } } }
private static async Task <EventHubReceiver> CreateReceiver(EventHubPartitionSettings partitionSettings, string offset, Logger logger) #endif { bool offsetInclusive = true; #if NETSTANDARD var connectionStringBuilder = new EventHubsConnectionStringBuilder(partitionSettings.Hub.ConnectionString) { EntityPath = partitionSettings.Hub.Path }; EventHubClient client = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString()); #else EventHubClient client = EventHubClient.CreateFromConnectionString(partitionSettings.Hub.ConnectionString, partitionSettings.Hub.Path); EventHubConsumerGroup consumerGroup = client.GetConsumerGroup(partitionSettings.Hub.ConsumerGroup); if (partitionSettings.Hub.PrefetchCount.HasValue) { consumerGroup.PrefetchCount = partitionSettings.Hub.PrefetchCount.Value; } #endif // if we have a starting offset or if we're not configured to start reading from utc now, read from offset if (!partitionSettings.Hub.StartFromNow || offset != EventHubConstants.StartOfStream) { logger.Info("Starting to read from EventHub partition {0}-{1} at offset {2}", partitionSettings.Hub.Path, partitionSettings.Partition, offset); } else { // to start reading from most recent data, we get the latest offset from the partition. #if NETSTANDARD EventHubPartitionRuntimeInformation partitionInfo = #else PartitionRuntimeInformation partitionInfo = #endif await client.GetPartitionRuntimeInformationAsync(partitionSettings.Partition); offset = partitionInfo.LastEnqueuedOffset; offsetInclusive = false; logger.Info("Starting to read latest messages from EventHub partition {0}-{1} at offset {2}", partitionSettings.Hub.Path, partitionSettings.Partition, offset); } #if NETSTANDARD PartitionReceiver receiver = client.CreateReceiver(partitionSettings.Hub.ConsumerGroup, partitionSettings.Partition, offset, offsetInclusive); if (partitionSettings.Hub.PrefetchCount.HasValue) { receiver.PrefetchCount = partitionSettings.Hub.PrefetchCount.Value; } return(receiver); #else return(await consumerGroup.CreateReceiverAsync(partitionSettings.Partition, offset, offsetInclusive)); #endif }
private async Task <List <EventHubReceiver> > GetReceivers() { EventHubConsumerGroup consumerGroup = eventHubClient.GetConsumerGroup(consumerGroupName); EventHubRuntimeInformation runtimeInfo = await eventHubClient.GetRuntimeInformationAsync(); var receivers = new List <EventHubReceiver>(); foreach (string partition in runtimeInfo.PartitionIds) { receivers.Add(await consumerGroup.CreateReceiverAsync(partition, EndOfStream)); } return(receivers); }
public Task InitializeEventProcessor(string connectionString, string eventHubName, string storageConnectionString, string path, string cGroup = null, string vin = null, string activityId = null) { Filter.BlockingCollection = new BlockingCollection<string>(int.MaxValue); Filter.EventDataCollection = new BlockingCollection<EventData>(int.MaxValue); Filter.Path = path; Filter.Vin = vin; Filter.ActivityId = activityId; this.eventHubClient = EventHubClient.CreateFromConnectionString(connectionString, eventHubName); this.consumerGroup = string.IsNullOrWhiteSpace(cGroup) ? eventHubClient.GetDefaultConsumerGroup() : eventHubClient.GetConsumerGroup(cGroup); this.eventProcessorHost = new EventProcessorHost("EventHubReader", eventHubClient.Path, this.consumerGroup.GroupName, connectionString, storageConnectionString); return this.eventProcessorHost.RegisterEventProcessorAsync<EventProcessor>(); }
public Task InitializeEventProcessor(string connectionString, string eventHubName, string storageConnectionString, string path, string cGroup = null, string vin = null, string activityId = null) { Filter.BlockingCollection = new BlockingCollection <string>(int.MaxValue); Filter.EventDataCollection = new BlockingCollection <EventData>(int.MaxValue); Filter.Path = path; Filter.Vin = vin; Filter.ActivityId = activityId; this.eventHubClient = EventHubClient.CreateFromConnectionString(connectionString, eventHubName); this.consumerGroup = string.IsNullOrWhiteSpace(cGroup) ? eventHubClient.GetDefaultConsumerGroup() : eventHubClient.GetConsumerGroup(cGroup); this.eventProcessorHost = new EventProcessorHost("EventHubReader", eventHubClient.Path, this.consumerGroup.GroupName, connectionString, storageConnectionString); return(this.eventProcessorHost.RegisterEventProcessorAsync <EventProcessor>()); }
public void MessageProcessingWithPartitionDistribution() { EventHubClient eventHubClient = EventHubClient.CreateFromConnectionString(eventHubConnectionString, this.eventHubName); // Get the default Consumer Group defaultConsumerGroup = eventHubClient.GetConsumerGroup(this.consumerGroup); string blobConnectionString = ConfigurationManager.AppSettings["AzureStorageConnectionString"]; // Required for checkpoint/state eventProcessorHost = new EventProcessorHost("singleworker", eventHubClient.Path, defaultConsumerGroup.GroupName, this.eventHubConnectionString, blobConnectionString); eventProcessorHost.RegisterEventProcessorAsync <MetricsEventProcessor>().Wait(); }
static void Main(string[] args) { string connection = ConfigurationManager.AppSettings["ServiceConnection"]; string consumerGroupName = "$Default"; string deviceName = "PC"; EventHubClient eventHubClient = null; EventHubReceiver eventHubReceiver = null; eventHubClient = EventHubClient.CreateFromConnectionString(connection, "messages/events"); var ri = eventHubClient.GetRuntimeInformation(); if (deviceName != "") { string partition = EventHubPartitionKeyResolver.ResolveToPartition(deviceName, ri.PartitionCount); eventHubReceiver = eventHubClient.GetConsumerGroup(consumerGroupName). CreateReceiver(partition, DateTime.Now); Console.WriteLine($"{deviceName} - {partition}"); Task.Run(() => EventLoopAsync(eventHubReceiver)); } else { EventHubReceiver[] eventHubReceivers = new EventHubReceiver[ri.PartitionCount]; Console.WriteLine($"PartitionCount: {ri.PartitionCount}"); int i = 0; foreach (var partition in ri.PartitionIds) { Console.WriteLine($"PartitionID: {partition}"); eventHubReceivers[i] = eventHubClient.GetConsumerGroup(consumerGroupName).CreateReceiver(partition, DateTime.Now); //Task.Run(() => eventLoop(eventHubReceivers[i])); <- very common bug! var r = eventHubReceivers[i]; Task.Run(() => EventLoopAsync(r)); i++; } } Console.ReadLine(); }
private async Task receiveMessageFromHub(string partition) { var receiver = client.GetConsumerGroup("iotdata").CreateReceiver(partition); while (true) { EventData data = await receiver.ReceiveAsync(TimeSpan.FromSeconds(20)); if (data == null) { continue; } OnDataReceived.Invoke(data); } }
public AzureIoTHubMonitor(CancellationTokenSource cancellationTokenSource, List<AzureIoTDevice> allDevices, DateTime? startTime = null) { this.StartTime = startTime.HasValue ? startTime.Value:DateTime.Now; this._allDevices = allDevices; var consumerGroupName = "$Default"; this._cancellationTokenSource = cancellationTokenSource; _eventHubClient = EventHubClient.CreateFromConnectionString(AzureIoTHubDevices.ConnectionString, "messages/events"); var eventHubPartitionsCount = _eventHubClient.GetRuntimeInformation().PartitionCount; foreach (var d in this._allDevices) { var partition = EventHubPartitionKeyResolver.ResolveToPartition(d.DeviceId, eventHubPartitionsCount); _eventHubReceivers.Add(d, _eventHubClient.GetConsumerGroup(consumerGroupName).CreateReceiver(partition, this.StartTime)); } }
static void Main(string[] args) { string connection = "HostName=pltkdpepliot2016S1.azure-devices.net;SharedAccessKeyName=service;SharedAccessKey=Q2Lib3zRmGtHYWJMogofGWbhwrBCpGmemnTvXOWj+qE="; string consumerGroupName = "$Default"; string deviceName = ""; EventHubClient eventHubClient = null; EventHubReceiver eventHubReceiver = null; eventHubClient = EventHubClient.CreateFromConnectionString(connection, "messages/events"); var ri = eventHubClient.GetRuntimeInformation(); if (deviceName != "") { string partition = EventHubPartitionKeyResolver.ResolveToPartition(deviceName, ri.PartitionCount); eventHubReceiver = eventHubClient.GetConsumerGroup(consumerGroupName). CreateReceiver(partition, DateTime.Now); Task.Run(() => EventLoopAsync(eventHubReceiver)); } else { EventHubReceiver[] eventHubReceivers = new EventHubReceiver[ri.PartitionCount]; Console.WriteLine($"PartitionCount: {ri.PartitionCount}"); int i = 0; foreach (var partition in ri.PartitionIds) { Console.WriteLine($"PartitionID: {partition}"); eventHubReceivers[i] = eventHubClient.GetConsumerGroup(consumerGroupName).CreateReceiver(partition, DateTime.Now); //Task.Run(() => eventLoop(eventHubReceivers[i])); <- very common bug! var r = eventHubReceivers[i]; Task.Run(() => EventLoopAsync(r)); i++; } } Console.ReadLine(); }
private void FetchHubData() { var indicators = new Indicators(); indicators.Reset(); var cts = new CancellationTokenSource(); if (_eventHubClient == null) { Messenger.Default.Send($"Opening '{configSettings.Path}', partition {configSettings.PartitionId}, consumer group '{configSettings.GroupName}', StartingDateTimeUtc = {configSettings.StartingDateTimeUtc}", "MonitorLog"); _eventHubClient = EventHubClient.CreateFromConnectionString(configSettings.ConnectionString, configSettings.Path); } var partition = _eventHubClient.GetRuntimeInformation().PartitionCount; partitionNumber = partitionNumber == 0 ? partition : partitionNumber; var consumerGroup = _eventHubClient.GetConsumerGroup(configSettings.GroupName); var receiver = consumerGroup.CreateReceiver(configSettings.PartitionId, configSettings.StartingDateTimeUtc); stopwatch = Stopwatch.StartNew(); while (!pause) { try { var eventData = receiver.Receive(TimeSpan.FromSeconds(1)); if (eventData != null) { indicators.Push(eventData); totalDevice = indicators.TotalDevices; totalMessage = indicators.TotalMessages; currentDate = DateTime.Now; runningTime = stopwatch.Elapsed; throughPut = (int)(indicators.TotalMessages / stopwatch.Elapsed.TotalMinutes); deviceToHubDelayAvg = FormatDelay(indicators.DeviceToIoTHubDelay.StreamAvg); deviceToHubDelayOneMin = FormatDelay(indicators.DeviceToIoTHubDelay.WindowAvg); e2EDelay = FormatDelay(indicators.E2EDelay.StreamAvg); sampleContent = indicators.SampleEvent; sampleEventSender = indicators.SampleEventSender; } } catch { continue; } Thread.Sleep(100); } }
/// <summary> /// Receiver from IotHub or EventHub /// </summary> /// <param name="partition"></param> /// <returns></returns> /// private Task receiveMessagesAsync(string partition) { var t = new Task(() => { try { var eventHubReceiver = m_EventHubClient.GetConsumerGroup(m_ConsumerGroup).CreateReceiver(partition, m_StartTime); // Header.WriteLine($"Connected to partition {partition}", ConsoleColor.Yellow); bool isColor = true; while (true) { EventData eventData = eventHubReceiver.ReceiveAsync().Result; if (eventData == null) { Console.WriteLine($"Partition: {partition}. No events received ..."); continue; } string data = Encoding.UTF8.GetString(eventData.GetBytes()); StringBuilder stBuider = new StringBuilder(); stBuider.AppendLine($"x-opt-sequence-number : {eventData.SystemProperties["x-opt-sequence-number"]}"); stBuider.AppendLine($"x-opt-offset: {eventData.SystemProperties["x-opt-offset"]}"); stBuider.AppendLine($"x-opt-enqueued-time: {eventData.SystemProperties["x-opt-enqueued-time"]}"); stBuider.AppendLine($"Message received. Partition: {partition} Data: '{data}'"); // // Different color if (isColor) { Helper.WriteLine(stBuider.ToString(), ConsoleColor.Blue); } else { Helper.WriteLine(stBuider.ToString(), ConsoleColor.White); } isColor = !isColor; // readProperties(data); } } catch (Exception) { throw; } }); return(t); }
private static Task <EventHubReceiver> CreateReceiver(EventHubPartitionConfig partitionConfig, string offset) { EventHubClient client = EventHubClient.CreateFromConnectionString(partitionConfig.Hub.ConnectionString, partitionConfig.Hub.Path); EventHubConsumerGroup consumerGroup = client.GetConsumerGroup(partitionConfig.Hub.ConsumerGroup); if (partitionConfig.Hub.PrefetchCount.HasValue) { consumerGroup.PrefetchCount = partitionConfig.Hub.PrefetchCount.Value; } // if we have a starting offset or if we're not configured to start reading from utc now, read from offset if (!partitionConfig.Hub.StartFromNow || offset != EventHubConsumerGroup.StartOfStream) { return(consumerGroup.CreateReceiverAsync(partitionConfig.Partition, offset, true)); } return(consumerGroup.CreateReceiverAsync(partitionConfig.Partition, DateTime.UtcNow)); }
async Task sendSingleMessage(Client.TransportType transport) { Tuple <string, string> deviceInfo = TestUtil.CreateDevice("E2E_Message_CSharp_", hostName, registryManager); EventHubClient eventHubClient = EventHubClient.CreateFromConnectionString(hubConnectionString, "messages/events"); var eventHubPartitionsCount = eventHubClient.GetRuntimeInformation().PartitionCount; string partition = EventHubPartitionKeyResolver.ResolveToPartition(deviceInfo.Item1, eventHubPartitionsCount); string consumerGroupName = Environment.GetEnvironmentVariable("IOTHUB_EVENTHUB_CONSUMER_GROUP"); if (consumerGroupName == null) { consumerGroupName = "$Default"; } EventHubReceiver eventHubReceiver = eventHubClient.GetConsumerGroup(consumerGroupName).CreateReceiver(partition, DateTime.Now); var deviceClient = DeviceClient.CreateFromConnectionString(deviceInfo.Item2, transport); await deviceClient.OpenAsync(); string dataBuffer = Guid.NewGuid().ToString(); string propertyName = "property1"; string propertyValue = Guid.NewGuid().ToString(); Client.Message eventMessage = new Client.Message(Encoding.UTF8.GetBytes(dataBuffer)); eventMessage.Properties[propertyName] = propertyValue; await deviceClient.SendEventAsync(eventMessage); var events = await eventHubReceiver.ReceiveAsync(int.MaxValue, TimeSpan.FromSeconds(5)); foreach (var eventData in events) { var data = Encoding.UTF8.GetString(eventData.GetBytes()); Assert.AreEqual(data, dataBuffer); var connectionDeviceId = eventData.SystemProperties["iothub-connection-device-id"].ToString(); Assert.AreEqual(connectionDeviceId.ToUpper(), deviceInfo.Item1.ToUpper()); Assert.AreEqual(eventData.Properties.Count, 1); var property = eventData.Properties.Single(); Assert.AreEqual(property.Key, propertyName); Assert.AreEqual(property.Value, propertyValue); } await deviceClient.CloseAsync(); await eventHubClient.CloseAsync(); TestUtil.RemoveDevice(deviceInfo.Item1, registryManager); }
public PartitionListenerControl(WriteToLogDelegate writeToLog, Func<Task> stopLog, Action startLog, string iotHubConnectionString, string consumerGroupName) { Task.Factory.StartNew(AsyncTrackEventData).ContinueWith(t => { if (t.IsFaulted && t.Exception != null) { writeToLog(t.Exception.Message); } }); this.iotHubConnectionString = iotHubConnectionString; this.writeToLog = writeToLog; this.stopLog = stopLog; this.startLog = startLog; serviceBusHelper = new ServiceBusHelper(writeToLog); eventHubClient = EventHubClient.CreateFromConnectionString(iotHubConnectionString, "messages/events"); consumerGroup = string.Compare(consumerGroupName, DefaultConsumerGroupName, StringComparison.InvariantCultureIgnoreCase) == 0 ? eventHubClient.GetDefaultConsumerGroup() : eventHubClient.GetConsumerGroup(consumerGroupName); IList<string> partitionIdList = new List<string>(eventHubClient.GetRuntimeInformation().PartitionIds); foreach (var id in partitionIdList) { partitionRuntumeInformationList.Add(eventHubClient.GetPartitionRuntimeInformation(id)); } partitionCount = partitionRuntumeInformationList.Count; InitializeComponent(); InitializeControls(); Disposed += ListenerControl_Disposed; }
public PartitionListenerControl(WriteToLogDelegate writeToLog, Func<Task> stopLog, Action startLog, ServiceBusHelper serviceBusHelper, ConsumerGroupDescription consumerGroupDescription, IEnumerable<PartitionDescription> partitionDescriptions) { Task.Factory.StartNew(AsyncTrackEventData).ContinueWith(t => { if (t.IsFaulted && t.Exception != null) { writeToLog(t.Exception.Message); } }); this.writeToLog = writeToLog; this.stopLog = stopLog; this.startLog = startLog; this.serviceBusHelper = serviceBusHelper; eventHubDescription = serviceBusHelper.NamespaceManager.GetEventHub(consumerGroupDescription.EventHubPath); eventHubClient = EventHubClient.CreateFromConnectionString(GetAmqpConnectionString(serviceBusHelper.ConnectionString), consumerGroupDescription.EventHubPath); consumerGroup = string.Compare(consumerGroupDescription.Name, DefaultConsumerGroupName, StringComparison.InvariantCultureIgnoreCase) == 0 ? eventHubClient.GetDefaultConsumerGroup() : eventHubClient.GetConsumerGroup(consumerGroupDescription.Name); IList<string> partitionIdList = partitionDescriptions.Select(pd => pd.PartitionId).ToList(); foreach (var id in partitionIdList) { partitionRuntumeInformationList.Add(eventHubClient.GetPartitionRuntimeInformation(id)); } partitionCount = partitionRuntumeInformationList.Count; InitializeComponent(); InitializeControls(); Disposed += ListenerControl_Disposed; }
public async Task<string> OpenAsync(CancellationToken cancellationToken) { await mOptions.PrepareAsync(); var useDefaultConsumerGroup = !string.IsNullOrEmpty(mOptions.EventHubConsumerGroupName); mMessagingFactory = MessagingFactory.CreateFromConnectionString(mOptions.EventHubConnectionString); mEventHubClient = mMessagingFactory.CreateEventHubClient(mOptions.EventHubName); mConsumerGroup = useDefaultConsumerGroup ? mEventHubClient.GetConsumerGroup(mOptions.EventHubConsumerGroupName) : mEventHubClient.GetDefaultConsumerGroup(); return string.Concat(mEventHubNamespace, "/", mOptions.EventHubName, "/", useDefaultConsumerGroup ? "<default group>" : mOptions.EventHubConsumerGroupName); }