| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Text; |
| | 7 | | using System.Threading; |
| | 8 | | using System.Threading.Tasks; |
| | 9 | | using Azure.Messaging.EventHubs.Consumer; |
| | 10 | | using Azure.Messaging.EventHubs.Processor.Samples.Infrastructure; |
| | 11 | | using Azure.Messaging.EventHubs.Producer; |
| | 12 | | using Azure.Storage.Blobs; |
| | 13 | |
|
| | 14 | | namespace Azure.Messaging.EventHubs.Processor.Samples |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// An example of ensuring that the handler for processing events is invoked on a fixed interval when no events ar |
| | 18 | | /// </summary> |
| | 19 | | /// |
| | 20 | | public class Sample08_EventProcessingHeartbeat : IEventHubsBlobCheckpointSample |
| | 21 | | { |
| | 22 | | /// <summary> |
| | 23 | | /// The name of the sample. |
| | 24 | | /// </summary> |
| | 25 | | /// |
| 0 | 26 | | public string Name => nameof(Sample08_EventProcessingHeartbeat); |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// A short description of the sample. |
| | 30 | | /// </summary> |
| | 31 | | /// |
| 0 | 32 | | public string Description => "An example of ensuring that the handler for processing events is invoked on a fixe |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// Runs the sample using the specified Event Hubs and Azure storage connection information. |
| | 36 | | /// </summary> |
| | 37 | | /// |
| | 38 | | /// <param name="eventHubsConnectionString">The connection string for the Event Hubs namespace that the sample s |
| | 39 | | /// <param name="eventHubName">The name of the Event Hub, sometimes known as its path, that the sample should ru |
| | 40 | | /// <param name="blobStorageConnectionString">The connection string for the storage account where checkpoints an |
| | 41 | | /// <param name="blobContainerName">The name of the blob storage container where checkpoints and state should be |
| | 42 | | /// |
| | 43 | | public async Task RunAsync(string eventHubsConnectionString, |
| | 44 | | string eventHubName, |
| | 45 | | string blobStorageConnectionString, |
| | 46 | | string blobContainerName) |
| | 47 | | { |
| | 48 | | // In this example, our Event Processor client will be configured to use a maximum wait time which is consid |
| | 49 | | // When events are available, the Event Processor client will pass them to the ProcessEvent handler to be pr |
| | 50 | | // partition for longer than the specified maximum wait interval, the processor will invoke the ProcessEvent |
| | 51 | | // include an event. This allows your handler code to receive control and perform actions such as sending a |
| | 52 | | // operation specific to your application needs. |
| | 53 | | // |
| | 54 | | // For our processor, we'll specify a small maximum wait time value as part of the options. |
| | 55 | |
|
| 0 | 56 | | EventProcessorClientOptions clientOptions = new EventProcessorClientOptions |
| 0 | 57 | | { |
| 0 | 58 | | MaximumWaitTime = TimeSpan.FromMilliseconds(150) |
| 0 | 59 | | }; |
| | 60 | |
|
| 0 | 61 | | string consumerGroup = EventHubConsumerClient.DefaultConsumerGroupName; |
| 0 | 62 | | BlobContainerClient storageClient = new BlobContainerClient(blobStorageConnectionString, blobContainerName); |
| 0 | 63 | | EventProcessorClient processor = new EventProcessorClient(storageClient, consumerGroup, eventHubsConnectionS |
| | 64 | |
|
| | 65 | | // For this example, we'll create a simple event handler that writes to the |
| | 66 | | // console each time it was invoked. |
| | 67 | |
|
| 0 | 68 | | int eventIndex = 0; |
| | 69 | |
|
| | 70 | | async Task processEventHandler(ProcessEventArgs eventArgs) |
| | 71 | | { |
| 0 | 72 | | if (eventArgs.CancellationToken.IsCancellationRequested) |
| | 73 | | { |
| 0 | 74 | | return; |
| | 75 | | } |
| | 76 | |
|
| | 77 | | try |
| | 78 | | { |
| | 79 | | // The "HasEvent" property of the arguments will be set if an event was available from the |
| | 80 | | // Event Hubs service. If so, the argument properties for the event is populated and checkpoints |
| | 81 | | // may be created. |
| | 82 | | // |
| | 83 | | // If the "HasEvent" property is unset, the event will be empty and checkpoints may not be created. |
| | 84 | |
|
| 0 | 85 | | if (eventArgs.HasEvent) |
| | 86 | | { |
| 0 | 87 | | Console.WriteLine($"Event Received: { Encoding.UTF8.GetString(eventArgs.Data.Body.ToArray()) }") |
| | 88 | | } |
| | 89 | |
|
| | 90 | | // Simulate sending a heartbeat using a simple helper that writes a status to the |
| | 91 | | // console. |
| | 92 | |
|
| 0 | 93 | | await SendHeartbeatAsync(); |
| 0 | 94 | | ++eventIndex; |
| 0 | 95 | | } |
| 0 | 96 | | catch (Exception ex) |
| | 97 | | { |
| 0 | 98 | | Console.WriteLine(); |
| 0 | 99 | | Console.WriteLine($"An error was observed while processing events. Message: { ex.Message }"); |
| 0 | 100 | | Console.WriteLine(); |
| 0 | 101 | | } |
| 0 | 102 | | }; |
| | 103 | |
|
| | 104 | | // For this example, exceptions will just be logged to the console. |
| | 105 | |
|
| | 106 | | Task processErrorHandler(ProcessErrorEventArgs eventArgs) |
| | 107 | | { |
| 0 | 108 | | if (eventArgs.CancellationToken.IsCancellationRequested) |
| | 109 | | { |
| 0 | 110 | | return Task.CompletedTask; |
| | 111 | | } |
| | 112 | |
|
| 0 | 113 | | Console.WriteLine(); |
| 0 | 114 | | Console.WriteLine("==============================="); |
| 0 | 115 | | Console.WriteLine($"The error handler was invoked during the operation: { eventArgs.Operation ?? "Unknow |
| 0 | 116 | | Console.WriteLine("==============================="); |
| 0 | 117 | | Console.WriteLine(); |
| | 118 | |
|
| 0 | 119 | | return Task.CompletedTask; |
| | 120 | | } |
| | 121 | |
|
| 0 | 122 | | processor.ProcessEventAsync += processEventHandler; |
| 0 | 123 | | processor.ProcessErrorAsync += processErrorHandler; |
| | 124 | |
|
| | 125 | | try |
| | 126 | | { |
| 0 | 127 | | Console.WriteLine("Starting the Event Processor client..."); |
| 0 | 128 | | Console.WriteLine(); |
| | 129 | |
|
| 0 | 130 | | eventIndex = 0; |
| 0 | 131 | | await processor.StartProcessingAsync(); |
| | 132 | |
|
| 0 | 133 | | using var cancellationSource = new CancellationTokenSource(); |
| 0 | 134 | | cancellationSource.CancelAfter(TimeSpan.FromSeconds(90)); |
| | 135 | |
|
| | 136 | | // We'll publish a batch of events for our processor to receive. We'll split the events into a couple of |
| | 137 | | // increase the chance they'll be spread around to different partitions and introduce a delay between ba |
| | 138 | | // allow for the handler to be invoked without an available event interleaved. |
| | 139 | |
|
| 0 | 140 | | var expectedEvents = new List<EventData>() |
| 0 | 141 | | { |
| 0 | 142 | | new EventData(Encoding.UTF8.GetBytes("First Event, First Batch")), |
| 0 | 143 | | new EventData(Encoding.UTF8.GetBytes("Second Event, First Batch")), |
| 0 | 144 | | new EventData(Encoding.UTF8.GetBytes("Third Event, First Batch")), |
| 0 | 145 | |
|
| 0 | 146 | | new EventData(Encoding.UTF8.GetBytes("First Event, Second Batch")), |
| 0 | 147 | | new EventData(Encoding.UTF8.GetBytes("Second Event, Second Batch")), |
| 0 | 148 | | new EventData(Encoding.UTF8.GetBytes("Third Event, Second Batch")), |
| 0 | 149 | |
|
| 0 | 150 | | new EventData(Encoding.UTF8.GetBytes("First Event, Third Batch")), |
| 0 | 151 | | new EventData(Encoding.UTF8.GetBytes("Second Event, Third Batch")), |
| 0 | 152 | | new EventData(Encoding.UTF8.GetBytes("Third Event, Third Batch")) |
| 0 | 153 | | }; |
| | 154 | |
|
| 0 | 155 | | int sentIndex = 0; |
| 0 | 156 | | int numberOfBatches = 3; |
| 0 | 157 | | int eventsPerBatch = (expectedEvents.Count / numberOfBatches); |
| | 158 | |
|
| 0 | 159 | | await using (var producer = new EventHubProducerClient(eventHubsConnectionString, eventHubName)) |
| | 160 | | { |
| 0 | 161 | | while (sentIndex < expectedEvents.Count) |
| | 162 | | { |
| 0 | 163 | | using EventDataBatch eventBatch = await producer.CreateBatchAsync(); |
| | 164 | |
|
| 0 | 165 | | for (int index = 0; index < eventsPerBatch; ++index) |
| | 166 | | { |
| 0 | 167 | | eventBatch.TryAdd(expectedEvents[sentIndex]); |
| 0 | 168 | | ++sentIndex; |
| | 169 | | } |
| | 170 | |
|
| 0 | 171 | | await producer.SendAsync(eventBatch); |
| 0 | 172 | | await Task.Delay(250, cancellationSource.Token); |
| 0 | 173 | | } |
| | 174 | | } |
| | 175 | |
|
| | 176 | | // We'll allow the Event Processor client to read and dispatch the events that we published, along with |
| | 177 | | // ensuring a few invocations with no event. Note that, due to non-determinism in the timing, we may or |
| | 178 | | // not see all of the events from our batches read. |
| | 179 | |
|
| 0 | 180 | | while ((!cancellationSource.IsCancellationRequested) && (eventIndex <= expectedEvents.Count + 5)) |
| | 181 | | { |
| 0 | 182 | | await Task.Delay(TimeSpan.FromMilliseconds(250)); |
| | 183 | | } |
| | 184 | |
|
| | 185 | | // Once we arrive at this point, either cancellation was requested or we have processed all of our event |
| | 186 | | // both cases, we'll want to shut down the processor. |
| | 187 | |
|
| 0 | 188 | | Console.WriteLine(); |
| 0 | 189 | | Console.WriteLine("Stopping the processor..."); |
| | 190 | |
|
| 0 | 191 | | await processor.StopProcessingAsync(); |
| 0 | 192 | | } |
| | 193 | | finally |
| | 194 | | { |
| | 195 | | // It is encouraged that you unregister your handlers when you have finished |
| | 196 | | // using the Event Processor to ensure proper cleanup. This is especially |
| | 197 | | // important when using lambda expressions or handlers in any form that may |
| | 198 | | // contain closure scopes or hold other references. |
| | 199 | |
|
| 0 | 200 | | processor.ProcessEventAsync -= processEventHandler; |
| 0 | 201 | | processor.ProcessErrorAsync -= processErrorHandler; |
| | 202 | | } |
| | 203 | |
|
| | 204 | | // The Event Processor client has been stopped and is not explicitly disposable; there |
| | 205 | | // is nothing further that we need to do for cleanup. |
| | 206 | |
|
| 0 | 207 | | Console.WriteLine(); |
| 0 | 208 | | } |
| | 209 | |
|
| | 210 | | /// <summary> |
| | 211 | | /// A helper method to simulate sending a heartbeat for health monitoring |
| | 212 | | /// during event processing. |
| | 213 | | /// </summary> |
| | 214 | | /// |
| | 215 | | private async Task SendHeartbeatAsync() |
| | 216 | | { |
| 0 | 217 | | Console.WriteLine("Sending heartbeat (simulated)..."); |
| 0 | 218 | | await Task.Delay(50); |
| 0 | 219 | | } |
| | 220 | | } |
| | 221 | | } |