| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Text; |
| | 6 | | using System.Threading; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | | using Azure.Messaging.EventHubs.Consumer; |
| | 9 | | using Azure.Messaging.EventHubs.Producer; |
| | 10 | | using Azure.Messaging.EventHubs.Samples.Infrastructure; |
| | 11 | |
|
| | 12 | | namespace Azure.Messaging.EventHubs.Samples |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// An introduction to reading all events available from an Event Hub. |
| | 16 | | /// </summary> |
| | 17 | | /// |
| | 18 | | public class Sample05_ReadEvents : IEventHubsSample |
| | 19 | | { |
| | 20 | | /// <summary> |
| | 21 | | /// The name of the sample. |
| | 22 | | /// </summary> |
| | 23 | | /// |
| 0 | 24 | | public string Name => nameof(Sample05_ReadEvents); |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// A short description of the sample. |
| | 28 | | /// </summary> |
| | 29 | | /// |
| 0 | 30 | | public string Description => "An introduction to reading all events available from an Event Hub."; |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Runs the sample using the specified Event Hubs connection information. |
| | 34 | | /// </summary> |
| | 35 | | /// |
| | 36 | | /// <param name="connectionString">The connection string for the Event Hubs namespace that the sample should tar |
| | 37 | | /// <param name="eventHubName">The name of the Event Hub, sometimes known as its path, that she sample should ru |
| | 38 | | /// |
| | 39 | | public async Task RunAsync(string connectionString, |
| | 40 | | string eventHubName) |
| | 41 | | { |
| | 42 | | // To start, we'll publish a small number of events using a producer client. To ensure that our client is a |
| | 43 | | // take advantage of the asynchronous dispose when we are done or when an exception is encountered. |
| | 44 | |
|
| 0 | 45 | | await using (var producerClient = new EventHubProducerClient(connectionString, eventHubName)) |
| | 46 | | { |
| 0 | 47 | | using EventDataBatch eventBatch = await producerClient.CreateBatchAsync(); |
| 0 | 48 | | eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("Hello, Event Hubs!"))); |
| 0 | 49 | | eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("The middle event is this one"))); |
| 0 | 50 | | eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("Goodbye, Event Hubs!"))); |
| | 51 | |
|
| 0 | 52 | | await producerClient.SendAsync(eventBatch); |
| | 53 | |
|
| 0 | 54 | | Console.WriteLine("The event batch has been published."); |
| 0 | 55 | | } |
| | 56 | |
|
| | 57 | | // Now that the events have been published, we'll read back all events from the Event Hub using a consumer c |
| | 58 | | // It's important to note that because events are not removed from the partition when consuming, that if you |
| | 59 | | // an existing Event Hub for the sample, you will see events that were published prior to running this sampl |
| | 60 | | // as those from the batch that we just sent. |
| | 61 | | // |
| | 62 | | // An Event Hub consumer is associated with a specific Event Hub and consumer group. The consumer group is |
| | 63 | | // a label that identifies one or more consumers as a set. Often, consumer groups are named after the respo |
| | 64 | | // of the consumer in an application, such as "Telemetry" or "OrderProcessing". When an Event Hub is create |
| | 65 | | // consumer group is created with it, called "$Default." |
| | 66 | | // |
| | 67 | | // Each consumer has a unique view of the events in a partition that it reads from, meaning that events are |
| | 68 | | // consumers and are not removed from the partition when a consumer reads them. This allows for one or more |
| | 69 | | // process events from the partition at different speeds and beginning with different events without interfe |
| | 70 | | // one another. |
| | 71 | | // |
| | 72 | | // When events are published, they will continue to exist in the partition and be available for consuming un |
| | 73 | | // reach an age where they are older than the retention period. |
| | 74 | | // (see: https://docs.microsoft.com/en-us/azure/event-hubs/event-hubs-faq#what-is-the-maximum-retention-peri |
| | 75 | | // |
| | 76 | | // In this example, we will create our consumer client using the default consumer group that is created with |
| | 77 | | // Our consumer will begin watching the partition at the very end, reading only new events that we will publ |
| | 78 | |
|
| 0 | 79 | | await using (var consumerClient = new EventHubConsumerClient(EventHubConsumerClient.DefaultConsumerGroupName |
| | 80 | | { |
| | 81 | | // To ensure that we do not wait for an indeterminate length of time, we'll stop reading after we receiv |
| | 82 | | // fresh Event Hub, those will be the three that we had published. We'll also ask for cancellation afte |
| | 83 | | // safe. |
| | 84 | |
|
| 0 | 85 | | using CancellationTokenSource cancellationSource = new CancellationTokenSource(); |
| 0 | 86 | | cancellationSource.CancelAfter(TimeSpan.FromSeconds(90)); |
| | 87 | |
|
| 0 | 88 | | int eventsRead = 0; |
| 0 | 89 | | int maximumEvents = 3; |
| | 90 | |
|
| 0 | 91 | | await foreach (PartitionEvent partitionEvent in consumerClient.ReadEventsAsync(cancellationSource.Token) |
| | 92 | | { |
| 0 | 93 | | Console.WriteLine($"Event Read: { Encoding.UTF8.GetString(partitionEvent.Data.Body.ToArray()) }"); |
| 0 | 94 | | eventsRead++; |
| | 95 | |
|
| 0 | 96 | | if (eventsRead >= maximumEvents) |
| | 97 | | { |
| | 98 | | break; |
| | 99 | | } |
| | 100 | | } |
| 0 | 101 | | } |
| | 102 | |
|
| | 103 | | // At this point, our clients have both passed their "using" scopes and have safely been disposed of. We |
| | 104 | | // have no further obligations. |
| | 105 | |
|
| 0 | 106 | | Console.WriteLine(); |
| 0 | 107 | | } |
| | 108 | | } |
| | 109 | | } |