| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Threading.Tasks; |
| | 6 | | using Azure.Messaging.EventHubs.Producer; |
| | 7 | | using Azure.Messaging.EventHubs.Samples.Infrastructure; |
| | 8 | |
|
| | 9 | | namespace Azure.Messaging.EventHubs.Samples |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// An introduction to Event Hubs, illustrating how to create a client and explore an Event Hub. |
| | 13 | | /// </summary> |
| | 14 | | /// |
| | 15 | | public class Sample01_HelloWorld : IEventHubsSample |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// The name of the sample. |
| | 19 | | /// </summary> |
| | 20 | | /// |
| 0 | 21 | | public string Name => nameof(Sample01_HelloWorld); |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// A short description of the sample. |
| | 25 | | /// </summary> |
| | 26 | | /// |
| 0 | 27 | | public string Description => "An introduction to Event Hubs, illustrating how to create a client and explore an |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Runs the sample using the specified Event Hubs connection information. |
| | 31 | | /// </summary> |
| | 32 | | /// |
| | 33 | | /// <param name="connectionString">The connection string for the Event Hubs namespace that the sample should tar |
| | 34 | | /// <param name="eventHubName">The name of the Event Hub, sometimes known as its path, that she sample should ru |
| | 35 | | /// |
| | 36 | | public async Task RunAsync(string connectionString, |
| | 37 | | string eventHubName) |
| | 38 | | { |
| | 39 | | // To interact with an Event Hubs, a client is needed. There are clients associated with each of the core a |
| | 40 | | // with an Event Hub, naming publishing events and consuming events. Each of these clients manages resource |
| | 41 | | // explicitly closed or disposed, but it is not necessary to do both. |
| | 42 | | // |
| | 43 | | // In this example, we will create a producer client and use it to inspect the properties of an Event Hub. |
| | 44 | | // advantage of the new asynchronous dispose to ensure that clean-up is performed when we are done or when a |
| | 45 | |
|
| 0 | 46 | | await using (var producerClient = new EventHubProducerClient(connectionString, eventHubName)) |
| | 47 | | { |
| | 48 | | // Using the client, we will inspect the Event Hub that it is connected to, getting |
| | 49 | | // access to its informational properties. |
| | 50 | |
|
| 0 | 51 | | EventHubProperties properties = await producerClient.GetEventHubPropertiesAsync(); |
| | 52 | |
|
| 0 | 53 | | Console.WriteLine("The Event Hub has the following properties:"); |
| 0 | 54 | | Console.WriteLine($"\tThe path to the Event Hub from the namespace is: { properties.Name }"); |
| 0 | 55 | | Console.WriteLine($"\tThe Event Hub was created at: { properties.CreatedOn.ToString("yyyy-MM-dd hh:mm:ss |
| 0 | 56 | | Console.WriteLine(); |
| | 57 | |
|
| | 58 | | // Partitions of an Event Hub are an important concept. Using the Event Hub properties, we'll inspect e |
| | 59 | | // getting access to partition-level properties. |
| | 60 | |
|
| 0 | 61 | | foreach (string partitionId in properties.PartitionIds) |
| | 62 | | { |
| 0 | 63 | | PartitionProperties partitionProperties = await producerClient.GetPartitionPropertiesAsync(partition |
| | 64 | |
|
| 0 | 65 | | Console.WriteLine($"\tPartition: { partitionProperties.Id }"); |
| 0 | 66 | | Console.WriteLine($"\t\tThe partition contains no events: { partitionProperties.IsEmpty }"); |
| 0 | 67 | | Console.WriteLine($"\t\tThe first sequence number of an event in the partition is: { partitionProper |
| 0 | 68 | | Console.WriteLine($"\t\tThe last sequence number of an event in the partition is: { partitionPropert |
| 0 | 69 | | Console.WriteLine($"\t\tThe last offset of an event in the partition is: { partitionProperties.LastE |
| 0 | 70 | | Console.WriteLine($"\t\tThe last time that an event was enqueued in the partition is: { partitionPro |
| 0 | 71 | | Console.WriteLine(); |
| | 72 | | } |
| | 73 | | } |
| | 74 | |
|
| | 75 | | // At this point, our client has passed its "using" scope and has safely been disposed of. We have no |
| | 76 | | // further obligations. |
| | 77 | |
|
| 0 | 78 | | Console.WriteLine(); |
| 0 | 79 | | } |
| | 80 | | } |
| | 81 | | } |