예제 #1
0
        /// <summary>
        ///   Creates an Event Hub 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="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 virtual EventHubConsumer CreateConsumer(string partitionId,
                                                       EventPosition eventPosition,
                                                       EventHubConsumerOptions consumerOptions = default)
        {
            Guard.ArgumentNotNullOrEmpty(nameof(partitionId), partitionId);
            Guard.ArgumentNotNull(nameof(eventPosition), eventPosition);

            var options = consumerOptions?.Clone() ?? new EventHubConsumerOptions {
                Retry = null, DefaultMaximumReceiveWaitTime = null
            };

            options.Retry = options.Retry ?? ClientOptions.Retry.Clone();
            options.DefaultMaximumReceiveWaitTime = options.MaximumReceiveWaitTimeOrDefault ?? ClientOptions.DefaultTimeout;

            return(InnerClient.CreateConsumer(partitionId, eventPosition, options));
        }
        /// <summary>
        ///   Initializes a new instance of the <see cref="EventHubConsumer"/> class.
        /// </summary>
        ///
        /// <param name="transportConsumer">An abstracted Event Consumer specific to the active protocol and transport intended to perform delegated operations.</param>
        /// <param name="eventHubPath">The path of the Event Hub from which events will be received.</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 use for this consumer.</param>
        ///
        /// <remarks>
        ///   If the starting event position is not specified in the <paramref name="consumerOptions"/>, the consumer will
        ///   default to ignoring events in the partition that were queued prior to the consumer being created and read only
        ///   events which appear after that point.
        ///
        ///   Because this is a non-public constructor, it is assumed that the <paramref name="consumerOptions" /> passed are
        ///   owned by this instance and are safe from changes made by consumers.  It is considered the responsibility of the
        ///   caller to ensure that any needed cloning of options is performed.
        /// </remarks>
        ///
        internal EventHubConsumer(TransportEventHubConsumer transportConsumer,
                                  string eventHubPath,
                                  string partitionId,
                                  EventPosition eventPosition,
                                  EventHubConsumerOptions consumerOptions)
        {
            Guard.ArgumentNotNull(nameof(transportConsumer), transportConsumer);
            Guard.ArgumentNotNullOrEmpty(nameof(eventHubPath), eventHubPath);
            Guard.ArgumentNotNullOrEmpty(nameof(partitionId), partitionId);
            Guard.ArgumentNotNull(nameof(eventPosition), eventPosition);
            Guard.ArgumentNotNull(nameof(consumerOptions), consumerOptions);

            PartitionId      = partitionId;
            StartingPosition = eventPosition;
            OwnerLevel       = consumerOptions.OwnerLevel;
            ConsumerGroup    = consumerOptions.ConsumerGroup;
            Options          = consumerOptions;
            InnerConsumer    = transportConsumer;
        }