| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Text; |
| | 6 | | using System.Threading.Tasks; |
| | 7 | | using Azure.Messaging.EventHubs.Producer; |
| | 8 | | using Azure.Messaging.EventHubs.Samples.Infrastructure; |
| | 9 | |
|
| | 10 | | namespace Azure.Messaging.EventHubs.Samples |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// An example of publishing events using a custom size limitation with the batch. |
| | 14 | | /// </summary> |
| | 15 | | /// |
| | 16 | | public class Sample11_PublishAnEventBatchWithCustomSizeLimit : IEventHubsSample |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// The name of the sample. |
| | 20 | | /// </summary> |
| | 21 | | /// |
| 0 | 22 | | public string Name => nameof(Sample11_PublishAnEventBatchWithCustomSizeLimit); |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// A short description of the sample. |
| | 26 | | /// </summary> |
| | 27 | | /// |
| 0 | 28 | | public string Description => "An example of publishing events using a custom size limitation with the batch."; |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Runs the sample using the specified Event Hubs connection information. |
| | 32 | | /// </summary> |
| | 33 | | /// |
| | 34 | | /// <param name="connectionString">The connection string for the Event Hubs namespace that the sample should tar |
| | 35 | | /// <param name="eventHubName">The name of the Event Hub, sometimes known as its path, that she sample should ru |
| | 36 | | /// |
| | 37 | | public async Task RunAsync(string connectionString, |
| | 38 | | string eventHubName) |
| | 39 | | { |
| | 40 | | // We will start by creating a producer client using its default options. |
| | 41 | |
|
| 0 | 42 | | await using (var producerClient = new EventHubProducerClient(connectionString, eventHubName)) |
| | 43 | | { |
| | 44 | | // There is a limit to the size of an event or batch of events that can be published at once. This limi |
| | 45 | | // on the Event Hubs service and the properties of the namespace that owns the target Event Hub. Becaus |
| | 46 | | // on the size of an event or batch as it would be sent over the network, it is not simple to understand |
| | 47 | | // it is being created. |
| | 48 | | // |
| | 49 | | // In order to avoid failures when publishing, events are packaged into an Event Batch. When created, t |
| | 50 | | // of the size limit for the associated Event Hub. Events can be added to the batch using a "TryAdd" pa |
| | 51 | | // into when an event or batch would be too large to publish without triggering an exception. |
| | 52 | | // |
| | 53 | | // To support scenarios where bandwidth is limited or publishers need to maintain control over how much |
| | 54 | | // a custom size limit (in bytes) may be specified when creating an event batch, overriding the default |
| | 55 | | // Hub. It is important to note that the custom limit may not exceed the limit specified by the Event H |
| | 56 | | // |
| | 57 | | // In this example, we'll create a batch with a small limit and add events until no more are able to fit |
| | 58 | |
|
| 0 | 59 | | CreateBatchOptions options = new CreateBatchOptions |
| 0 | 60 | | { |
| 0 | 61 | | MaximumSizeInBytes = 150 |
| 0 | 62 | | }; |
| | 63 | |
|
| 0 | 64 | | using EventDataBatch batch = await producerClient.CreateBatchAsync(options); |
| 0 | 65 | | EventData currentEvent = new EventData(Encoding.UTF8.GetBytes("First event")); |
| | 66 | |
|
| 0 | 67 | | while (batch.TryAdd(currentEvent)) |
| | 68 | | { |
| 0 | 69 | | Console.WriteLine($"The batch is now { batch.SizeInBytes } bytes in size."); |
| 0 | 70 | | currentEvent = new EventData(Encoding.UTF8.GetBytes($"Event { batch.Count + 1 }")); |
| | 71 | | } |
| | 72 | |
|
| 0 | 73 | | Console.WriteLine($"The final size of the batch is { batch.SizeInBytes } bytes."); |
| 0 | 74 | | Console.WriteLine(); |
| | 75 | |
|
| | 76 | | // Now that the batch is full, send it. |
| | 77 | |
|
| 0 | 78 | | await producerClient.SendAsync(batch); |
| 0 | 79 | | Console.WriteLine("The event batch has been published."); |
| 0 | 80 | | } |
| | 81 | |
|
| | 82 | | // At this point, our client has passed its "using" scope and has safely been disposed of. We |
| | 83 | | // have no further obligations. |
| | 84 | |
|
| 0 | 85 | | Console.WriteLine(); |
| 0 | 86 | | } |
| | 87 | | } |
| | 88 | | } |