| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Net; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using Azure.Messaging.EventHubs.Consumer; |
| | | 8 | | using Azure.Messaging.EventHubs.Processor.Samples.Infrastructure; |
| | | 9 | | using Azure.Storage.Blobs; |
| | | 10 | | |
| | | 11 | | namespace Azure.Messaging.EventHubs.Processor.Samples |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// An introduction to the Event Processor client, exploring additional options for creating the |
| | | 15 | | /// processor. |
| | | 16 | | /// </summary> |
| | | 17 | | /// |
| | | 18 | | public class Sample02_ProcessorWithCustomOptions : IEventHubsBlobCheckpointSample |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// The name of the sample. |
| | | 22 | | /// </summary> |
| | | 23 | | /// |
| | 0 | 24 | | public string Name => nameof(Sample02_ProcessorWithCustomOptions); |
| | | 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, exploring additional options for cr |
| | | 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 | | // The Event Processor client offers additional options on creation, allowing you to control different aspec |
| | | 47 | | // should your scenario have needs that differ from the common defaults. If you choose not to provide these |
| | | 48 | | // suitable to most scenarios are used. |
| | | 49 | | // |
| | | 50 | | // The processor allows you to customize how it interacts with the Event Hubs service, such as by influencin |
| | | 51 | | // to the service by specifying the transport that communication should use and whether a proxy should be us |
| | | 52 | | // note that a proxy is only supported when using WebSockets as a transport; it isn't compatible with raw TC |
| | | 53 | | // |
| | | 54 | | // This sample will customize the transport for the connection, using WebSockets and will adjust some of the |
| | | 55 | | // illustration. |
| | | 56 | | |
| | 0 | 57 | | string consumerGroup = EventHubConsumerClient.DefaultConsumerGroupName; |
| | 0 | 58 | | BlobContainerClient storageClient = new BlobContainerClient(blobStorageConnectionString, blobContainerName); |
| | | 59 | | |
| | 0 | 60 | | var processorOptions = new EventProcessorClientOptions |
| | 0 | 61 | | { |
| | 0 | 62 | | ConnectionOptions = new EventHubConnectionOptions |
| | 0 | 63 | | { |
| | 0 | 64 | | TransportType = EventHubsTransportType.AmqpWebSockets, |
| | 0 | 65 | | Proxy = (IWebProxy)null |
| | 0 | 66 | | }, |
| | 0 | 67 | | |
| | 0 | 68 | | RetryOptions = new EventHubsRetryOptions |
| | 0 | 69 | | { |
| | 0 | 70 | | MaximumRetries = 5, |
| | 0 | 71 | | TryTimeout = TimeSpan.FromMinutes(1) |
| | 0 | 72 | | } |
| | 0 | 73 | | }; |
| | | 74 | | |
| | 0 | 75 | | EventProcessorClient processor = new EventProcessorClient(storageClient, consumerGroup, eventHubsConnectionS |
| | | 76 | | |
| | | 77 | | // Once the client has been created, it may be used normally. For completeness, we'll |
| | | 78 | | // mirror the minimal skeleton from our first sample, starting and stopping the processor |
| | | 79 | | // without performing any processing. |
| | | 80 | | |
| | 0 | 81 | | Task processEventHandler(ProcessEventArgs eventArgs) => Task.CompletedTask; |
| | 0 | 82 | | Task processErrorHandler(ProcessErrorEventArgs eventArgs) => Task.CompletedTask; |
| | | 83 | | |
| | 0 | 84 | | processor.ProcessEventAsync += processEventHandler; |
| | 0 | 85 | | processor.ProcessErrorAsync += processErrorHandler; |
| | | 86 | | |
| | | 87 | | try |
| | | 88 | | { |
| | 0 | 89 | | await processor.StartProcessingAsync(); |
| | 0 | 90 | | await Task.Delay(TimeSpan.FromSeconds(1)); |
| | 0 | 91 | | await processor.StopProcessingAsync(); |
| | 0 | 92 | | } |
| | | 93 | | finally |
| | | 94 | | { |
| | | 95 | | // It is encouraged that you unregister your handlers when you have finished |
| | | 96 | | // using the Event Processor to ensure proper cleanup. This is especially |
| | | 97 | | // important when using lambda expressions or handlers in any form that may |
| | | 98 | | // contain closure scopes or hold other references. |
| | | 99 | | |
| | 0 | 100 | | processor.ProcessEventAsync -= processEventHandler; |
| | 0 | 101 | | processor.ProcessErrorAsync -= processErrorHandler; |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | // The Event Processor client has been stopped and is not explicitly disposable; there |
| | | 105 | | // is nothing further that we need to do for cleanup. |
| | | 106 | | |
| | 0 | 107 | | Console.WriteLine(); |
| | 0 | 108 | | } |
| | | 109 | | } |
| | | 110 | | } |