async Task InvokeOnNull()
        {
            PartitionReceiver partitionReceiver = this.EventHubClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, "0", EventPosition.FromEnd());

            try
            {
                EventWaitHandle nullReceivedEvent = new EventWaitHandle(false, EventResetMode.ManualReset);
                var             handler           = new TestPartitionReceiveHandler();

                handler.EventsReceived += (s, eventDatas) =>
                {
                    if (eventDatas == null)
                    {
                        TestUtility.Log("Received null.");
                        nullReceivedEvent.Set();
                    }
                };

                partitionReceiver.SetReceiveHandler(handler, true);

                if (!nullReceivedEvent.WaitOne(TimeSpan.FromSeconds(120)))
                {
                    throw new InvalidOperationException("Did not receive null.");
                }
            }
            finally
            {
                // Unregister handler.
                partitionReceiver.SetReceiveHandler(null);

                // Close clients.
                await partitionReceiver.CloseAsync();
            }
        }
        async Task DefaultBehaviorNoInvokeOnNull()
        {
            PartitionReceiver partitionReceiver = this.EventHubClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, "0", EventPosition.FromEnd());

            try
            {
                EventWaitHandle nullReceivedEvent = new EventWaitHandle(false, EventResetMode.ManualReset);
                EventWaitHandle dataReceivedEvent = new EventWaitHandle(false, EventResetMode.ManualReset);
                var             handler           = new TestPartitionReceiveHandler();

                handler.EventsReceived += (s, eventDatas) =>
                {
                    if (eventDatas == null)
                    {
                        TestUtility.Log("Received null.");
                        nullReceivedEvent.Set();
                    }
                    else
                    {
                        TestUtility.Log("Received message.");
                        dataReceivedEvent.Set();
                    }
                };

                partitionReceiver.SetReceiveHandler(handler);

                if (nullReceivedEvent.WaitOne(TimeSpan.FromSeconds(120)))
                {
                    throw new InvalidOperationException("Received null.");
                }

                // Send one message. Pump should receive this.
                await TestUtility.SendToPartitionAsync(this.EventHubClient, "0", "new event");

                if (!dataReceivedEvent.WaitOne(TimeSpan.FromSeconds(60)))
                {
                    throw new InvalidOperationException("Data Received Event was not signaled.");
                }
            }
            finally
            {
                // Unregister handler.
                partitionReceiver.SetReceiveHandler(null);

                // Close clients.
                await partitionReceiver.CloseAsync();
            }
        }
Пример #3
0
        public async Task ReceiveEventsAsync(
            string deviceId,
            Func <EventData, bool> onEventReceived,
            CancellationToken token)
        {
            EventHubClient    client    = this.EventHubClient;
            int               count     = (await client.GetRuntimeInformationAsync()).PartitionCount;
            string            partition = EventHubPartitionKeyResolver.ResolveToPartition(deviceId, count);
            PartitionReceiver receiver  = client.CreateReceiver("$Default", partition, EventPosition.FromEnd());

            var result = new TaskCompletionSource <bool>();

            using (token.Register(() => result.TrySetCanceled()))
            {
                receiver.SetReceiveHandler(
                    new PartitionReceiveHandler(
                        data =>
                {
                    bool done = onEventReceived(data);
                    if (done)
                    {
                        result.TrySetResult(true);
                    }

                    return(done);
                }));

                await result.Task;
            }

            await receiver.CloseAsync();
        }
Пример #4
0
        async Task PartitionReceiverSetReceiveHandler()
        {
            Log("Receiving Events via PartitionReceiver.SetReceiveHandler()");
            string            partitionId       = "1";
            PartitionReceiver partitionReceiver = this.EventHubClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, partitionId, DateTime.UtcNow.AddMinutes(-10));
            PartitionSender   partitionSender   = this.EventHubClient.CreatePartitionSender(partitionId);

            try
            {
                string uniqueEventId = Guid.NewGuid().ToString();
                Log($"Sending an event to Partition {partitionId} with custom property EventId {uniqueEventId}");
                var sendEvent = new EventData(Encoding.UTF8.GetBytes("Hello EventHub!"));
                sendEvent.Properties = new Dictionary <string, object> {
                    ["EventId"] = uniqueEventId
                };
                await partitionSender.SendAsync(sendEvent);

                EventWaitHandle dataReceivedEvent = new EventWaitHandle(false, EventResetMode.ManualReset);
                var             handler           = new TestPartitionReceiveHandler();
                handler.ErrorReceived  += (s, e) => Log($"TestPartitionReceiveHandler.ProcessError {e.GetType().Name}: {e.Message}");
                handler.EventsReceived += (s, eventDatas) =>
                {
                    int count = eventDatas != null?eventDatas.Count() : 0;

                    Log($"Received {count} event(s):");

                    foreach (var eventData in eventDatas)
                    {
                        object objectValue;
                        if (eventData.Properties != null && eventData.Properties.TryGetValue("EventId", out objectValue))
                        {
                            Log($"Received message with EventId {objectValue}");
                            string receivedId = objectValue.ToString();
                            if (receivedId == uniqueEventId)
                            {
                                Log("Success");
                                dataReceivedEvent.Set();
                                break;
                            }
                        }
                    }
                };

                partitionReceiver.SetReceiveHandler(handler);

                if (!dataReceivedEvent.WaitOne(TimeSpan.FromSeconds(20)))
                {
                    throw new InvalidOperationException("Data Received Event was not signaled.");
                }
            }
            finally
            {
                await partitionSender.CloseAsync();

                await partitionReceiver.CloseAsync();
            }
        }
