Exemplo n.º 1
0
        public static async Task Main(string[] args)
        {
            string hostName = "host-" + Guid.NewGuid().ToString().Substring(0, 5);

            Console.WriteLine($"Registering EventProcessor {hostName}");

            var eventProcessorHost = new EventProcessorHost(
                hostName,
                EventHubName,
                PartitionReceiver.DefaultConsumerGroupName,
                EventHubConnectionString,
                StorageConnectionString,
                StorageContainerName);

            // Registers the Event Processor Host and starts receiving messages
            await eventProcessorHost.RegisterEventProcessorAsync <SimpleEventProcessor>(new EventProcessorOptions()
            {
                InitialOffsetProvider = (partitionId) => EventPosition.FromEnd()
            });

            Console.WriteLine("Receiving. Press enter key to stop worker.");
            Console.ReadLine();

            // Disposes of the Event Processor Host
            await eventProcessorHost.UnregisterEventProcessorAsync();
        }
        private static ReliableEventHubReceiverConnectionFactory CreateTarget(IEnumerable <Unit> loop, IEnumerable <TestableEventData> events)
        {
            var receiver = new Mock <ITestablePartitionReceiver>();

            receiver.Setup(r => r.ReceiveAsync(
                               It.IsAny <int>(), It.IsAny <TimeSpan>()))
            .Returns(Task.FromResult(events));

            var client = new Mock <ITestableEventHubClient>();

            client.Setup(c => c.GetRuntimeInformationAsync())
            .Returns(Task.FromResult(new EventHubRuntimeInformation()
            {
                PartitionCount = 1, PartitionIds = new[] { "0" }
            }));
            client.Setup(c => c.CreateEpochReceiver(
                             It.IsAny <string>(), It.IsAny <string>(), It.IsAny <EventPosition>(), It.IsAny <long>(), It.IsAny <ReceiverOptions>()))
            .Returns(receiver.Object);

            var state = new MockReliableStateManager();

            return(new ReliableEventHubReceiverConnectionFactory(
                       client: client.Object,
                       state: state,
                       handlers: checkpointer => (ev, err) => new BatchCheckpointEventHandler(ev, err, checkpointer),
                       partitions: pk => Task.FromResult(pk.ToString()),
                       initialPosition: EventPosition.FromEnd(),
                       initialEpoch: 0,
                       loop: loop));
        }
