示例#1
0
        public void CloseClosesTheTransportConsumer()
        {
            var transportConsumer = new ObservableTransportConsumerMock();
            var receiver          = new PartitionReceiver("group", "0", "hub", true, TimeSpan.Zero, transportConsumer);

            receiver.Close();
            Assert.That(transportConsumer.WasCloseCalled, Is.True);
        }
示例#2
0
        private static void receive(PartitionReceiver partitionReceiver, string requestId)
        {
            try
            {
                while (true)
                {
                    var receiveTask = partitionReceiver.ReceiveAsync(1, TimeSpan.FromSeconds(10));
                    receiveTask.Wait();
                    IEnumerable <EventData> receivedEvents = receiveTask.Result;

                    if (receivedEvents != null)
                    {
                        foreach (EventData eventData in receivedEvents)
                        {
                            if (eventData != null)
                            {
                                if (eventData.Properties != null && eventData.Properties.ContainsKey(COMMAND_REQUEST_ID_PROPERTY_NAME))
                                {
                                    string payload = Encoding.UTF8.GetString(eventData.Body);
                                    Console.WriteLine("Received an update on the async command:");
                                    Console.WriteLine();
                                    foreach (string propertyKey in eventData.Properties.Keys)
                                    {
                                        Console.WriteLine("    " + propertyKey + ":" + eventData.Properties[propertyKey]);
                                    }
                                    Console.WriteLine();
                                    Console.WriteLine("    " + "Update Payload: ");
                                    Console.WriteLine("        " + payload);
                                    Console.WriteLine();

                                    if (payload.Contains("100%"))
                                    {
                                        Console.WriteLine("Async command has finished, enter any key to finish\n");
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (ThreadInterruptedException e)
            {
                //Thread was aborted, so allow it to end
                partitionReceiver.Close();
            }
        }
示例#3
0
        public async Task ReadFromSpecificPartition()
        {
            await using var scope = await EventHubScope.CreateAsync(1);

            #region Snippet:EventHubs_Migrate_T1_ReadFromSpecificPartition
#if SNIPPET
            var connectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>";
            var eventHubName     = "<< NAME OF THE EVENT HUB >>";
            var consumerGroup    = "<< NAME OF THE EVENT HUB CONSUMER GROUP >>";
#else
            var connectionString = TestUtility.EventHubsConnectionString;
            var eventHubName     = scope.EventHubName;
            var consumerGroup    = PartitionReceiver.DefaultConsumerGroupName;
#endif

            var builder = new EventHubsConnectionStringBuilder(connectionString);
            builder.EntityPath = eventHubName;

            EventHubClient    client   = EventHubClient.CreateFromConnectionString(builder.ToString());
            PartitionReceiver receiver = default;

            try
            {
                string firstPartition = (await client.GetRuntimeInformationAsync()).PartitionIds.First();
                receiver = client.CreateReceiver(consumerGroup, firstPartition, EventPosition.FromStart());

                IEnumerable <EventData> events = await receiver.ReceiveAsync(50);

                foreach (var eventData in events)
                {
                    Debug.WriteLine($"Read event of length { eventData.Body.Count } from { firstPartition }");
                }
            }
            finally
            {
                receiver?.Close();
                client.Close();
            }

            #endregion
        }