Пример #5
0
        private async Task StartListeners()
        {
            var ehRuntime = await _eventHubClient.GetRuntimeInformationAsync();

            foreach (var partitionId in ehRuntime.PartitionIds)
            {
                var cp = _checkpoint.Get($"{Constants.EventHubCheckpoint}.{_storeConfig.EventHubName}.{partitionId}");
                _receiver = _eventHubClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, partitionId, EventPosition.FromOffset(cp ?? "-1"));
                _receiver.SetReceiveHandler(_storeReceiver);
            }
        }
Пример #6
0
        static async Task ReceiveMessagesAsync(DateTime lastReceivedMesssage)
        {
            var builder = new EventHubsConnectionStringBuilder(Settings.Current.EventHubConnectionString);

            Logger.LogInformation($"Receiving events from device '{Settings.Current.DeviceId}' on Event Hub '{builder.EntityPath}' with last received message at {lastReceivedMesssage.ToString()}");

            EventHubClient eventHubClient =
                EventHubClient.CreateFromConnectionString(builder.ToString());

            PartitionReceiver eventHubReceiver = eventHubClient.CreateReceiver(
                Settings.Current.ConsumerGroupId,
                EventHubPartitionKeyResolver.ResolveToPartition(Settings.Current.DeviceId, (await eventHubClient.GetRuntimeInformationAsync()).PartitionCount),
                EventPosition.FromEnqueuedTime(lastReceivedMesssage == DateTime.MinValue ? DateTime.UtcNow : lastReceivedMesssage));

            eventHubReceiver.SetReceiveHandler(new PartitionReceiveHandler(Settings.Current.DeviceId, Settings.Current.ExcludedModuleIds));
        }
Пример #7
0
        static async Task ReceiveMessages()
        {
            var builder = new EventHubsConnectionStringBuilder(Settings.Current.EventHubConnectionString);

            Log.LogInformation($"Receiving events from device '{Settings.Current.DeviceId}' on Event Hub '{builder.EntityPath}'");

            EventHubClient eventHubClient =
                EventHubClient.CreateFromConnectionString(builder.ToString());

            PartitionReceiver eventHubReceiver = eventHubClient.CreateReceiver(
                "$Default",
                EventHubPartitionKeyResolver.ResolveToPartition(Settings.Current.DeviceId, (await eventHubClient.GetRuntimeInformationAsync()).PartitionCount),
                EventPosition.FromEnqueuedTime(DateTime.UtcNow));

            eventHubReceiver.SetReceiveHandler(new PartitionReceiveHandler(Settings.Current.DeviceId, Settings.Current.ExcludedModuleIds));
        }
Пример #8
0
        /// <inheritdoc />
        public BoolResult StartProcessing(OperationContext context, EventSequencePoint sequencePoint, IPartitionReceiveHandler processor)
        {
            Tracer.Info(context, $"{Tracer.Name}: Initializing event processing for event hub '{_configuration.EventHubName}' and consumer group '{_configuration.ConsumerGroupName}'.");

            if (_partitionReceiver == null)
            {
                _partitionReceiver = _eventHubClient.CreateReceiver(
                    _configuration.ConsumerGroupName,
                    PartitionId,
                    GetInitialOffset(context, sequencePoint),
                    new ReceiverOptions()
                {
                    Identifier = _hostName
                });

                _partitionReceiver.SetReceiveHandler(processor);
            }

            return(BoolResult.Success);
        }