Exemplo n.º 3
0
        protected async Task VerifyDataOnIoTHub(string moduleId)
        {
            Console.WriteLine($"Verifying data on IoTHub from {moduleId}");

            // First Verify if module is already running.
            await this.bootstrapper.VerifyModuleIsRunning(moduleId);

            var builder = new EventHubsConnectionStringBuilder(this.eventhubCompatibleEndpointWithEntityPath)
            {
                TransportType = this.eventHubClientTransportType
            };

            Console.WriteLine($"Receiving events from device '{this.context.DeviceId}' on Event Hub '{builder.EntityPath}'");

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

            this.proxy.ForEach(p => eventHubClient.WebProxy = p);

            PartitionReceiver eventHubReceiver = eventHubClient.CreateReceiver(
                "$Default",
                EventHubPartitionKeyResolver.ResolveToPartition(
                    this.context.DeviceId,
                    (await eventHubClient.GetRuntimeInformationAsync()).PartitionCount),
                EventPosition.FromEnd());

            // TODO: [Improvement] should verify test results without using event hub, which introduce latency.
            var result = new TaskCompletionSource <bool>();

            using (var cts = new CancellationTokenSource(TimeSpan.FromMinutes(20))) // This long timeout is needed in case event hub is slow to process messages
            {
                using (cts.Token.Register(() => result.TrySetCanceled()))
                {
                    eventHubReceiver.SetReceiveHandler(
                        new PartitionReceiveHandler(
                            eventData =>
                    {
                        eventData.SystemProperties.TryGetValue("iothub-connection-device-id", out object devId);
                        eventData.SystemProperties.TryGetValue("iothub-connection-module-id", out object modId);

                        if (devId != null && devId.ToString().Equals(this.context.DeviceId) &&
                            modId != null && modId.ToString().Equals(moduleId))
                        {
                            result.TrySetResult(true);
                            return(true);
                        }

                        return(false);
                    }));

                    await result.Task;
                }
            }

            Console.WriteLine("VerifyDataOnIoTHub completed.");
            await eventHubReceiver.CloseAsync();

            await eventHubClient.CloseAsync();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Determines the default start position for processing when no checkpoint is found
        /// </summary>
        /// <returns>An event position indicating the startup logic</returns>
        public static EventPosition GetStartPosition()
        {
            EventPosition startPosition;
            var           startDefinition = Environment.GetEnvironmentVariable("StartPosition");

            if (!bool.TryParse(Environment.GetEnvironmentVariable("StartInclusive"), out var startInclusive))
            {
                startInclusive = false;
            }

            if (string.IsNullOrWhiteSpace(startDefinition))
            {
                startDefinition = "Start";
            }
            startDefinition = startDefinition.Trim().ToLowerInvariant();

            if (string.IsNullOrWhiteSpace(startDefinition))
            {
                startDefinition = "Start";
            }


            switch (startDefinition)
            {
            case "end":
                startPosition = EventPosition.FromEnd();
                break;

            case "offset":
                startPosition = EventPosition.FromOffset(Environment.GetEnvironmentVariable("StartOffset"), startInclusive);
                break;

            case "sequence":
                if (!long.TryParse(Environment.GetEnvironmentVariable("StartSequence"), out var startSequence))
                {
                    startSequence = -1;
                }
                startPosition = EventPosition.FromSequenceNumber(startSequence, startInclusive);
                break;

            case "time":
                if (!int.TryParse(Environment.GetEnvironmentVariable("StartSeconds"), out var startSeconds))
                {
                    startSeconds = 0;
                }
                startPosition = EventPosition.FromEnqueuedTime(DateTime.UtcNow.AddSeconds(startSeconds * -1));
                break;

            default:
                startPosition = EventPosition.FromStart();
                break;
            }


            return(startPosition);
        }
Exemplo n.º 5
0
        protected async Task VerifyDataOnIoTHub(string moduleId)
        {
            var builder = new EventHubsConnectionStringBuilder(this.eventhubCompatibleEndpointWithEntityPath);

            builder.TransportType = this.eventHubClientTransportType;

            Console.WriteLine($"Receiving events from device '{this.context.Device.Id}' on Event Hub '{builder.EntityPath}'");

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

            PartitionReceiver eventHubReceiver = eventHubClient.CreateReceiver(
                "$Default",
                EventHubPartitionKeyResolver.ResolveToPartition(
                    this.context.Device.Id,
                    (await eventHubClient.GetRuntimeInformationAsync()).PartitionCount),
                EventPosition.FromEnd());

            var result = new TaskCompletionSource <bool>();

            using (var cts = new CancellationTokenSource(TimeSpan.FromMinutes(10)))
            {
                using (cts.Token.Register(() => result.TrySetCanceled()))
                {
                    eventHubReceiver.SetReceiveHandler(
                        new PartitionReceiveHandler(
                            eventData =>
                    {
                        eventData.SystemProperties.TryGetValue("iothub-connection-device-id", out object devId);
                        eventData.SystemProperties.TryGetValue("iothub-connection-module-id", out object modId);

                        if (devId != null && devId.ToString().Equals(this.context.Device.Id) &&
                            modId != null && modId.ToString().Equals(moduleId))
                        {
                            result.TrySetResult(true);
                            return(true);
                        }

                        return(false);
                    }));

                    await result.Task;
                }
            }

            Console.WriteLine("VerifyDataOnIoTHub completed.");
            await eventHubReceiver.CloseAsync();

            await eventHubClient.CloseAsync();
        }
Exemplo n.º 6
0
 public ReliableEventHubReceiverConnectionFactory(
     ITestableEventHubClient client,
     IReliableStateManager state,
     Func <SaveCheckpoint, CreateHandler> handlers,
     Func <long, Task <string> > partitions,
     EventPosition initialPosition = default,
     uint?initialEpoch             = default) : this(
         client : client,
         state : state,
         handlers : handlers,
         partitions : partitions,
         initialPosition : initialPosition ?? EventPosition.FromEnd(),
         initialEpoch : initialEpoch ?? 0,
         loop : Loops.Infinite())
 {
 }
        public void Read()
        {
            var connectionStringBuilder = new EventHubsConnectionStringBuilder(_connectionString)
            {
                EntityPath = _entityPath
            };

            var eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
            var messageCount   = 0;
            var receiver       = eventHubClient.CreateReceiver(_consumerGroup, "0", EventPosition.FromEnd());
            var receiver2      = eventHubClient.CreateReceiver(_consumerGroup, "1", EventPosition.FromEnd());
            var retries        = 5;
            var currentRetries = 0;

            Console.WriteLine("Reading from event hub...");
            do
            {
                var tasks = new List <Task <IEnumerable <EventData> > >
                {
                    receiver.ReceiveAsync(200),
                    receiver2.ReceiveAsync(200)
                };

                var messages = Task.WhenAll(tasks)
                               .GetAwaiter()
                               .GetResult();

                if (messages != null)
                {
                    foreach (var item in messages.Where(x => x != null).SelectMany(x => x))
                    {
                        OnMessageReceived(item);
                        messageCount++;
                    }
                }

                Console.WriteLine($"MessageCount : {messageCount}");
                currentRetries = messages.Any(x => x != null) ? 0 : currentRetries + 1;
            }while (retries > currentRetries);

            receiver.Close();
            receiver2.Close();
        }
        async Task InitialOffsetProviderWithEndOfStream()
        {
            // Use a randomly generated container name so that initial offset provider will be respected.
            var eventProcessorHost = new EventProcessorHost(
                string.Empty,
                PartitionReceiver.DefaultConsumerGroupName,
                TestUtility.EventHubsConnectionString,
                TestUtility.StorageConnectionString,
                Guid.NewGuid().ToString());

            var processorOptions = new EventProcessorOptions
            {
                ReceiveTimeout        = TimeSpan.FromSeconds(15),
                InitialOffsetProvider = partitionId => EventPosition.FromEnd(),
                MaxBatchSize          = 100
            };

            var runResult = await this.RunGenericScenario(eventProcessorHost, processorOptions);

            // We should have received only 1 event from each partition.
            Assert.False(runResult.ReceivedEvents.Any(kvp => kvp.Value.Count != 1), "One of the partitions didn't return exactly 1 event");
        }
        private static IEventHubReceiver CreateReceiver(EventHubPartitionSettings partitionSettings, string offset, ILogger logger, ITelemetryProducer telemetryProducer)
        {
            var connectionStringBuilder = new EventHubsConnectionStringBuilder(partitionSettings.Hub.ConnectionString)
            {
                EntityPath = partitionSettings.Hub.Path
            };
            EventHubClient client = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());

            EventPosition eventPosition;

            // If we have a starting offset, read from offset
            if (offset != EventHubConstants.StartOfStream)
            {
                logger.Info("Starting to read from EventHub partition {0}-{1} at offset {2}", partitionSettings.Hub.Path, partitionSettings.Partition, offset);
                eventPosition = EventPosition.FromOffset(offset, true);
            }
            // else, if configured to start from now, start reading from most recent data
            else if (partitionSettings.ReceiverOptions.StartFromNow)
            {
                eventPosition = EventPosition.FromEnd();
                logger.Info("Starting to read latest messages from EventHub partition {0}-{1}.", partitionSettings.Hub.Path, partitionSettings.Partition);
            }
            else
            // else, start reading from begining of the partition
            {
                eventPosition = EventPosition.FromStart();
                logger.Info("Starting to read messages from begining of EventHub partition {0}-{1}.", partitionSettings.Hub.Path, partitionSettings.Partition);
            }

            PartitionReceiver receiver = client.CreateReceiver(partitionSettings.Hub.ConsumerGroup, partitionSettings.Partition, eventPosition);

            if (partitionSettings.ReceiverOptions.PrefetchCount.HasValue)
            {
                receiver.PrefetchCount = partitionSettings.ReceiverOptions.PrefetchCount.Value;
            }

            return(new EventHubReceiverProxy(receiver));
        }
