Beispiel #1
0
        /// <summary>
        ///   Creates a consumer responsible for reading <see cref="EventData" /> from a specific Event Hub partition,
        ///   and as a member of a specific consumer group.
        ///
        ///   A consumer may be exclusive, which asserts ownership over the partition for the consumer
        ///   group to ensure that only one consumer from that group is reading the from the partition.
        ///   These exclusive consumers are sometimes referred to as "Epoch Consumers."
        ///
        ///   A consumer may also be non-exclusive, allowing multiple consumers from the same consumer
        ///   group to be actively reading events from the partition.  These non-exclusive consumers are
        ///   sometimes referred to as "Non-epoch Consumers."
        ///
        ///   Designating a consumer as exclusive may be specified in the <paramref name="consumerOptions" />.
        ///   By default, consumers are created as non-exclusive.
        /// </summary>
        ///
        /// <param name="consumerGroup">The name of the consumer group this consumer is associated with.  Events are read in the context of this group.</param>
        /// <param name="partitionId">The identifier of the Event Hub partition from which events will be received.</param>
        /// <param name="eventPosition">The position within the partition where the consumer should begin reading events.</param>
        /// <param name="consumerOptions">The set of options to apply when creating the consumer.</param>
        /// <param name="defaultRetryPolicy">The default retry policy to use if no retry options were specified in the <paramref name="consumerOptions" />.</param>
        ///
        /// <returns>An Event Hub consumer configured in the requested manner.</returns>
        ///
        public override EventHubConsumer CreateConsumer(string consumerGroup,
                                                        string partitionId,
                                                        EventPosition eventPosition,
                                                        EventHubConsumerOptions consumerOptions,
                                                        EventHubRetryPolicy defaultRetryPolicy)
        {
            TrackOne.PartitionReceiver CreateReceiverFactory(EventHubRetryPolicy activeRetryPolicy)
            {
                var position = new TrackOne.EventPosition
                {
                    IsInclusive     = eventPosition.IsInclusive,
                    Offset          = eventPosition.Offset,
                    SequenceNumber  = eventPosition.SequenceNumber,
                    EnqueuedTimeUtc = eventPosition.EnqueuedTime?.UtcDateTime
                };

                var trackOneOptions = new TrackOne.ReceiverOptions
                {
                    Identifier = consumerOptions.Identifier,
                    EnableReceiverRuntimeMetric = consumerOptions.TrackLastEnqueuedEventInformation
                };

                PartitionReceiver consumer;

                if (consumerOptions.OwnerLevel.HasValue)
                {
                    consumer = TrackOneClient.CreateEpochReceiver(consumerGroup, partitionId, position, consumerOptions.OwnerLevel.Value, trackOneOptions);
                }
                else
                {
                    consumer = TrackOneClient.CreateReceiver(consumerGroup, partitionId, position, trackOneOptions);
                }

                consumer.RetryPolicy = new TrackOneRetryPolicy(activeRetryPolicy);

                return(consumer);
            }

            EventHubRetryPolicy initialRetryPolicy = (consumerOptions.RetryOptions != null)
                ? new BasicRetryPolicy(consumerOptions.RetryOptions)
                : defaultRetryPolicy;

            LastEnqueuedEventProperties partitionMetrics = (consumerOptions.TrackLastEnqueuedEventInformation)
                ? new LastEnqueuedEventProperties(EventHubName, partitionId)
                : null;

            return(new EventHubConsumer
                   (
                       new TrackOneEventHubConsumer(CreateReceiverFactory, initialRetryPolicy, partitionMetrics),
                       TrackOneClient.EventHubName,
                       partitionId,
                       consumerGroup,
                       eventPosition,
                       consumerOptions,
                       initialRetryPolicy
                   ));
        }
        /// <summary>
        ///   Creates a consumer responsible for reading <see cref="EventData" /> from a specific Event Hub partition,
        ///   and as a member of a specific consumer group.
        ///
        ///   A consumer may be exclusive, which asserts ownership over the partition for the consumer
        ///   group to ensure that only one consumer from that group is reading the from the partition.
        ///   These exclusive consumers are sometimes referred to as "Epoch Consumers."
        ///
        ///   A consumer may also be non-exclusive, allowing multiple consumers from the same consumer
        ///   group to be actively reading events from the partition.  These non-exclusive consumers are
        ///   sometimes referred to as "Non-epoch Consumers."
        ///
        ///   Designating a consumer as exclusive may be specified in the <paramref name="consumerOptions" />.
        ///   By default, consumers are created as non-exclusive.
        /// </summary>
        ///
        /// <param name="consumerGroup">The name of the consumer group this consumer is associated with.  Events are read in the context of this group.</param>
        /// <param name="partitionId">The identifier of the Event Hub partition from which events will be received.</param>
        /// <param name="eventPosition">The position within the partition where the consumer should begin reading events.</param>
        /// <param name="consumerOptions">The set of options to apply when creating the consumer.</param>
        ///
        /// <returns>An Event Hub consumer configured in the requested manner.</returns>
        ///
        public override EventHubConsumer CreateConsumer(string consumerGroup,
                                                        string partitionId,
                                                        EventPosition eventPosition,
                                                        EventHubConsumerOptions consumerOptions)
        {
            TrackOne.PartitionReceiver CreateReceiverFactory()
            {
                var position = new TrackOne.EventPosition
                {
                    IsInclusive     = eventPosition.IsInclusive,
                    Offset          = eventPosition.Offset,
                    SequenceNumber  = eventPosition.SequenceNumber,
                    EnqueuedTimeUtc = eventPosition.EnqueuedTime?.UtcDateTime
                };

                var trackOneOptions = new TrackOne.ReceiverOptions {
                    Identifier = consumerOptions.Identifier
                };

                PartitionReceiver consumer;

                if (consumerOptions.OwnerLevel.HasValue)
                {
                    consumer = TrackOneClient.CreateEpochReceiver(consumerGroup, partitionId, position, consumerOptions.OwnerLevel.Value, trackOneOptions);
                }
                else
                {
                    consumer = TrackOneClient.CreateReceiver(consumerGroup, partitionId, position, trackOneOptions);
                }

                (TimeSpan minBackoff, TimeSpan maxBackoff, int maxRetries) = ((ExponentialRetry)consumerOptions.Retry).GetProperties();
                consumer.RetryPolicy = new RetryExponential(minBackoff, maxBackoff, maxRetries);

                return(consumer);
            }

            return(new EventHubConsumer
                   (
                       new TrackOneEventHubConsumer(CreateReceiverFactory),
                       TrackOneClient.EventHubName,
                       partitionId,
                       consumerGroup,
                       eventPosition,
                       consumerOptions
                   ));
        }
        /// <summary>
        ///   Creates an event receiver responsible for reading <see cref="EventData" /> from a specific Event Hub partition,
        ///   and as a member of a specific consumer group.
        ///
        ///   A receiver may be exclusive, which asserts ownership over the partition for the consumer
        ///   group to ensure that only one receiver from that group is reading the from the partition.
        ///   These exclusive receivers are sometimes referred to as "Epoch Receivers."
        ///
        ///   A receiver may also be non-exclusive, allowing multiple receivers from the same consumer
        ///   group to be actively reading events from the partition.  These non-exclusive receivers are
        ///   sometimes referred to as "Non-epoch Receivers."
        ///
        ///   Designating a receiver as exclusive may be specified in the <paramref name="receiverOptions" />.
        ///   By default, receivers are created as non-exclusive.
        /// </summary>
        ///
        /// <param name="partitionId">The identifier of the Event Hub partition from which events will be received.</param>
        /// <param name="receiverOptions">The set of options to apply when creating the receiver.</param>
        ///
        /// <returns>An event receiver configured in the requested manner.</returns>
        ///
        public override EventReceiver CreateReceiver(string partitionId,
                                                     EventReceiverOptions receiverOptions)
        {
            TrackOne.PartitionReceiver CreateReceiverFactory()
            {
                var position = new TrackOne.EventPosition
                {
                    IsInclusive     = receiverOptions.BeginReceivingAt.IsInclusive,
                    Offset          = receiverOptions.BeginReceivingAt.Offset,
                    SequenceNumber  = receiverOptions.BeginReceivingAt.SequenceNumber,
                    EnqueuedTimeUtc = receiverOptions.BeginReceivingAt.EnqueuedTimeUtc
                };

                var trackOneOptions = new TrackOne.ReceiverOptions {
                    Identifier = receiverOptions.Identifier
                };

                PartitionReceiver receiver;

                if (receiverOptions.ExclusiveReceiverPriority.HasValue)
                {
                    receiver = TrackOneClient.CreateEpochReceiver(receiverOptions.ConsumerGroup, partitionId, position, receiverOptions.ExclusiveReceiverPriority.Value, trackOneOptions);
                }
                else
                {
                    receiver = TrackOneClient.CreateReceiver(receiverOptions.ConsumerGroup, partitionId, position, trackOneOptions);
                }

                (TimeSpan minBackoff, TimeSpan maxBackoff, int maxRetries) = ((ExponentialRetry)receiverOptions.Retry).GetProperties();
                receiver.RetryPolicy = new RetryExponential(minBackoff, maxBackoff, maxRetries);

                return(receiver);
            }

            return(new EventReceiver
                   (
                       new TrackOneEventReceiver(CreateReceiverFactory),
                       TrackOneClient.EventHubName,
                       partitionId,
                       receiverOptions
                   ));
        }
 /// <summary>
 /// Returns the position for the end of a stream. Provide this position in receiver creation
 /// to start receiving from the next available event in the partition after the receiver is created.
 /// </summary>
 public static EventPosition FromEnd()
 {
     return(EventPosition.FromOffset(EndOfStream));
 }
 /// <summary>
 /// Returns the position for the start of a stream. Provide this position in receiver creation
 /// to starting receiving from the first available event in the partition.
 /// </summary>
 public static EventPosition FromStart()
 {
     return(EventPosition.FromOffset(StartOfStream));
 }
 /// <summary></summary>
 /// <param name="consumerGroupName"></param>
 /// <param name="partitionId"></param>
 /// <param name="eventPosition"></param>
 /// <param name="epoch"></param>
 /// <param name="receiverOptions"></param>
 /// <returns></returns>
 protected abstract PartitionReceiver OnCreateReceiver(string consumerGroupName, string partitionId, EventPosition eventPosition, long?epoch, ReceiverOptions receiverOptions);
 /// <summary>
 /// Create a Epoch based EventHub receiver with given <see cref="EventPosition"/>.
 /// The receiver is created for a specific EventHub Partition from the specific consumer group.
 /// <para/>It is important to pay attention to the following when creating epoch based receiver:
 /// <para/>- Ownership enforcement: Once you created an epoch based receiver, you cannot create a non-epoch receiver to the same consumerGroup-Partition combo until all receivers to the combo are closed.
 /// <para/>- Ownership stealing: If a receiver with higher epoch value is created for a consumerGroup-Partition combo, any older epoch receiver to that combo will be force closed.
 /// <para/>- Any receiver closed due to lost of ownership to a consumerGroup-Partition combo will get ReceiverDisconnectedException for all operations from that receiver.
 /// </summary>
 /// <param name="consumerGroupName">the consumer group name that this receiver should be grouped under.</param>
 /// <param name="partitionId">the partition Id that the receiver belongs to. All data received will be from this partition only.</param>
 /// <param name="eventPosition">The starting <see cref="EventPosition"/> at which to start receiving messages.</param>
 /// <param name="epoch">a unique identifier (epoch value) that the service uses, to enforce partition/lease ownership.</param>
 /// <param name="receiverOptions">Options for a event hub receiver.</param>
 /// <returns>The created PartitionReceiver</returns>
 /// <seealso cref="PartitionReceiver"/>
 public PartitionReceiver CreateEpochReceiver(string consumerGroupName, string partitionId, EventPosition eventPosition, long epoch, ReceiverOptions receiverOptions = null)
 {
     Guard.ArgumentNotNull(nameof(eventPosition), eventPosition);
     return(OnCreateReceiver(consumerGroupName, partitionId, eventPosition, epoch, receiverOptions));
 }