Пример #9
0
        protected override async Task ExecuteAsync(CancellationToken cancellationToken)
        {
            this.logger.LogInformation("Test Result Event Receiving Service running.");

            DateTime eventEnqueuedFrom = DateTime.UtcNow;
            var      builder           = new EventHubsConnectionStringBuilder(Settings.Current.EventHubConnectionString);

            this.logger.LogDebug($"Receiving events from device '{Settings.Current.DeviceId}' on Event Hub '{builder.EntityPath}' enqueued on or after {eventEnqueuedFrom}");

            EventHubClient    eventHubClient   = EventHubClient.CreateFromConnectionString(builder.ToString());
            PartitionReceiver eventHubReceiver = eventHubClient.CreateReceiver(
                Settings.Current.ConsumerGroupName,
                EventHubPartitionKeyResolver.ResolveToPartition(Settings.Current.DeviceId, (await eventHubClient.GetRuntimeInformationAsync()).PartitionCount),
                EventPosition.FromEnqueuedTime(eventEnqueuedFrom));

            eventHubReceiver.SetReceiveHandler(new PartitionReceiveHandler(Settings.Current.TrackingId, Settings.Current.DeviceId, this.storage));

            await cancellationToken.WhenCanceled();

            this.logger.LogInformation($"Finish ExecuteAsync method in {nameof(TestResultEventReceivingService)}");
        }
Пример #10
0
        public async Task ReceiveEventsAsync(
            string deviceId,
            DateTime seekTime,
            Func <EventData, bool> onEventReceived,
            CancellationToken token)
        {
            EventHubClient client    = this.EventHubClient;
            int            count     = (await client.GetRuntimeInformationAsync()).PartitionCount;
            string         partition = EventHubPartitionKeyResolver.ResolveToPartition(deviceId, count);

            seekTime = seekTime.ToUniversalTime().Subtract(TimeSpan.FromMinutes(2)); // substract 2 minutes to account for client/server drift
            EventPosition     position = EventPosition.FromEnqueuedTime(seekTime);
            PartitionReceiver receiver = client.CreateReceiver("$Default", partition, position);

            var result = new TaskCompletionSource <bool>();

            using (token.Register(() => result.TrySetCanceled()))
            {
                receiver.SetReceiveHandler(
                    new PartitionReceiveHandler(
                        data =>
                {
                    bool done = onEventReceived(data);
                    if (done)
                    {
                        result.TrySetResult(true);
                    }

                    return(done);
                }));

                await result.Task;
            }

            await receiver.CloseAsync();
        }
        async Task ReceiveHandlerReregister()
        {
            int    totalNumberOfMessagesToSend = 100;
            string partitionId = "0";

            var pInfo = await this.EventHubClient.GetPartitionRuntimeInformationAsync(partitionId);

            PartitionReceiver partitionReceiver = this.EventHubClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, partitionId, EventPosition.FromOffset(pInfo.LastEnqueuedOffset));
            await TestUtility.SendToPartitionAsync(this.EventHubClient, partitionId, $"{partitionId} event.", totalNumberOfMessagesToSend);

            try
            {
                var handler = new TestPartitionReceiveHandler();
                handler.MaxBatchSize = 1;

                // Not expecting any errors.
                handler.ErrorReceived += (s, e) =>
                {
                    // SetReceiveHandler will ignore any exception thrown so log here for output.
                    TestUtility.Log($"TestPartitionReceiveHandler.ProcessError {e.GetType().Name}: {e.Message}");
                    throw new Exception($"TestPartitionReceiveHandler.ProcessError {e.GetType().Name}: {e.Message}");
                };

                int totalnumberOfMessagesReceived = 0;
                handler.EventsReceived += (s, eventDatas) =>
                {
                    int count = eventDatas != null?eventDatas.Count() : 0;

                    Interlocked.Add(ref totalnumberOfMessagesReceived, count);
                    TestUtility.Log($"Received {count} event(s).");
                };

                TestUtility.Log("Registering");
                partitionReceiver.SetReceiveHandler(handler);
                TestUtility.Log("Unregistering");
                partitionReceiver.SetReceiveHandler(null);
                TestUtility.Log("Registering");
                partitionReceiver.SetReceiveHandler(handler);
                await Task.Delay(3000);

                // Second register call will trigger error handler but throw from handler should be ignored
                // so below register call should not fail.
                TestUtility.Log("Registering when already registered");
                partitionReceiver.SetReceiveHandler(handler);

                TestUtility.Log("All register calls done.");

                // Send another set of messages.
                // Since handler is still registered we should be able to receive these messages just fine.
                await TestUtility.SendToPartitionAsync(this.EventHubClient, partitionId, $"{partitionId} event.", totalNumberOfMessagesToSend);

                // Allow 1 minute to receive all messages.
                await Task.Delay(TimeSpan.FromSeconds(60));

                TestUtility.Log($"Received {totalnumberOfMessagesReceived}.");
                Assert.True(totalnumberOfMessagesReceived == totalNumberOfMessagesToSend * 2, $"Did not receive {totalNumberOfMessagesToSend * 2} messages, received {totalnumberOfMessagesReceived}.");
            }
            finally
            {
                // Unregister handler.
                partitionReceiver.SetReceiveHandler(null);

                // Close clients.
                await partitionReceiver.CloseAsync();
            }
        }