Exemplo n.º 10
0
        public async Task CreateReceiverWithEndOfStream()
        {
            var receiver        = default(PartitionReceiver);
            var partitionSender = default(PartitionSender);

            await using (var scope = await EventHubScope.CreateAsync(1))
            {
                var connectionString = TestUtility.BuildEventHubsConnectionString(scope.EventHubName);
                var ehClient         = EventHubClient.CreateFromConnectionString(connectionString);

                try
                {
                    // Randomly pick one of the available partitons.
                    var partitions = await this.GetPartitionsAsync(ehClient);

                    var partitionId = partitions[new Random().Next(partitions.Length)];
                    TestUtility.Log($"Randomly picked partition {partitionId}");

                    partitionSender = ehClient.CreatePartitionSender(partitionId);

                    // Send couple of messages before creating an EndOfStream receiver.
                    // We are not expecting to receive these messages would be sent before receiver creation.
                    for (int i = 0; i < 10; i++)
                    {
                        var ed = new EventData(new byte[1]);
                        await partitionSender.SendAsync(ed);
                    }

                    // Create a new receiver which will start reading from the end of the stream.
                    TestUtility.Log($"Creating a new receiver with offset EndOFStream");
                    receiver = ehClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, partitionId, EventPosition.FromEnd());

                    // Attemp to receive the message. This should return only 1 message.
                    var receiveTask = receiver.ReceiveAsync(100);

                    // Send a new message which is expected to go to the end of stream.
                    // We are expecting to receive only this message.
                    // Wait 5 seconds before sending to avoid race.
                    await Task.Delay(5000);

                    var eventToReceive = new EventData(new byte[1]);
                    eventToReceive.Properties["stamp"] = Guid.NewGuid().ToString();
                    await partitionSender.SendAsync(eventToReceive);

                    // Complete asyncy receive task.
                    var receivedMessages = await receiveTask;

                    // We should have received only 1 message from this call.
                    Assert.True(receivedMessages.Count() == 1, $"Didn't receive 1 message. Received {receivedMessages.Count()} messages(s).");

                    // Check stamp.
                    Assert.True(receivedMessages.Single().Properties["stamp"].ToString() == eventToReceive.Properties["stamp"].ToString()
                                , "Stamps didn't match on the message sent and received!");

                    TestUtility.Log("Received correct message as expected.");

                    // Next receive on this partition shouldn't return any more messages.
                    receivedMessages = await receiver.ReceiveAsync(100, TimeSpan.FromSeconds(15));

                    Assert.True(receivedMessages == null, $"Received messages at the end.");
                }
                finally
                {
                    await Task.WhenAll(
                        partitionSender.CloseAsync(),
                        receiver.CloseAsync(),
                        ehClient.CloseAsync());
                }
            }
        }
        public async Task InvokeOnNull()
        {
            await using (var scope = await EventHubScope.CreateAsync(1))
            {
                var connectionString  = TestUtility.BuildEventHubsConnectionString(scope.EventHubName);
                var ehClient          = EventHubClient.CreateFromConnectionString(connectionString);
                var partitionReceiver = ehClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, "0", EventPosition.FromEnd());

                try
                {
                    var tcs     = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);
                    var handler = new TestPartitionReceiveHandler();

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

                    partitionReceiver.SetReceiveHandler(handler, true);
                    await tcs.Task.WithTimeout(TimeSpan.FromSeconds(120));
                }
                finally
                {
                    // Unregister handler.
                    partitionReceiver.SetReceiveHandler(null);

                    // Close clients.

                    await Task.WhenAll(
                        partitionReceiver.CloseAsync(),
                        ehClient.CloseAsync());
                }
            }
        }
 private Task <PartitionReceiver> SetupFromEndOfStreamAsync(string consumerGroup, string partitionId)
 {
     return(Task.FromResult(_client.CreateReceiver(consumerGroup, partitionId, EventPosition.FromEnd())));
 }
