| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using Azure.Core; |
| | 6 | | using Azure.Messaging.EventHubs.Core; |
| | 7 | |
|
| | 8 | | namespace Azure.Messaging.EventHubs.Consumer |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Represents an Event Hub partition and its relative state, as scoped to an associated |
| | 12 | | /// operation performed against it. |
| | 13 | | /// </summary> |
| | 14 | | /// |
| | 15 | | public class PartitionContext |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// The identifier of the Event Hub partition this context is associated with. |
| | 19 | | /// </summary> |
| | 20 | | /// |
| 5800 | 21 | | public string PartitionId { get; } |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// The <see cref="TransportConsumer" /> for this context to use as the source for information. |
| | 25 | | /// </summary> |
| | 26 | | /// |
| 4 | 27 | | private WeakReference<TransportConsumer> SourceConsumer { get; } |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// A set of information about the last enqueued event of a partition, as observed by the associated EventHubs |
| | 31 | | /// associated with this context as events are received from the Event Hubs service. This is only available i |
| | 32 | | /// created with <see cref="ReadEventOptions.TrackLastEnqueuedEventProperties" /> set. |
| | 33 | | /// </summary> |
| | 34 | | /// |
| | 35 | | /// <returns>The set of properties for the last event that was enqueued to the partition. If no events were rea |
| | 36 | | /// |
| | 37 | | /// <remarks> |
| | 38 | | /// When information about the partition's last enqueued event is being tracked, each event received from the |
| | 39 | | /// service will carry metadata about the partition that it otherwise would not. This results in a small amoun |
| | 40 | | /// additional network bandwidth consumption that is generally a favorable trade-off when considered |
| | 41 | | /// against periodically making requests for partition properties using an Event Hub client. |
| | 42 | | /// </remarks> |
| | 43 | | /// |
| | 44 | | /// <exception cref="EventHubsException">Occurs when the Event Hubs client needed to read this information is no |
| | 45 | | /// |
| | 46 | | public virtual LastEnqueuedEventProperties ReadLastEnqueuedEventProperties() |
| | 47 | | { |
| 4 | 48 | | var consumer = default(TransportConsumer); |
| | 49 | |
|
| 4 | 50 | | if ((SourceConsumer?.TryGetTarget(out consumer) == false) || (consumer == null)) |
| | 51 | | { |
| | 52 | | // If the consumer instance was not available, treat it as a closed instance for |
| | 53 | | // messaging consistency. |
| | 54 | |
|
| 2 | 55 | | Argument.AssertNotClosed(true, Resources.ClientNeededForThisInformationNotAvailable); |
| | 56 | | } |
| | 57 | |
|
| 2 | 58 | | return new LastEnqueuedEventProperties(consumer.LastReceivedEvent); |
| | 59 | | } |
| | 60 | |
|
| | 61 | | /// <summary> |
| | 62 | | /// Initializes a new instance of the <see cref="PartitionContext"/> class. |
| | 63 | | /// </summary> |
| | 64 | | /// |
| | 65 | | /// <param name="partitionId">The identifier of the Event Hub partition this context is associated with.</param> |
| | 66 | | /// <param name="consumer">The <see cref="TransportConsumer" /> for this context to use as the source for inform |
| | 67 | | /// |
| | 68 | | /// <remarks> |
| | 69 | | /// The <paramref name="consumer" />, if provided, will be held in a weak reference to ensure that it |
| | 70 | | /// does not impact resource use should the partition context be held beyond the lifespan of the |
| | 71 | | /// consumer instance. |
| | 72 | | /// </remarks> |
| | 73 | | /// |
| | 74 | | internal PartitionContext(string partitionId, |
| 156 | 75 | | TransportConsumer consumer) : this(partitionId) |
| | 76 | | { |
| 152 | 77 | | Argument.AssertNotNull(consumer, nameof(consumer)); |
| 150 | 78 | | SourceConsumer = new WeakReference<TransportConsumer>(consumer); |
| 150 | 79 | | } |
| | 80 | |
|
| | 81 | | /// <summary> |
| | 82 | | /// Initializes a new instance of the <see cref="PartitionContext"/> class. |
| | 83 | | /// </summary> |
| | 84 | | /// |
| | 85 | | /// <param name="partitionId">The identifier of the Event Hub partition this context is associated with.</param> |
| | 86 | | /// |
| 160 | 87 | | protected internal PartitionContext(string partitionId) |
| | 88 | | { |
| 160 | 89 | | Argument.AssertNotNullOrEmpty(partitionId, nameof(partitionId)); |
| 152 | 90 | | PartitionId = partitionId; |
| 152 | 91 | | } |
| | 92 | | } |
| | 93 | | } |