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; } }
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}"); } }
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); }
// This function create a device with x509 cert and send a message to the iothub on the transport specified. // It then verifies the message is received at the eventHubClient. private async Task SendSingleMessageX509(Client.TransportType transport) { Tuple <string, string> deviceInfo = TestUtil.CreateDeviceWithX509(DevicePrefix, hostName, registryManager); EventHubClient eventHubClient; EventHubReceiver eventHubReceiver = CreateEventHubReceiver(deviceInfo.Item1, out eventHubClient); string certBase64 = Environment.GetEnvironmentVariable("IOTHUB_X509_PFX_CERTIFICATE"); Byte[] buff = Convert.FromBase64String(certBase64); var cert = new X509Certificate2(buff); var auth = new DeviceAuthenticationWithX509Certificate(deviceInfo.Item1, cert); var deviceClient = DeviceClient.Create(deviceInfo.Item2, auth, transport); await deviceClient.OpenAsync(); string payload; string p1Value; Client.Message testMessage = ComposeD2CTestMessage(out payload, out p1Value); await deviceClient.SendEventAsync(testMessage); var events = await eventHubReceiver.ReceiveAsync(int.MaxValue, TimeSpan.FromSeconds(5)); VerifyTestMessage(events, deviceInfo.Item1, payload, p1Value); await deviceClient.CloseAsync(); await eventHubClient.CloseAsync(); TestUtil.RemoveDevice(deviceInfo.Item1, registryManager); }
//------------------------------------------------------------------------------------------------------------------------ #endregion #region Constructor //------------------------------------------------------------------------------------------------------------------------ public EventHubDirectConsumer(EventHubClient client) { //create event hub direct consumer EventHubConsumerGroup group = client.GetDefaultConsumerGroup(); var x = client.GetRuntimeInformation().PartitionIds[0]; eventHubDirectReceiver = group.CreateReceiver(client.GetRuntimeInformation().PartitionIds[0]); }
private async Task EventLoop(EventHubReceiver receiver) { mStarted = true; var eventsBuffer = new List <EventData>(); var lastOffset = string.Empty; while (mKeepRunning) { var events = await receiver.ReceiveAsync(mOptions.BatchSize); foreach (var evt in events) { lastOffset = evt.Offset; eventsBuffer.Add(evt); if (eventsBuffer.Count == mOptions.BatchSize) { var shouldSave = await mOptions.Processor.ProcessEventsAsync(events.AsEnumerable(), mState); if (shouldSave) { mState.Offset = lastOffset; await mState.SaveAsync(); } eventsBuffer.Clear(); } } } mStarted = false; }
public static async Task ReceiveMessagesFromDeviceAsync(EventHubReceiver receiver) { List <string> eneryString = new List <string>(); while (true) { EventData eventData = await receiver.ReceiveAsync(); if (eventData == null) { continue; } string data = Encoding.UTF8.GetString(eventData.GetBytes()); if (data.StartsWith("ambulance")) { string[] value = data.Split(','); LoadAmbulanceData(value[0], Convert.ToDouble(value[1]), Convert.ToDouble(value[2])); } if (data.StartsWith("Medical_IOT")) { string[] patientData = data.Split(','); AnalysePatientData(patientData[0], patientData[1], patientData[2], patientData[3], patientData[4]); } Console.WriteLine(data); } }
public Task Shutdown(TimeSpan timeout) { // if receiver was already shutdown, do nothing if (ReceiverShutdown == Interlocked.Exchange(ref recieverState, ReceiverShutdown)) { return(TaskDone.Done); } logger.Info("Stopping reading from EventHub partition {0}-{1}", settings.Hub.Path, settings.Partition); // clear cache and receiver IEventHubQueueCache localCache = Interlocked.Exchange(ref cache, null); EventHubReceiver localReceiver = Interlocked.Exchange(ref receiver, null); // start closing receiver Task closeTask = TaskDone.Done; if (localReceiver != null) { closeTask = localReceiver.CloseAsync(); } // dispose of cache localCache?.Dispose(); // finish return receiver closing task return(closeTask); }
public async void StartListeningAsync() { this.client = EventHubClient.CreateFromConnectionString(Common.CoreConstants.SharedEventHubEndpoint, Common.CoreConstants.SharedFlightActivityHubName); if (Common.CoreConstants.ConsumerGroup.ToLower().Trim() == "$default") { this.consumerGroup = this.client.GetDefaultConsumerGroup(); } else { try { this.consumerGroup = this.client.GetConsumerGroup(Common.CoreConstants.ConsumerGroup.ToLower().Trim()); } catch { this.consumerGroup = this.client.GetDefaultConsumerGroup(); } } this.primaryReceiver = this.consumerGroup.CreateReceiver("0", DateTime.Now.ToUniversalTime()); this.secondaryReceiver = this.consumerGroup.CreateReceiver("1", DateTime.Now.ToUniversalTime()); await Task.Run(() => StartListeningForNavigationCommands()); }
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; } }
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); }
static void Main(string[] args) { string ehname = "<eventhubname>"; string connection = "<eventhubconnectionstring>,TransportType=Amqp"; MessagingFactory factory = MessagingFactory.CreateFromConnectionString(connection); EventHubClient ehub = factory.CreateEventHubClient(ehname); EventHubConsumerGroup group = ehub.GetDefaultConsumerGroup(); EventHubReceiver reciever = group.CreateReceiver("0"); while (true) { EventData data = reciever.Receive(); if (data != null) { try { string message = Encoding.UTF8.GetString(data.GetBytes()); //Console.WriteLine("Partition Key: {0}", data.PartitionKey); Console.WriteLine(message); Console.WriteLine(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } } }
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 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); } }
static void Main(string[] args) { string ehname = "<eventhubname>"; string connection = "Endpoint=sb://<eventhubnamespace>.servicebus.windows.net/;SharedAccessKeyName=<policy>;SharedAccessKey=<key>;TransportType=Amqp"; MessagingFactory factory = MessagingFactory.CreateFromConnectionString(connection); EventHubClient ehub = factory.CreateEventHubClient(ehname); EventHubConsumerGroup group = ehub.GetDefaultConsumerGroup(); EventHubReceiver reciever = group.CreateReceiver("0"); while (true) { EventData data = reciever.Receive(); if (data != null) { try { string message = Encoding.UTF8.GetString(data.GetBytes()); Console.WriteLine(message + " " + DateTime.Now); Console.WriteLine(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } } }
private static void EventHubReceive() { var ehName = "davnaeventhub"; var connection = "Endpoint=sb://alewife.servicebus.windows.net/;SharedAccessKeyName=Receiver;SharedAccessKey=3jaTeykwRu84x93OpW3DftRO1WSHt4oi7QB0FRyHoyg=;TransportType=Amqp"; MessagingFactory factory = MessagingFactory.CreateFromConnectionString(connection); EventHubClient ehub = factory.CreateEventHubClient(ehName); EventHubConsumerGroup group = ehub.GetDefaultConsumerGroup(); EventHubReceiver receiver = group.CreateReceiver("1"); while (true) { EventData data = receiver.Receive(); if (data != null) { var message = Encoding.UTF8.GetString(data.GetBytes()); Console.WriteLine("PartitionKey: {0}", data.PartitionKey); Console.WriteLine("SequenceNumber: {0}", data.SequenceNumber); Console.WriteLine(message); } } }
private async Task SendMessageRecovery(Client.TransportType transport, string faultType, string reason, int delayInSec, int durationInSec = 0, int retryDurationInMilliSec = 240000) { await sequentialTestSemaphore.WaitAsync(); Tuple <string, string> deviceInfo = TestUtil.CreateDevice(DevicePrefix, hostName, registryManager); EventHubClient eventHubClient; EventHubReceiver eventHubReceiver = CreateEventHubReceiver(deviceInfo.Item1, out eventHubClient); var deviceClient = DeviceClient.CreateFromConnectionString(deviceInfo.Item2, transport); deviceClient.OperationTimeoutInMilliseconds = (uint)retryDurationInMilliSec; await deviceClient.OpenAsync(); string payload, p1Value; Client.Message testMessage = ComposeD2CTestMessage(out payload, out p1Value); await deviceClient.SendEventAsync(testMessage); bool isReceived = false; Stopwatch sw = new Stopwatch(); sw.Start(); while (!isReceived && sw.Elapsed.Minutes < 1) { var events = await eventHubReceiver.ReceiveAsync(int.MaxValue, TimeSpan.FromSeconds(5)); isReceived = VerifyTestMessage(events, deviceInfo.Item1, payload, p1Value); } sw.Stop(); // send error command and clear eventHubReceiver of the fault injection message await deviceClient.SendEventAsync(TestUtil.ComposeErrorInjectionProperties(faultType, reason, delayInSec, durationInSec)); await eventHubReceiver.ReceiveAsync(int.MaxValue, TimeSpan.FromSeconds(5)); Thread.Sleep(1000); testMessage = ComposeD2CTestMessage(out payload, out p1Value); await deviceClient.SendEventAsync(testMessage); sw.Reset(); sw.Start(); while (!isReceived && sw.Elapsed.Minutes < 1) { var events = await eventHubReceiver.ReceiveAsync(int.MaxValue, TimeSpan.FromSeconds(5)); isReceived = VerifyTestMessage(events, deviceInfo.Item1, payload, p1Value); } sw.Stop(); await deviceClient.CloseAsync(); await eventHubClient.CloseAsync(); TestUtil.RemoveDevice(deviceInfo.Item1, registryManager); sequentialTestSemaphore.Release(1); }
//code adapted from tutorial https://paolopatierno.wordpress.com/2015/11/02/azure-iot-hub-get-telemetry-data-using-amqp-stack-and-azure-sb-lite/ public static string GetMessage(string partitionId) { string result = null; ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(ConnectionString); builder.TransportType = TransportType.Amqp; MessagingFactory factory = MessagingFactory.CreateFromConnectionString(ConnectionString); EventHubClient client = factory.CreateEventHubClient(eventHubEntity); EventHubConsumerGroup group = client.GetDefaultConsumerGroup(); var startingDateTimeUtc = DateTime.Now; EventHubReceiver receiver = group.CreateReceiver(partitionId, startingDateTimeUtc); EventData data = receiver.Receive(); receiver.Close(); client.Close(); factory.Close(); if (data != null) { result = Encoding.UTF8.GetString(data.GetBytes()); return(result); } else { return(null); } }
public async Task ReceiveDataFromCloud() { startingDateTimeUtc = DateTime.UtcNow - TimeSpan.FromHours(24); factory = MessagingFactory.CreateFromConnectionString(ConnectionString); client = factory.CreateEventHubClient(eventHubEntity); group = client.GetDefaultConsumerGroup(); receiver = group.CreateReceiver(partitionId.ToString(), startingDateTimeUtc); try { while (true) { EventData data = receiver.Receive(); if (data != null) { var receiveddata = Encoding.UTF8.GetString(data.GetBytes()); Debug.WriteLine("{0} {1} {2}", data.SequenceNumber, data.EnqueuedTimeUtc.ToLocalTime(), receiveddata); } } } catch (Exception ex) { Debug.WriteLine("Exception {0}", ex); } receiver.Close(); client.Close(); factory.Close(); }
/// <summary> /// /// </summary> /// <param name="receiverindex"></param> /// <param name="r"></param> public static void StartReceiver(int receiverindex, EventHubReceiver r) { var starttime = DateTime.Now; bool isMessageAvailable = true; var requestdata = new WEP.GSP.Data.RequestData(); EventHubProcessor processor = new EventHubProcessor(); do { try { var messages = r.Receive(10); if (messages == null || messages.Count() == 0) { isMessageAvailable = false; } else { Task.Factory.StartNew(() => processor.EventMessageHandler(messages)); requestdata.UpdatePartitionOffset(Convert.ToString(receiverindex), messages.Last().Offset); requests = requests + messages.Count(); } } catch (Exception exception) { Trace.TraceError("exception on receive {0}", exception.Message); } } while (isMessageAvailable); requestdata.UpdateReleasePartitionLock(Convert.ToString(receiverindex)); }
protected void ReceiveMessagesFromDeviceAsync(EventHubReceiver receiver) { while (true) { var eventData = receiver.Receive(10, new TimeSpan(0, 0, 10)); if (eventData == null) { break; } List <RowRead> receivedRows = new List <RowRead>(); int count = 0; foreach (var ed in eventData) { count++; string data = Encoding.Unicode.GetString(ed.GetBytes()); RowRead row = new RowRead(); row.Values = JsonConvert.DeserializeObject <Dictionary <string, object> >(data); receivedRows.Add(row); this.EnqueuedTimeUtc = ed.EnqueuedTimeUtc; this.EnqueuedOffset = long.Parse(ed.Offset); } if (count == 0) { break; } } }
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}"); } } }
private async Task SendMessageThrottledForHttp() { await sequentialTestSemaphore.WaitAsync(); Tuple <string, string> deviceInfo = TestUtil.CreateDevice(DevicePrefix, hostName, registryManager); EventHubClient eventHubClient; EventHubReceiver eventHubReceiver = CreateEventHubReceiver(deviceInfo.Item1, out eventHubClient); var deviceClient = DeviceClient.CreateFromConnectionString(deviceInfo.Item2, Client.TransportType.Http1); deviceClient.OperationTimeoutInMilliseconds = (uint)TestUtil.ShortRetryInMilliSec; await deviceClient.OpenAsync(); string payload, p1Value; Client.Message testMessage = ComposeD2CTestMessage(out payload, out p1Value); await deviceClient.SendEventAsync(testMessage); var events = await eventHubReceiver.ReceiveAsync(int.MaxValue, TimeSpan.FromSeconds(5)); VerifyTestMessage(events, deviceInfo.Item1, payload, p1Value); // Implementation of error injection of throttling on http is that it will throttle the // fault injection message itself only. The duration of fault has no effect on http throttle. // Client is supposed to retry sending the throttling fault message until operation timeout. await deviceClient.SendEventAsync(TestUtil.ComposeErrorInjectionProperties(TestUtil.FaultType_Throttle, TestUtil.FaultCloseReason_Boom, TestUtil.DefaultDelayInSec, TestUtil.DefaultDurationInSec)); sequentialTestSemaphore.Release(1); }
private async Task EventLoop(EventHubReceiver receiver) { mStarted = true; var eventsBuffer = new List<EventData>(); var lastOffset = string.Empty; while (mKeepRunning) { var events = await receiver.ReceiveAsync(mOptions.BatchSize); foreach (var evt in events) { lastOffset = evt.Offset; eventsBuffer.Add(evt); if (eventsBuffer.Count == mOptions.BatchSize) { var shouldSave = await mOptions.Processor.ProcessEventsAsync(events.AsEnumerable(), mState); if (shouldSave) { mState.Offset = lastOffset; await mState.SaveAsync(); } eventsBuffer.Clear(); } } } mStarted = false; }
static int Main(string[] args) { if (args.Length == 0) { Console.WriteLine("Proton-DotNet.exe connection_string"); return(2); } try { EventHubClient ehc = EventHubClient.CreateFromConnectionString(args[0]); EventHubReceiver receiver = ehc.GetDefaultConsumerGroup().CreateReceiver("0"); while (true) { EventData data = receiver.Receive(); if (data == null) { break; } string text = Encoding.UTF8.GetString(data.GetBytes()); Console.WriteLine(data.SequenceNumber + ":" + text); } ehc.Close(); return(0); } catch (Exception exception) { Console.WriteLine(exception.ToString()); } return(1); }
private static async Task ReceiveMessagesFromDeviceAsync(string partition, Action <string> processEvent, CancellationToken ct) { DateTime startingDateTimeUtc = DateTime.Now; EventHubReceiver receiver0 = group.CreateReceiver(partition, startingDateTimeUtc); receiverList.Add(receiver0); while (true) { if (ct.IsCancellationRequested) { break; } if (!receiver0.IsClosed) { EventData data = receiver0.Receive(); string theEvent = Encoding.UTF8.GetString(data.GetBytes()); processEvent(theEvent); Debug.WriteLine($"{data.PartitionKey} {data.EnqueuedTimeUtc.ToLocalTime()} {theEvent}"); } await Task.Delay(2000); } }
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 async Task SendSingleMessage(Client.TransportType transport) { Tuple <string, string> deviceInfo = TestUtil.CreateDevice(DevicePrefix, hostName, registryManager); EventHubClient eventHubClient; EventHubReceiver eventHubReceiver = CreateEventHubReceiver(deviceInfo.Item1, out eventHubClient); var deviceClient = DeviceClient.CreateFromConnectionString(deviceInfo.Item2, transport); await deviceClient.OpenAsync(); string payload; string p1Value; Client.Message testMessage = ComposeD2CTestMessage(out payload, out p1Value); await deviceClient.SendEventAsync(testMessage); var events = await eventHubReceiver.ReceiveAsync(int.MaxValue, TimeSpan.FromSeconds(5)); VerifyTestMessage(events, deviceInfo.Item1, payload, p1Value); await deviceClient.CloseAsync(); await eventHubClient.CloseAsync(); TestUtil.RemoveDevice(deviceInfo.Item1, registryManager); }
private async Task Listen(EventHubReceiver receiver) { while (!_tokenSrc.IsCancellationRequested) { _sbTasks.RemoveAll(x => x.IsCompleted); //Every 30 seconds, let's check for a cancellation. As far as I could tell, there is not a listen method that //has native cancellation support. There is for the normal Azure service bus, but guess it hasn't made it to //the IoT hub libraries. var eventData = await receiver.ReceiveAsync(TimeSpan.FromSeconds(5)); if (eventData == null) { continue; } var ctx = GlobalHost.ConnectionManager.GetHubContext <BusRHub>(); var data = Encoding.UTF8.GetString(eventData.GetBytes()); var theEvent = JsonConvert.DeserializeObject <AllInOneModelDto>(data).ToFullModel() as AllInOneModel; //Send the event if (theEvent == null) { ctx.Clients.All.eventReceived(data); continue; } var stream = (EventStreamEnum)theEvent.EventStream; ctx.Clients.All.eventReceived(theEvent.ToString(_deviceRepo)); //If this is a summary event, trigger that method if (stream == EventStreamEnum.Summary) { ctx.Clients.All.summaryUpdate(theEvent.ToString(_deviceRepo)); continue; } //If it's a state change if (stream != EventStreamEnum.StateChange) { continue; } //Let's get some more friendly device information var dev = _deviceRepo.GetByDeviceId(theEvent.DeviceId ?? theEvent.IntersectionId ?? Guid.Empty); //and trigger the stateChange method for our clients ctx.Clients.All.stateChange(dev.DeviceId, theEvent.CurrentState); //Finally the bulbChange method when appropriate to update the graphical UI if (dev?.DeviceType == "Bulb") { ctx.Clients.All.bulbChange(dev.DeviceId, theEvent.CurrentState == "On" || theEvent.CurrentState == "AssumedOn"); } } var test = true; }
/// <summary> /// Initializes a new instance of the PSEventHubReceiver class. /// </summary> /// <param name="receiver">The receiver to wrap.</param> public PSEventHubReceiver(EventHubReceiver receiver) { this.Name = receiver.Name; this.SubscriptionId = receiver.SubscriptionId; this.EventHubNameSpace = receiver.EventHubNameSpace; this.EventHubName = receiver.EventHubName; this.UseCommonAlertSchema = receiver.UseCommonAlertSchema.HasValue ? receiver.UseCommonAlertSchema.Value : false; }
public SensorDataHub(IHubContext <SensorDataHub> hubContext, IConfiguration configuration) { _hubContext = hubContext; _eventHubReceiver = new EventHubReceiver(configuration); _eventHubReceiver.SensorDataReceived += EventHubReceiver_SensorDataReceived; _eventHubReceiver.Start().ConfigureAwait(false); }
async static Task ReceiveMessagesFromDeviceAsync(EventHubReceiver receiver) { while (true) { EventData eventData = await receiver.ReceiveAsync(); if (eventData == null) continue; string data = Encoding.UTF8.GetString(eventData.GetBytes()); Console.WriteLine("Message received: '{0}'", data); } }
public Task ReadAsync(EventHubReceiver receiver, CancellationTokenSource cancellation) { return Task.Factory.StartNew(async () => { Debug.WriteLine(string.Format("partition {0} started", PartitionId)); while (!cancellation.IsCancellationRequested) { var message = await receiver.ReceiveAsync(TimeSpan.FromSeconds(5)); if (message != null) { _messages.OnNext(new EventHubMessage(receiver.PartitionId,message)); } } await receiver.CloseAsync(); Debug.WriteLine(string.Format("partition {0} finished", PartitionId)); }, cancellation.Token, TaskCreationOptions.LongRunning, TaskScheduler.Current); }
public EventHubListener(EventHubReceiver reciever) { this.reciever = reciever; }
public async Task Initialize(TimeSpan timeout) { cache = new SimpleQueueCache(config.CacheSize, logger); receiver = await CreateReceiver(config); }
static async Task<Tuple<EventData, string>[]> CollectEventHubMessagesAsync(EventHubReceiver[] receivers, int messagesPending) { List<Task<EventData>> receiveTasks = receivers.Select(r => r.ReceiveAsync(TimeSpan.FromMinutes(20))).ToList(); var ehMessages = new Tuple<EventData, string>[messagesPending]; while (true) { Task<EventData> receivedTask = await Task.WhenAny(receiveTasks); EventData eventData = receivedTask.Result; if (eventData != null) { ehMessages[messagesPending - 1] = Tuple.Create(eventData, Encoding.UTF8.GetString(eventData.GetBytes())); if (--messagesPending == 0) { break; } } int receivedIndex = receiveTasks.IndexOf(receivedTask); receiveTasks[receivedIndex] = receivers[receivedIndex].ReceiveAsync(TimeSpan.FromMinutes(20)); } return ehMessages; }