Exemplo n.º 13
0
        private static async Task ReceiveMessagesFromDeviceAsync(string partition, CancellationToken ct)
        {
            var  eventHubReceiver = eventHubClient.CreateReceiver("$Default", partition, EventPosition.FromEnd());
            bool written          = false;

            while (!written)
            {
                if (ct.IsCancellationRequested)
                {
                    break;
                }
                Console.WriteLine("Listening for messages on: " + partition);
                var events = await eventHubReceiver.ReceiveAsync(100);

                // If there is data in the batch, process it.
                if (events == null)
                {
                    continue;
                }

                foreach (EventData eventData in events)
                {
                    Console.WriteLine("Message received on partition {0} enqueued at {1} offset {2} seqno {3}:",
                                      partition,
                                      eventData.SystemProperties["x-opt-enqueued-time"],
                                      eventData.SystemProperties["x-opt-offset"],
                                      eventData.SystemProperties["x-opt-sequence-number"]);

                    var blockBlob = cloudBlobContainer.GetBlockBlobReference(partition);
                    HubCheckpointInfo input;
                    using (var stream = await blockBlob.OpenReadAsync())
                    {
                        var buff = new byte[stream.Length];

                        await stream.ReadAsync(buff, 0, (int)stream.Length);

                        string content = Encoding.UTF8.GetString(buff);
                        input = JsonConvert.DeserializeObject <HubCheckpointInfo>(content);
                        Console.WriteLine("read {0}", content);
                        input.Offset         = eventData.SystemProperties["x-opt-offset"].ToString();
                        input.SequenceNumber = eventData.SystemProperties["x-opt-sequence-number"].ToString();
                    }
                    string output = JsonConvert.SerializeObject(input);
                    using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(output), false))
                    {
                        blockBlob.UploadFromStream(stream, null);
                    }
                    Console.WriteLine("wrote {0}", output);
                    written = true;
                    break;
                }
            }
        }
        public async Task DefaultBehaviorNoInvokeOnNull()
        {
            await using (var scope = await EventHubScope.CreateAsync(1))
            {
                var connectionString  = TestUtility.BuildEventHubsConnectionString(scope.EventHubName);
                var ehClient          = EventHubClient.CreateFromConnectionString(connectionString);
                var partitionReceiver = ehClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, "0", EventPosition.FromEnd());

                try
                {
                    var nullCompletionSource = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);
                    var dataCompletionSource = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);
                    var handler = new TestPartitionReceiveHandler();

                    handler.EventsReceived += (s, eventDatas) =>
                    {
                        if (eventDatas == null)
                        {
                            TestUtility.Log("Received null.");
                            nullCompletionSource.TrySetResult(true);
                        }
                        else
                        {
                            TestUtility.Log("Received message.");
                            dataCompletionSource.TrySetResult(true);
                        }
                    };

                    partitionReceiver.SetReceiveHandler(handler);
                    await Assert.ThrowsAsync <TimeoutException>(() => nullCompletionSource.Task.WithTimeout(TimeSpan.FromSeconds(120)));

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

                    await dataCompletionSource.Task.WithTimeout(TimeSpan.FromSeconds(60), timeoutCallback : () => throw new TimeoutException("The data event was not received"));
                }
                finally
                {
                    // Unregister handler.
                    partitionReceiver.SetReceiveHandler(null);

                    // Close clients.
                    await Task.WhenAll(
                        partitionReceiver.CloseAsync(),
                        ehClient.CloseAsync());
                }
            }
        }
