| | | 1 | | // Copyright (c) Microsoft. All rights reserved. |
| | | 2 | | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| | | 3 | | |
| | | 4 | | namespace Microsoft.Azure.EventHubs.Processor |
| | | 5 | | { |
| | | 6 | | using System; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using Microsoft.Azure.EventHubs.Primitives; |
| | | 9 | | using Microsoft.Azure.Storage; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Represents a host for processing Event Hubs event data. |
| | | 13 | | /// </summary> |
| | | 14 | | public sealed class EventProcessorHost |
| | | 15 | | { |
| | | 16 | | // A processor host will work on either the token provider or the connection string. |
| | | 17 | | readonly ITokenProvider tokenProvider; |
| | | 18 | | string eventHubConnectionString; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Create a new host to process events from an Event Hub. |
| | | 22 | | /// |
| | | 23 | | /// <para>Since Event Hubs are frequently used for scale-out, high-traffic scenarios, generally there will |
| | | 24 | | /// be only one host per process, and the processes will be run on separate machines. However, it is |
| | | 25 | | /// supported to run multiple hosts on one machine, or even within one process, if throughput is not |
| | | 26 | | /// a concern.</para> |
| | | 27 | | /// |
| | | 28 | | /// This overload of the constructor uses the default, built-in lease and checkpoint managers. The |
| | | 29 | | /// Azure Storage account specified by the storageConnectionString parameter is used by the built-in |
| | | 30 | | /// managers to record leases and checkpoints. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="eventHubPath">The name of the EventHub.</param> |
| | | 33 | | /// <param name="consumerGroupName">The name of the consumer group within the Event Hub.</param> |
| | | 34 | | /// <param name="eventHubConnectionString">Connection string for the Event Hub to receive from.</param> |
| | | 35 | | /// <param name="storageConnectionString">Connection string to Azure Storage account used for leases and checkpo |
| | | 36 | | /// <param name="leaseContainerName">Azure Storage container name for use by built-in lease and checkpoint manag |
| | | 37 | | public EventProcessorHost( |
| | | 38 | | string eventHubPath, |
| | | 39 | | string consumerGroupName, |
| | | 40 | | string eventHubConnectionString, |
| | | 41 | | string storageConnectionString, |
| | | 42 | | string leaseContainerName) |
| | 0 | 43 | | : this(EventProcessorHost.CreateHostName(null), |
| | 0 | 44 | | eventHubPath, |
| | 0 | 45 | | consumerGroupName, |
| | 0 | 46 | | eventHubConnectionString, |
| | 0 | 47 | | storageConnectionString, |
| | 0 | 48 | | leaseContainerName, |
| | 0 | 49 | | null) |
| | | 50 | | { |
| | 0 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Create a new host to process events from an Event Hub. |
| | | 55 | | /// |
| | | 56 | | /// <para>This overload of the constructor uses the default, built-in lease and checkpoint managers.</para> |
| | | 57 | | /// </summary> |
| | | 58 | | /// <param name="hostName">Name of the processor host. MUST BE UNIQUE. Strongly recommend including a Guid to en |
| | | 59 | | /// <param name="eventHubPath">The name of the EventHub.</param> |
| | | 60 | | /// <param name="consumerGroupName">The name of the consumer group within the Event Hub.</param> |
| | | 61 | | /// <param name="eventHubConnectionString">Connection string for the Event Hub to receive from.</param> |
| | | 62 | | /// <param name="storageConnectionString">Connection string to Azure Storage account used for leases and checkpo |
| | | 63 | | /// <param name="leaseContainerName">Azure Storage container name for use by built-in lease and checkpoint manag |
| | | 64 | | /// <param name="storageBlobPrefix">Prefix used when naming blobs within the storage container.</param> |
| | | 65 | | public EventProcessorHost( |
| | | 66 | | string hostName, |
| | | 67 | | string eventHubPath, |
| | | 68 | | string consumerGroupName, |
| | | 69 | | string eventHubConnectionString, |
| | | 70 | | string storageConnectionString, |
| | | 71 | | string leaseContainerName, |
| | | 72 | | string storageBlobPrefix = null) |
| | 0 | 73 | | : this(hostName, |
| | 0 | 74 | | eventHubPath, |
| | 0 | 75 | | consumerGroupName, |
| | 0 | 76 | | eventHubConnectionString, |
| | 0 | 77 | | new AzureStorageCheckpointLeaseManager(storageConnectionString, leaseContainerName, storageBlobPrefix)) |
| | | 78 | | { |
| | 0 | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Create a new host to process events from an Event Hub. |
| | | 83 | | /// |
| | | 84 | | /// <para>This overload of the constructor allows maximum flexibility. |
| | | 85 | | /// This one allows the caller to specify the name of the processor host as well. |
| | | 86 | | /// The overload also allows the caller to provide their own lease and checkpoint managers to replace the built- |
| | | 87 | | /// ones based on Azure Storage.</para> |
| | | 88 | | /// </summary> |
| | | 89 | | /// <param name="hostName">Name of the processor host. MUST BE UNIQUE. Strongly recommend including a Guid to en |
| | | 90 | | /// <param name="eventHubPath">The name of the EventHub.</param> |
| | | 91 | | /// <param name="consumerGroupName">The name of the consumer group within the Event Hub.</param> |
| | | 92 | | /// <param name="eventHubConnectionString">Connection string for the Event Hub to receive from.</param> |
| | | 93 | | /// <param name="checkpointManager">Object implementing ICheckpointManager which handles partition checkpointing |
| | | 94 | | /// <param name="leaseManager">Object implementing ILeaseManager which handles leases for partitions.</param> |
| | 0 | 95 | | public EventProcessorHost( |
| | 0 | 96 | | string hostName, |
| | 0 | 97 | | string eventHubPath, |
| | 0 | 98 | | string consumerGroupName, |
| | 0 | 99 | | string eventHubConnectionString, |
| | 0 | 100 | | ICheckpointManager checkpointManager, |
| | 0 | 101 | | ILeaseManager leaseManager) |
| | | 102 | | { |
| | 0 | 103 | | Guard.ArgumentNotNullOrWhiteSpace(nameof(hostName), hostName); |
| | 0 | 104 | | Guard.ArgumentNotNullOrWhiteSpace(nameof(consumerGroupName), consumerGroupName); |
| | 0 | 105 | | Guard.ArgumentNotNull(nameof(checkpointManager), checkpointManager); |
| | 0 | 106 | | Guard.ArgumentNotNull(nameof(leaseManager), leaseManager); |
| | | 107 | | |
| | 0 | 108 | | var csb = new EventHubsConnectionStringBuilder(eventHubConnectionString); |
| | 0 | 109 | | if (string.IsNullOrEmpty(eventHubPath)) |
| | | 110 | | { |
| | | 111 | | // Entity path is expected in the connection string if not provided with eventHubPath parameter. |
| | 0 | 112 | | if (string.IsNullOrEmpty(csb.EntityPath)) |
| | | 113 | | { |
| | 0 | 114 | | throw new ArgumentException(nameof(eventHubConnectionString), |
| | 0 | 115 | | "Provide EventHub entity path either in eventHubPath parameter or in eventHubConnectionString.") |
| | | 116 | | } |
| | | 117 | | } |
| | | 118 | | else |
| | | 119 | | { |
| | | 120 | | // Entity path should not conflict with connection string. |
| | 0 | 121 | | if (!string.IsNullOrEmpty(csb.EntityPath) && |
| | 0 | 122 | | string.Compare(csb.EntityPath, eventHubPath, StringComparison.OrdinalIgnoreCase) != 0) |
| | | 123 | | { |
| | 0 | 124 | | throw new ArgumentException(nameof(eventHubConnectionString), |
| | 0 | 125 | | "Provided EventHub path in eventHubPath parameter conflicts with the path in provided EventHubs |
| | | 126 | | } |
| | | 127 | | |
| | 0 | 128 | | csb.EntityPath = eventHubPath; |
| | | 129 | | } |
| | | 130 | | |
| | 0 | 131 | | this.HostName = hostName; |
| | 0 | 132 | | this.EventHubPath = csb.EntityPath; |
| | 0 | 133 | | this.ConsumerGroupName = consumerGroupName; |
| | 0 | 134 | | this.eventHubConnectionString = csb.ToString(); |
| | 0 | 135 | | this.CheckpointManager = checkpointManager; |
| | 0 | 136 | | this.LeaseManager = leaseManager; |
| | 0 | 137 | | this.TransportType = csb.TransportType; |
| | 0 | 138 | | this.OperationTimeout = csb.OperationTimeout; |
| | 0 | 139 | | this.EndpointAddress = csb.Endpoint; |
| | 0 | 140 | | this.PartitionManager = new PartitionManager(this); |
| | 0 | 141 | | ProcessorEventSource.Log.EventProcessorHostCreated(this.HostName, this.EventHubPath); |
| | 0 | 142 | | } |
| | | 143 | | |
| | | 144 | | /// <summary> |
| | | 145 | | /// Create a new host to process events from an Event Hub with provided <see cref="TokenProvider"/> |
| | | 146 | | /// </summary> |
| | | 147 | | /// <param name="endpointAddress">Fully qualified domain name for Event Hubs. Most likely, {yournamespace}.servi |
| | | 148 | | /// <param name="eventHubPath">The name of the EventHub.</param> |
| | | 149 | | /// <param name="consumerGroupName">The name of the consumer group within the Event Hub.</param> |
| | | 150 | | /// <param name="tokenProvider">Token provider which will generate security tokens for authorization.</param> |
| | | 151 | | /// <param name="cloudStorageAccount">Azure Storage account used for leases and checkpointing.</param> |
| | | 152 | | /// <param name="leaseContainerName">Azure Storage container name for use by built-in lease and checkpoint manag |
| | | 153 | | public EventProcessorHost( |
| | | 154 | | Uri endpointAddress, |
| | | 155 | | string eventHubPath, |
| | | 156 | | string consumerGroupName, |
| | | 157 | | ITokenProvider tokenProvider, |
| | | 158 | | CloudStorageAccount cloudStorageAccount, |
| | | 159 | | string leaseContainerName) |
| | 0 | 160 | | : this(EventProcessorHost.CreateHostName(null), |
| | 0 | 161 | | endpointAddress, |
| | 0 | 162 | | eventHubPath, |
| | 0 | 163 | | consumerGroupName, |
| | 0 | 164 | | tokenProvider, |
| | 0 | 165 | | cloudStorageAccount, |
| | 0 | 166 | | leaseContainerName) |
| | | 167 | | { |
| | 0 | 168 | | } |
| | | 169 | | |
| | | 170 | | /// <summary> |
| | | 171 | | /// Create a new host to process events from an Event Hub with provided <see cref="TokenProvider"/> |
| | | 172 | | /// </summary> |
| | | 173 | | /// <param name="hostName">Name of the processor host. MUST BE UNIQUE. Strongly recommend including a Guid to en |
| | | 174 | | /// <param name="endpointAddress">Fully qualified domain name for Event Hubs. Most likely, {yournamespace}.servi |
| | | 175 | | /// <param name="eventHubPath">The name of the EventHub.</param> |
| | | 176 | | /// <param name="consumerGroupName">The name of the consumer group within the Event Hub.</param> |
| | | 177 | | /// <param name="tokenProvider">Token provider which will generate security tokens for authorization.</param> |
| | | 178 | | /// <param name="cloudStorageAccount">Azure Storage account used for leases and checkpointing.</param> |
| | | 179 | | /// <param name="leaseContainerName">Azure Storage container name for use by built-in lease and checkpoint manag |
| | | 180 | | /// <param name="storageBlobPrefix">Prefix used when naming blobs within the storage container.</param> |
| | | 181 | | /// <param name="operationTimeout">Operation timeout for Event Hubs operations.</param> |
| | | 182 | | /// <param name="transportType">Transport type on connection.</param> |
| | | 183 | | public EventProcessorHost( |
| | | 184 | | string hostName, |
| | | 185 | | Uri endpointAddress, |
| | | 186 | | string eventHubPath, |
| | | 187 | | string consumerGroupName, |
| | | 188 | | ITokenProvider tokenProvider, |
| | | 189 | | CloudStorageAccount cloudStorageAccount, |
| | | 190 | | string leaseContainerName, |
| | | 191 | | string storageBlobPrefix = null, |
| | | 192 | | TimeSpan? operationTimeout = null, |
| | | 193 | | TransportType transportType = TransportType.Amqp) |
| | 0 | 194 | | : this(hostName, |
| | 0 | 195 | | endpointAddress, |
| | 0 | 196 | | eventHubPath, |
| | 0 | 197 | | consumerGroupName, |
| | 0 | 198 | | tokenProvider, |
| | 0 | 199 | | new AzureStorageCheckpointLeaseManager(cloudStorageAccount, leaseContainerName, storageBlobPrefix), |
| | 0 | 200 | | operationTimeout, |
| | 0 | 201 | | transportType) |
| | | 202 | | { |
| | 0 | 203 | | } |
| | | 204 | | |
| | | 205 | | /// <summary> |
| | | 206 | | /// Create a new host to process events from an Event Hub with provided <see cref="TokenProvider"/> |
| | | 207 | | /// </summary> |
| | | 208 | | /// <param name="endpointAddress">Fully qualified domain name for Event Hubs. Most likely, {yournamespace}.servi |
| | | 209 | | /// <param name="eventHubPath">The name of the EventHub.</param> |
| | | 210 | | /// <param name="consumerGroupName">The name of the consumer group within the Event Hub.</param> |
| | | 211 | | /// <param name="tokenProvider">Token provider which will generate security tokens for authorization.</param> |
| | | 212 | | /// <param name="cloudStorageAccount">Azure Storage account used for leases and checkpointing.</param> |
| | | 213 | | /// <param name="leaseContainerName">Azure Storage container name for use by built-in lease and checkpoint manag |
| | | 214 | | /// <param name="storageBlobPrefix">Prefix used when naming blobs within the storage container.</param> |
| | | 215 | | /// <param name="operationTimeout">Operation timeout for Event Hubs operations.</param> |
| | | 216 | | /// <param name="transportType">Transport type on connection.</param> |
| | | 217 | | public EventProcessorHost( |
| | | 218 | | Uri endpointAddress, |
| | | 219 | | string eventHubPath, |
| | | 220 | | string consumerGroupName, |
| | | 221 | | ITokenProvider tokenProvider, |
| | | 222 | | CloudStorageAccount cloudStorageAccount, |
| | | 223 | | string leaseContainerName, |
| | | 224 | | string storageBlobPrefix = null, |
| | | 225 | | TimeSpan? operationTimeout = null, |
| | | 226 | | TransportType transportType = TransportType.Amqp) |
| | 0 | 227 | | : this(EventProcessorHost.CreateHostName(null), |
| | 0 | 228 | | endpointAddress, |
| | 0 | 229 | | eventHubPath, |
| | 0 | 230 | | consumerGroupName, |
| | 0 | 231 | | tokenProvider, |
| | 0 | 232 | | cloudStorageAccount, |
| | 0 | 233 | | leaseContainerName, |
| | 0 | 234 | | storageBlobPrefix, |
| | 0 | 235 | | operationTimeout, |
| | 0 | 236 | | transportType) |
| | | 237 | | { |
| | 0 | 238 | | } |
| | | 239 | | |
| | | 240 | | /// <summary> |
| | | 241 | | /// Create a new host to process events from an Event Hub with provided <see cref="TokenProvider"/> |
| | | 242 | | /// </summary> |
| | | 243 | | /// <param name="hostName">Name of the processor host. MUST BE UNIQUE. Strongly recommend including a Guid to en |
| | | 244 | | /// <param name="endpointAddress">Fully qualified domain name for Event Hubs. Most likely, {yournamespace}.servi |
| | | 245 | | /// <param name="eventHubPath">The name of the EventHub.</param> |
| | | 246 | | /// <param name="consumerGroupName">The name of the consumer group within the Event Hub.</param> |
| | | 247 | | /// <param name="tokenProvider">Token provider which will generate security tokens for authorization.</param> |
| | | 248 | | /// <param name="checkpointManager">Object implementing ICheckpointManager which handles partition checkpointing |
| | | 249 | | /// <param name="leaseManager">Object implementing ILeaseManager which handles leases for partitions.</param> |
| | | 250 | | /// <param name="operationTimeout">Operation timeout for Event Hubs operations.</param> |
| | | 251 | | /// <param name="transportType">Transport type on connection.</param> |
| | 0 | 252 | | public EventProcessorHost( |
| | 0 | 253 | | string hostName, |
| | 0 | 254 | | Uri endpointAddress, |
| | 0 | 255 | | string eventHubPath, |
| | 0 | 256 | | string consumerGroupName, |
| | 0 | 257 | | ITokenProvider tokenProvider, |
| | 0 | 258 | | ICheckpointManager checkpointManager, |
| | 0 | 259 | | ILeaseManager leaseManager, |
| | 0 | 260 | | TimeSpan? operationTimeout = null, |
| | 0 | 261 | | TransportType transportType = TransportType.Amqp) |
| | | 262 | | { |
| | 0 | 263 | | Guard.ArgumentNotNullOrWhiteSpace(nameof(hostName), hostName); |
| | 0 | 264 | | Guard.ArgumentNotNull(nameof(endpointAddress), endpointAddress); |
| | 0 | 265 | | Guard.ArgumentNotNullOrWhiteSpace(nameof(eventHubPath), eventHubPath); |
| | 0 | 266 | | Guard.ArgumentNotNullOrWhiteSpace(nameof(consumerGroupName), consumerGroupName); |
| | 0 | 267 | | Guard.ArgumentNotNull(nameof(tokenProvider), tokenProvider); |
| | 0 | 268 | | Guard.ArgumentNotNull(nameof(checkpointManager), checkpointManager); |
| | 0 | 269 | | Guard.ArgumentNotNull(nameof(leaseManager), leaseManager); |
| | | 270 | | |
| | 0 | 271 | | this.HostName = hostName; |
| | 0 | 272 | | this.EndpointAddress = endpointAddress; |
| | 0 | 273 | | this.EventHubPath = eventHubPath; |
| | 0 | 274 | | this.ConsumerGroupName = consumerGroupName; |
| | 0 | 275 | | this.tokenProvider = tokenProvider; |
| | 0 | 276 | | this.CheckpointManager = checkpointManager; |
| | 0 | 277 | | this.LeaseManager = leaseManager; |
| | 0 | 278 | | this.TransportType = transportType; |
| | 0 | 279 | | this.OperationTimeout = operationTimeout ?? ClientConstants.DefaultOperationTimeout; |
| | 0 | 280 | | this.PartitionManager = new PartitionManager(this); |
| | 0 | 281 | | ProcessorEventSource.Log.EventProcessorHostCreated(this.HostName, this.EventHubPath); |
| | 0 | 282 | | } |
| | | 283 | | |
| | | 284 | | // Using this intermediate constructor to create single combined manager to be used as |
| | | 285 | | // both lease manager and checkpoint manager. |
| | | 286 | | EventProcessorHost( |
| | | 287 | | string hostName, |
| | | 288 | | string eventHubPath, |
| | | 289 | | string consumerGroupName, |
| | | 290 | | string eventHubConnectionString, |
| | | 291 | | AzureStorageCheckpointLeaseManager combinedManager) |
| | 0 | 292 | | : this(hostName, |
| | 0 | 293 | | eventHubPath, |
| | 0 | 294 | | consumerGroupName, |
| | 0 | 295 | | eventHubConnectionString, |
| | 0 | 296 | | combinedManager, |
| | 0 | 297 | | combinedManager) |
| | | 298 | | { |
| | 0 | 299 | | } |
| | | 300 | | |
| | | 301 | | // Using this intermediate constructor to create single combined manager to be used as |
| | | 302 | | // both lease manager and checkpoint manager. |
| | | 303 | | EventProcessorHost( |
| | | 304 | | string hostName, |
| | | 305 | | Uri endpointAddress, |
| | | 306 | | string eventHubPath, |
| | | 307 | | string consumerGroupName, |
| | | 308 | | ITokenProvider tokenProvider, |
| | | 309 | | AzureStorageCheckpointLeaseManager combinedManager, |
| | | 310 | | TimeSpan? operationTimeout = null, |
| | | 311 | | TransportType transportType = TransportType.Amqp) |
| | 0 | 312 | | : this(hostName, |
| | 0 | 313 | | endpointAddress, |
| | 0 | 314 | | eventHubPath, |
| | 0 | 315 | | consumerGroupName, |
| | 0 | 316 | | tokenProvider, |
| | 0 | 317 | | combinedManager, |
| | 0 | 318 | | combinedManager, |
| | 0 | 319 | | operationTimeout, |
| | 0 | 320 | | transportType) |
| | | 321 | | { |
| | 0 | 322 | | } |
| | | 323 | | |
| | | 324 | | /// <summary> |
| | | 325 | | /// Returns processor host name. |
| | | 326 | | /// If the processor host name was automatically generated, this is the only way to get it. |
| | | 327 | | /// </summary> |
| | 0 | 328 | | public string HostName { get; } |
| | | 329 | | |
| | | 330 | | /// <summary> |
| | | 331 | | /// Gets the event hub path. |
| | | 332 | | /// </summary> |
| | 0 | 333 | | public string EventHubPath { get; } |
| | | 334 | | |
| | | 335 | | /// <summary> |
| | | 336 | | /// Gets the consumer group name. |
| | | 337 | | /// </summary> |
| | 0 | 338 | | public string ConsumerGroupName { get; } |
| | | 339 | | |
| | | 340 | | /// <summary> |
| | | 341 | | /// Gets the event endpoint URI. |
| | | 342 | | /// </summary> |
| | 0 | 343 | | public Uri EndpointAddress { get; } |
| | | 344 | | |
| | | 345 | | /// <summary> |
| | | 346 | | /// Gets the transport type. |
| | | 347 | | /// </summary> |
| | 0 | 348 | | public TransportType TransportType { get; } |
| | | 349 | | |
| | | 350 | | /// <summary> |
| | | 351 | | /// Gets the operation timeout. |
| | | 352 | | /// </summary> |
| | 0 | 353 | | public TimeSpan OperationTimeout { get; internal set; } |
| | | 354 | | |
| | | 355 | | /// <summary>Gets or sets the |
| | | 356 | | /// <see cref="PartitionManagerOptions" /> instance used by the |
| | | 357 | | /// <see cref="EventProcessorHost" /> object.</summary> |
| | | 358 | | /// <value>The <see cref="PartitionManagerOptions" /> instance.</value> |
| | 0 | 359 | | public PartitionManagerOptions PartitionManagerOptions { get; set; } |
| | | 360 | | |
| | | 361 | | // All of these accessors are for internal use only. |
| | 0 | 362 | | internal ICheckpointManager CheckpointManager { get; } |
| | | 363 | | |
| | 0 | 364 | | internal EventProcessorOptions EventProcessorOptions { get; private set; } |
| | | 365 | | |
| | 0 | 366 | | internal ILeaseManager LeaseManager { get; private set; } |
| | | 367 | | |
| | 0 | 368 | | internal IEventProcessorFactory ProcessorFactory { get; private set; } |
| | | 369 | | |
| | 0 | 370 | | internal PartitionManager PartitionManager { get; private set; } |
| | | 371 | | |
| | | 372 | | /// <summary> |
| | | 373 | | /// This registers <see cref="IEventProcessor"/> implementation with the host using <see cref="DefaultEventProce |
| | | 374 | | /// This also starts the host and causes it to start participating in the partition distribution process. |
| | | 375 | | /// </summary> |
| | | 376 | | /// <typeparam name="T">Implementation of your application specific <see cref="IEventProcessor"/>.</typeparam> |
| | | 377 | | /// <returns>A task to indicate EventProcessorHost instance is started.</returns> |
| | | 378 | | public Task RegisterEventProcessorAsync<T>() where T : IEventProcessor, new() |
| | | 379 | | { |
| | 0 | 380 | | return RegisterEventProcessorAsync<T>(EventProcessorOptions.DefaultOptions); |
| | | 381 | | } |
| | | 382 | | |
| | | 383 | | /// <summary> |
| | | 384 | | /// This registers <see cref="IEventProcessor"/> implementation with the host using <see cref="DefaultEventProce |
| | | 385 | | /// This also starts the host and causes it to start participating in the partition distribution process. |
| | | 386 | | /// </summary> |
| | | 387 | | /// <typeparam name="T">Implementation of your application specific <see cref="IEventProcessor"/>.</typeparam> |
| | | 388 | | /// <param name="processorOptions"><see cref="EventProcessorOptions"/> to control various aspects of message pum |
| | | 389 | | /// is acquired for a particular partition of EventHub.</param> |
| | | 390 | | /// <returns>A task to indicate EventProcessorHost instance is started.</returns> |
| | | 391 | | public Task RegisterEventProcessorAsync<T>(EventProcessorOptions processorOptions) where T : IEventProcessor, ne |
| | | 392 | | { |
| | 0 | 393 | | IEventProcessorFactory f = new DefaultEventProcessorFactory<T>(); |
| | 0 | 394 | | return RegisterEventProcessorFactoryAsync(f, processorOptions); |
| | | 395 | | } |
| | | 396 | | |
| | | 397 | | /// <summary> |
| | | 398 | | /// This registers <see cref="IEventProcessorFactory"/> implementation with the host which is used to create an |
| | | 399 | | /// <see cref="IEventProcessor"/> when it takes ownership of a partition. This also starts the host and causes |
| | | 400 | | /// in the partition distribution process. |
| | | 401 | | /// </summary> |
| | | 402 | | /// <param name="factory">Instance of <see cref="IEventProcessorFactory"/> implementation.</param> |
| | | 403 | | /// <returns>A task to indicate EventProcessorHost instance is started.</returns> |
| | | 404 | | public Task RegisterEventProcessorFactoryAsync(IEventProcessorFactory factory) |
| | | 405 | | { |
| | 0 | 406 | | var epo = EventProcessorOptions.DefaultOptions; |
| | 0 | 407 | | epo.ReceiveTimeout = TimeSpan.MinValue; |
| | 0 | 408 | | return RegisterEventProcessorFactoryAsync(factory, epo); |
| | | 409 | | } |
| | | 410 | | |
| | | 411 | | /// <summary> |
| | | 412 | | /// This registers <see cref="IEventProcessorFactory"/> implementation with the host which is used to create an |
| | | 413 | | /// <see cref="IEventProcessor"/> when it takes ownership of a partition. This also starts the host and causes |
| | | 414 | | /// in the partition distribution process. |
| | | 415 | | /// </summary> |
| | | 416 | | /// <param name="factory">Instance of <see cref="IEventProcessorFactory"/> implementation.</param> |
| | | 417 | | /// <param name="processorOptions"><see cref="EventProcessorOptions"/> to control various aspects of message pum |
| | | 418 | | /// is acquired for a particular partition of EventHub.</param> |
| | | 419 | | /// <returns>A task to indicate EventProcessorHost instance is started.</returns> |
| | | 420 | | public async Task RegisterEventProcessorFactoryAsync(IEventProcessorFactory factory, EventProcessorOptions proce |
| | | 421 | | { |
| | 0 | 422 | | Guard.ArgumentNotNull(nameof(factory), factory); |
| | 0 | 423 | | Guard.ArgumentNotNull(nameof(processorOptions), processorOptions); |
| | | 424 | | |
| | | 425 | | // Initialize partition manager options with default values if not already set by the client. |
| | 0 | 426 | | if (this.PartitionManagerOptions == null) |
| | | 427 | | { |
| | | 428 | | // Assign partition manager with default options. |
| | 0 | 429 | | this.PartitionManagerOptions = new PartitionManagerOptions(); |
| | | 430 | | } |
| | | 431 | | |
| | 0 | 432 | | ProcessorEventSource.Log.EventProcessorHostOpenStart(this.HostName, factory.GetType().ToString()); |
| | | 433 | | |
| | | 434 | | try |
| | | 435 | | { |
| | | 436 | | // Override operation timeout by receive timeout? |
| | 0 | 437 | | if (processorOptions.ReceiveTimeout > TimeSpan.MinValue) |
| | | 438 | | { |
| | 0 | 439 | | this.OperationTimeout = processorOptions.ReceiveTimeout; |
| | | 440 | | |
| | 0 | 441 | | if (this.eventHubConnectionString != null) |
| | | 442 | | { |
| | 0 | 443 | | var cbs = new EventHubsConnectionStringBuilder(this.eventHubConnectionString) |
| | 0 | 444 | | { |
| | 0 | 445 | | OperationTimeout = processorOptions.ReceiveTimeout |
| | 0 | 446 | | }; |
| | 0 | 447 | | this.eventHubConnectionString = cbs.ToString(); |
| | | 448 | | } |
| | | 449 | | } |
| | | 450 | | |
| | | 451 | | // Initialize lease manager if this is an AzureStorageCheckpointLeaseManager |
| | 0 | 452 | | (this.LeaseManager as AzureStorageCheckpointLeaseManager)?.Initialize(this); |
| | | 453 | | |
| | 0 | 454 | | this.ProcessorFactory = factory; |
| | 0 | 455 | | this.EventProcessorOptions = processorOptions; |
| | 0 | 456 | | await this.PartitionManager.StartAsync().ConfigureAwait(false); |
| | 0 | 457 | | } |
| | 0 | 458 | | catch (Exception e) |
| | | 459 | | { |
| | 0 | 460 | | ProcessorEventSource.Log.EventProcessorHostOpenError(this.HostName, e.ToString()); |
| | 0 | 461 | | throw; |
| | | 462 | | } |
| | | 463 | | finally |
| | | 464 | | { |
| | 0 | 465 | | ProcessorEventSource.Log.EventProcessorHostOpenStop(this.HostName); |
| | | 466 | | } |
| | 0 | 467 | | } |
| | | 468 | | |
| | | 469 | | /// <summary> |
| | | 470 | | /// Stop processing events. Does not return until the shutdown is complete. |
| | | 471 | | /// </summary> |
| | | 472 | | /// <returns></returns> |
| | | 473 | | public async Task UnregisterEventProcessorAsync() // throws InterruptedException, ExecutionException |
| | | 474 | | { |
| | 0 | 475 | | ProcessorEventSource.Log.EventProcessorHostCloseStart(this.HostName); |
| | | 476 | | try |
| | | 477 | | { |
| | 0 | 478 | | await this.PartitionManager.StopAsync().ConfigureAwait(false); |
| | 0 | 479 | | } |
| | 0 | 480 | | catch (Exception e) |
| | | 481 | | { |
| | | 482 | | // Log the failure but nothing really to do about it. |
| | 0 | 483 | | ProcessorEventSource.Log.EventProcessorHostCloseError(this.HostName, e.ToString()); |
| | 0 | 484 | | throw; |
| | | 485 | | } |
| | | 486 | | finally |
| | | 487 | | { |
| | 0 | 488 | | ProcessorEventSource.Log.EventProcessorHostCloseStop(this.HostName); |
| | | 489 | | } |
| | 0 | 490 | | } |
| | | 491 | | |
| | | 492 | | /// <summary> |
| | | 493 | | /// Convenience method for generating unique host names, safe to pass to the EventProcessorHost constructors |
| | | 494 | | /// that take a hostName argument. |
| | | 495 | | /// |
| | | 496 | | /// If a prefix is supplied, the constructed name begins with that string. If the prefix argument is null or |
| | | 497 | | /// an empty string, the constructed name begins with "host". Then a dash '-' and a unique ID are appended to |
| | | 498 | | /// create a unique name. |
| | | 499 | | /// </summary> |
| | | 500 | | /// <param name="prefix">String to use as the beginning of the name. If null or empty, a default is used.</param |
| | | 501 | | /// <returns>A unique host name to pass to EventProcessorHost constructors.</returns> |
| | | 502 | | static string CreateHostName(string prefix) |
| | | 503 | | { |
| | 0 | 504 | | if (string.IsNullOrEmpty(prefix)) |
| | | 505 | | { |
| | 0 | 506 | | prefix = "host"; |
| | | 507 | | } |
| | | 508 | | |
| | 0 | 509 | | return prefix + "-" + Guid.NewGuid(); |
| | | 510 | | } |
| | | 511 | | |
| | | 512 | | internal EventHubClient CreateEventHubClient() |
| | | 513 | | { |
| | | 514 | | // Token provider already provided? |
| | 0 | 515 | | if (this.tokenProvider == null) |
| | | 516 | | { |
| | 0 | 517 | | return EventHubClient.CreateFromConnectionString(this.eventHubConnectionString); |
| | | 518 | | } |
| | | 519 | | else |
| | | 520 | | { |
| | 0 | 521 | | return EventHubClient.CreateWithTokenProvider( |
| | 0 | 522 | | this.EndpointAddress, |
| | 0 | 523 | | this.EventHubPath, |
| | 0 | 524 | | this.tokenProvider, |
| | 0 | 525 | | this.OperationTimeout, |
| | 0 | 526 | | this.TransportType); |
| | | 527 | | } |
| | | 528 | | } |
| | | 529 | | } |
| | | 530 | | } |