| | 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.Processor.Samples.Infrastructure; |
| | 10 | | using Azure.Storage.Blobs; |
| | 11 | |
|
| | 12 | | namespace Azure.Messaging.EventHubs.Processor.Samples |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// An introduction to the Event Processor client, illustrating how to participate in initialization for a partiti |
| | 16 | | /// </summary> |
| | 17 | | /// |
| | 18 | | public class Sample05_InitializeAPartition : IEventHubsBlobCheckpointSample |
| | 19 | | { |
| | 20 | | /// <summary> |
| | 21 | | /// The name of the sample. |
| | 22 | | /// </summary> |
| | 23 | | /// |
| 0 | 24 | | public string Name => nameof(Sample05_InitializeAPartition); |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// A short description of the sample. |
| | 28 | | /// </summary> |
| | 29 | | /// |
| 0 | 30 | | public string Description => "An introduction to the Event Processor client, illustrating how to participate in |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Runs the sample using the specified Event Hubs and Azure storage connection information. |
| | 34 | | /// </summary> |
| | 35 | | /// |
| | 36 | | /// <param name="eventHubsConnectionString">The connection string for the Event Hubs namespace that the sample s |
| | 37 | | /// <param name="eventHubName">The name of the Event Hub, sometimes known as its path, that the sample should ru |
| | 38 | | /// <param name="blobStorageConnectionString">The connection string for the storage account where checkpoints an |
| | 39 | | /// <param name="blobContainerName">The name of the blob storage container where checkpoints and state should be |
| | 40 | | /// |
| | 41 | | public async Task RunAsync(string eventHubsConnectionString, |
| | 42 | | string eventHubName, |
| | 43 | | string blobStorageConnectionString, |
| | 44 | | string blobContainerName) |
| | 45 | | { |
| | 46 | | // When the Event Processor client begins processing, it will take ownership over a set of Event Hub partiti |
| | 47 | | // partition that the processor claims, the first step in processing is to initialize the partition. In ord |
| | 48 | | // track partitions owned by the processor and to participate in the initialization, a "PartitionInitializin |
| | 49 | | // the processor. |
| | 50 | | // |
| | 51 | | // When a partition is initialized, one of the decisions made is where in the partition's event stream to be |
| | 52 | | // where a checkpoint exists for a partition, processing will begin at the next available event after the ch |
| | 53 | | // is found for a partition, a default location is used. |
| | 54 | | // |
| | 55 | | // One of the common reasons that you may choose to participate in initialization is to influence where to b |
| | 56 | | // is not found, overriding the default. To achieve this, you'll set the associated property on the event a |
| | 57 | |
|
| 0 | 58 | | string consumerGroup = EventHubConsumerClient.DefaultConsumerGroupName; |
| 0 | 59 | | BlobContainerClient storageClient = new BlobContainerClient(blobStorageConnectionString, blobContainerName); |
| 0 | 60 | | EventProcessorClient processor = new EventProcessorClient(storageClient, consumerGroup, eventHubsConnectionS |
| | 61 | |
|
| | 62 | | // The handler for partition initialization will set the default position for all partitions, asking to begi |
| | 63 | | // point in the stream. |
| | 64 | |
|
| | 65 | | Task partitionInitializingHandler(PartitionInitializingEventArgs eventArgs) |
| | 66 | | { |
| 0 | 67 | | if (eventArgs.CancellationToken.IsCancellationRequested) |
| | 68 | | { |
| 0 | 69 | | return Task.CompletedTask; |
| | 70 | | } |
| | 71 | |
|
| | 72 | | try |
| | 73 | | { |
| 0 | 74 | | eventArgs.DefaultStartingPosition = EventPosition.Latest; |
| 0 | 75 | | Console.WriteLine($"Initialized partition: { eventArgs.PartitionId }"); |
| 0 | 76 | | } |
| 0 | 77 | | catch (Exception ex) |
| | 78 | | { |
| | 79 | | // For real-world scenarios, you should take action appropriate to your application. For our exampl |
| | 80 | | // the exception to the console. |
| | 81 | |
|
| 0 | 82 | | Console.WriteLine(); |
| 0 | 83 | | Console.WriteLine($"An error was observed while initializing partition: { eventArgs.PartitionId }. |
| 0 | 84 | | Console.WriteLine(); |
| 0 | 85 | | } |
| | 86 | |
|
| 0 | 87 | | return Task.CompletedTask; |
| | 88 | | } |
| | 89 | |
|
| | 90 | | // For this example, events will just be logged to the console. |
| | 91 | |
|
| | 92 | | Task processEventHandler(ProcessEventArgs eventArgs) |
| | 93 | | { |
| 0 | 94 | | if (eventArgs.CancellationToken.IsCancellationRequested) |
| | 95 | | { |
| 0 | 96 | | return Task.CompletedTask; |
| | 97 | | } |
| | 98 | |
|
| | 99 | | try |
| | 100 | | { |
| 0 | 101 | | Console.WriteLine($"Event Received: { Encoding.UTF8.GetString(eventArgs.Data.Body.ToArray()) }"); |
| 0 | 102 | | } |
| 0 | 103 | | catch (Exception ex) |
| | 104 | | { |
| | 105 | | // For real-world scenarios, you should take action appropriate to your application. For our exampl |
| | 106 | | // the exception to the console. |
| | 107 | |
|
| 0 | 108 | | Console.WriteLine(); |
| 0 | 109 | | Console.WriteLine($"An error was observed while processing events. Message: { ex.Message }"); |
| 0 | 110 | | Console.WriteLine(); |
| 0 | 111 | | } |
| | 112 | |
|
| 0 | 113 | | return Task.CompletedTask; |
| | 114 | | }; |
| | 115 | |
|
| | 116 | | // For this example, exceptions will just be logged to the console. |
| | 117 | |
|
| | 118 | | Task processErrorHandler(ProcessErrorEventArgs eventArgs) |
| | 119 | | { |
| 0 | 120 | | if (eventArgs.CancellationToken.IsCancellationRequested) |
| | 121 | | { |
| 0 | 122 | | return Task.CompletedTask; |
| | 123 | | } |
| | 124 | |
|
| 0 | 125 | | Console.WriteLine("==============================="); |
| 0 | 126 | | Console.WriteLine($"The error handler was invoked during the operation: { eventArgs.Operation ?? "Unknow |
| 0 | 127 | | Console.WriteLine("==============================="); |
| 0 | 128 | | Console.WriteLine(); |
| | 129 | |
|
| 0 | 130 | | return Task.CompletedTask; |
| | 131 | | } |
| | 132 | |
|
| 0 | 133 | | processor.PartitionInitializingAsync += partitionInitializingHandler; |
| 0 | 134 | | processor.ProcessEventAsync += processEventHandler; |
| 0 | 135 | | processor.ProcessErrorAsync += processErrorHandler; |
| | 136 | |
|
| | 137 | | try |
| | 138 | | { |
| | 139 | | // In order to begin processing, an explicit call must be made to the processor. This will instruct the |
| | 140 | | // processing in the background, invoking your handlers when they are needed. |
| | 141 | |
|
| 0 | 142 | | await processor.StartProcessingAsync(); |
| | 143 | |
|
| | 144 | | // Because processing takes place in the background, we'll wait for a small period of time and then trig |
| | 145 | | // cancellation. |
| | 146 | |
|
| 0 | 147 | | using var cancellationSource = new CancellationTokenSource(); |
| 0 | 148 | | cancellationSource.CancelAfter(TimeSpan.FromSeconds(30)); |
| | 149 | |
|
| 0 | 150 | | while (!cancellationSource.IsCancellationRequested) |
| | 151 | | { |
| 0 | 152 | | await Task.Delay(TimeSpan.FromMilliseconds(250)); |
| | 153 | | } |
| | 154 | |
|
| | 155 | | // Once we arrive at this point, either cancellation was requested or we have processed all of our event |
| | 156 | | // both cases, we'll want to shut down the processor. |
| | 157 | |
|
| 0 | 158 | | await processor.StopProcessingAsync(); |
| 0 | 159 | | } |
| | 160 | | finally |
| | 161 | | { |
| | 162 | | // It is encouraged that you unregister your handlers when you have finished |
| | 163 | | // using the Event Processor to ensure proper cleanup. This is especially |
| | 164 | | // important when using lambda expressions or handlers in any form that may |
| | 165 | | // contain closure scopes or hold other references. |
| | 166 | |
|
| 0 | 167 | | processor.PartitionInitializingAsync -= partitionInitializingHandler; |
| 0 | 168 | | processor.ProcessEventAsync -= processEventHandler; |
| 0 | 169 | | processor.ProcessErrorAsync -= processErrorHandler; |
| | 170 | | } |
| | 171 | |
|
| | 172 | | // The Event Processor client has been stopped and is not explicitly disposable; there |
| | 173 | | // is nothing further that we need to do for cleanup. |
| | 174 | |
|
| 0 | 175 | | Console.WriteLine(); |
| 0 | 176 | | } |
| | 177 | | } |
| | 178 | | } |