Exemplo n.º 15
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();
        }
Exemplo n.º 16
0
        static async Task SendReceiveAsync(EventHubClient ehClient)
        {
            Console.WriteLine("Fetching eventhub description to discover partitions");
            var ehDesc = await ehClient.GetRuntimeInformationAsync();

            Console.WriteLine($"Discovered partitions as {string.Join(",", ehDesc.PartitionIds)}");

            var receiveTasks = ehDesc.PartitionIds.Select(async partitionId =>
            {
                Console.WriteLine($"Initiating receiver on partition {partitionId}");
                var receiver = ehClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, partitionId, EventPosition.FromEnd());

                while (true)
                {
                    var events = await receiver.ReceiveAsync(1, TimeSpan.FromSeconds(15));
                    if (events == null)
                    {
                        break;
                    }

                    var eventData = events.FirstOrDefault();
                    Console.WriteLine($"Received from partition {partitionId} with message content '" + Encoding.UTF8.GetString(eventData.Body.Array) + "'");
                }

                await receiver.CloseAsync();
            }).ToList <Task>();

            await Task.Delay(5000);

            Console.WriteLine("Sending single event");
            await ehClient.SendAsync(new EventData(Encoding.UTF8.GetBytes($"{DateTime.UtcNow}")));

            Console.WriteLine("Send done");

            Console.WriteLine("Waiting for receivers to complete");
            await Task.WhenAll(receiveTasks);

            Console.WriteLine("All receivers completed");

            await ehClient.CloseAsync();

            Console.WriteLine("Press enter to exit.");

            Console.ReadLine();
        }
Exemplo n.º 17
0
        public async ValueTask ExecuteAsync(IConsole console)
        {
            var            tcs                = new TaskCompletionSource <bool>();
            EventHubClient client             = EventHubClient.CreateFromConnectionString($"Endpoint=sb://{Namespace}.servicebus.windows.net;EntityPath={EventHub};SharedAccessKeyName={SasKeyName};SharedAccessKey={SasKeyValue}");
            var            runtimeInformation = await client.GetRuntimeInformationAsync();

            var partitionReceivers = runtimeInformation.PartitionIds.Select(partitionId => client.CreateReceiver("$Default", partitionId, ShouldReadFromStart ? EventPosition.FromStart() : EventPosition.FromEnd())).ToList();
            Channel <string> queue = Channel.CreateBounded <string>(10000);
            PartitionHandler pr    = new PartitionHandler(queue);

            Task readerTask = Task.Run(async() =>
            {
                while (await queue.Reader.WaitToReadAsync())
                {
                    await foreach (string s in queue.Reader.ReadAllAsync())
                    {
                        await console.Output.WriteLineAsync(s);
                    }
                }
            });

            foreach (var partitionReceiver in partitionReceivers)
            {
                partitionReceiver.SetReceiveHandler(pr);
            }

            Console.CancelKeyPress += async(s, e) =>
            {
                await client.CloseAsync();

                queue.Writer.TryComplete();
                await readerTask;
                tcs.TrySetResult(true);
            };

            await tcs.Task;
        }
        public PartitionReceiver ListenForResponses(uint partitionId)
        {
            var client = GetResponseClient();

            return(ResponseReceiver = client.CreateReceiver("$Default", partitionId.ToString(), EventPosition.FromEnd()));
        }
        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();
            }
        }
Exemplo n.º 20
0
        public async Task ClosingEventHubClientClosesReceiverEntities()
        {
            await using (var scope = await EventHubScope.CreateAsync(1))
            {
                var connectionString = TestUtility.BuildEventHubsConnectionString(scope.EventHubName);
                var ehClient         = EventHubClient.CreateFromConnectionString(connectionString);
                var ehReceiver       = ehClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, "0", EventPosition.FromStart());
                var ehReceiverEpoch  = ehClient.CreateEpochReceiver(PartitionReceiver.DefaultConsumerGroupName, "0", EventPosition.FromEnd(), 0);

                await ehClient.CloseAsync();

                Assert.True(ehReceiver.IsClosed, "ehReceiver.IsClosed is not true.");
                Assert.True(ehReceiverEpoch.IsClosed, "ehReceiverEpoch.IsClosed is not true.");
            }
        }
Exemplo n.º 21
0
        private static async Task ReceiveMessagesFromDeviceAsync(string partition, CancellationToken cancellationToken)
        {
            var eventHubReceiver = _eventHubClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, partition, EventPosition.FromEnd());

            while (true)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    break;
                }
                var eventData = await eventHubReceiver.ReceiveAsync(1);

                if (eventData == null)
                {
                    continue;
                }

                foreach (var d in eventData)
                {
                    var data = Encoding.UTF8.GetString(d.Body);
                    Console.WriteLine($"{d.SystemProperties["iothub-connection-device-id"]}: {data}");
                    //Console.WriteLine("Message received. Partition: {0} Data: '{1}'", partition, data);
                }
            }
        }
Exemplo n.º 22
0
        private async Task ReceiveMessagesFromDeviceAsync(string partition, CancellationToken cancellationToken)
        {
            var eventHubReceiver = eventHubClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, partition, EventPosition.FromEnd());

            while (true)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    break;
                }
                var eventData = await eventHubReceiver.ReceiveAsync(1);

                if (eventData == null)
                {
                    continue;
                }

                foreach (var d in eventData)
                {
                    var   data = Encoding.UTF8.GetString(d.Body.ToArray());
                    Event ev   = JsonConvert.DeserializeObject <Event>(data);;
                    switch (ev.EventName)
                    {
                    case LockEvent.EventNameConstant:
                        ev = JsonConvert.DeserializeObject <LockEvent>(data);
                        break;

                    case AccessEvent.EventNameConstant:
                        ev = JsonConvert.DeserializeObject <AccessEvent>(data);
                        break;

                    case UnauthorizedAccessEvent.EventNameConstant:
                        ev = JsonConvert.DeserializeObject <UnauthorizedAccessEvent>(data);
                        break;

                    case AlarmEvent.EventNameConstant:
                        ev = JsonConvert.DeserializeObject <AlarmEvent>(data);
                        break;
                    }

                    string deviceId = d.SystemProperties["iothub-connection-device-id"].ToString();

                    var message = $"{deviceId}: {ev.EventName}";

                    var payload = JsonConvert.SerializeObject(new
                    {
                        notification = new
                        {
                            title        = "AccessControl",
                            body         = message,
                            priority     = "10",
                            sound        = "default",
                            time_to_live = "600"
                        },
                        data = new
                        {
                            title = "AccessControl",
                            body  = message,
                            url   = "https://example.com"
                        }
                    });

                    await notificationHubClient.SendFcmNativeNotificationAsync(payload, string.Empty);

                    await hubContext.Clients.All.ReceiveAlarmNotification(new AlarmNotification()
                    {
                        Title = data
                    });

                    Domain.Enums.AccessEvent e = Domain.Enums.AccessEvent.Undefined;

                    if (ev is AccessControl.Messages.Events.LockEvent f)
                    {
                        if (f.LockState == LockState.Locked)
                        {
                            e = Domain.Enums.AccessEvent.Locked;
                        }
                        else
                        {
                            e = Domain.Enums.AccessEvent.Unlocked;
                        }
                    }
                    if (ev is AccessControl.Messages.Events.AlarmEvent g)
                    {
                        if (g.AlarmState == AlarmState.Armed)
                        {
                            e = Domain.Enums.AccessEvent.Armed;
                        }
                        else
                        {
                            e = Domain.Enums.AccessEvent.Disarmed;
                        }
                    }
                    else if (ev is AccessControl.Messages.Events.AccessEvent)
                    {
                        e = Domain.Enums.AccessEvent.Access;
                    }
                    else if (ev is AccessControl.Messages.Events.UnauthorizedAccessEvent)
                    {
                        e = Domain.Enums.AccessEvent.UnauthorizedAccess;
                    }

                    await _accessLogger.LogAsync(null, e, null, string.Empty);
                }
            }
        }
Exemplo n.º 23
0
        private static async Task MainAsync()
        {
            Console.WriteLine("Connecting to the Event Hub...");

            var eventHubClient     = EventHubClient.CreateFromConnectionString(eventHubConnectionString);
            var runtimeInformation = await eventHubClient.GetRuntimeInformationAsync();

            var partitionReceivers = runtimeInformation.PartitionIds
                                     .Select(partitionId =>
                                             eventHubClient.CreateReceiver("terminaldirect", partitionId, EventPosition.FromEnd())).ToList();

            Console.WriteLine("Waiting for incoming events...");

            foreach (var partitionReceiver in partitionReceivers)
            {
                partitionReceiver.SetReceiveHandler(new TerminalPartitionReceiveHandler(partitionReceiver.PartitionId));
            }

            Console.WriteLine("Press any key to shutdown");
            Console.ReadLine();
            await eventHubClient.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();
            }
        }
Exemplo n.º 25
0
        public static async Task Main(string[] args)
        {
            var connectionStringBuilder = new EventHubsConnectionStringBuilder(EventHubConnectionString)
            {
                EntityPath = EventHubName
            };

            var eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());

            var ehInfo = await eventHubClient.GetRuntimeInformationAsync();

            var receiveTasks = ehInfo.PartitionIds.Select(async partitionId =>
            {
                var receiver = eventHubClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, partitionId, EventPosition.FromEnd());

                Console.WriteLine($"{DateTime.Now} PARTITION {partitionId}: Starting to receive");

                while (true)
                {
                    var messages = await receiver.ReceiveAsync(10);
                    if (messages != null)
                    {
                        foreach (var message in messages)
                        {
                            var messageBody = ASCIIEncoding.UTF8.GetString(message.Body.Array);
                            Console.WriteLine($"{DateTime.Now} > PARTITION {partitionId}: {messageBody}");
                        }
                    }
                }
            });

            await Task.WhenAll(receiveTasks);
        }
Exemplo n.º 26
0
        public async Task Connect()
        {
            if (Connected)
            {
                return;
            }
            await SemaphoreSlim.WaitAsync();

            if (Connected)
            {
                return;
            }
            try
            {
                var runTimeInformation = await _eventHubClient.GetRuntimeInformationAsync();

                foreach (var partitionId in runTimeInformation.PartitionIds)
                {
                    var receiver = _eventHubClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, partitionId, EventPosition.FromEnd());
                    _receivers.Add(receiver);
                }
                _timer = new Timer(1000)
                {
                    AutoReset = true
                };
                _timer.Elapsed += Poll;
                _timer.Start();
                Connected = true;
            }
            finally
            {
                SemaphoreSlim.Release();
            }
        }
Exemplo n.º 27
0
        internal static void ConfigureOptions(EventHubOptions options)
        {
            string offsetType = options?.InitialOffsetOptions?.Type?.ToLower() ?? String.Empty;

            if (!offsetType.Equals(String.Empty))
            {
                switch (offsetType)
                {
                case "fromstart":
                    options.EventProcessorOptions.InitialOffsetProvider = (s) => { return(EventPosition.FromStart()); };
                    break;

                case "fromend":
                    options.EventProcessorOptions.InitialOffsetProvider = (s) => { return(EventPosition.FromEnd()); };
                    break;

                case "fromenqueuedtime":
                    try
                    {
                        DateTime enqueuedTimeUTC = DateTime.Parse(options.InitialOffsetOptions.EnqueuedTimeUTC).ToUniversalTime();
                        options.EventProcessorOptions.InitialOffsetProvider = (s) => { return(EventPosition.FromEnqueuedTime(enqueuedTimeUTC)); };
                    }
                    catch (System.FormatException fe)
                    {
                        string message = $"{nameof(EventHubOptions)}:{nameof(InitialOffsetOptions)}:{nameof(InitialOffsetOptions.EnqueuedTimeUTC)} is configured with an invalid format. " +
                                         "Please use a format supported by DateTime.Parse().  e.g. 'yyyy-MM-ddTHH:mm:ssZ'";
                        throw new InvalidOperationException(message, fe);
                    }
                    break;

                default:
                    throw new InvalidOperationException("An unsupported value was supplied for initialOffsetOptions.type");
                }
                // If not specified, EventProcessor's default offset will apply
            }
        }