| | 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 introduction to publishing events, using a batch with single event. |
| | 14 | | /// </summary> |
| | 15 | | /// |
| | 16 | | public class Sample03_PublishAnEventBatch : IEventHubsSample |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// The name of the sample. |
| | 20 | | /// </summary> |
| | 21 | | /// |
| 0 | 22 | | public string Name => nameof(Sample03_PublishAnEventBatch); |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// A short description of the sample. |
| | 26 | | /// </summary> |
| | 27 | | /// |
| 0 | 28 | | public string Description => "An introduction to publishing events, using a batch with single event."; |
| | 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 | | // To publish events, we will need to create a producer client. Like any client, our Event Hub producer man |
| | 41 | | // and should be explicitly closed or disposed, but it is not necessary to do both. In this example, we wil |
| | 42 | | // advantage of the new asynchronous dispose to ensure that we clean up our producer client when we are |
| | 43 | | // done or when an exception is encountered. |
| | 44 | |
|
| 0 | 45 | | await using (var producerClient = new EventHubProducerClient(connectionString, eventHubName)) |
| | 46 | | { |
| | 47 | | // An Event Hub producer is not associated with any specific partition. When publishing events, |
| | 48 | | // it will allow the Event Hubs service to route the event to an available partition. |
| | 49 | | // |
| | 50 | | // Allowing automatic routing of partitions is recommended when: |
| | 51 | | // - The publishing of events needs to be highly available. |
| | 52 | | // - The event data should be evenly distributed among all available partitions. |
| | 53 | | // |
| | 54 | | // An event is represented by an arbitrary collection of bytes and metadata. Event Hubs does not make a |
| | 55 | | // assumptions about the data nor attempt to perform any operations on it; you are free to create the da |
| | 56 | | // in whatever form makes sense for your scenario. |
| | 57 | | // |
| | 58 | | // In our case, we will translate a simple sentence into bytes and send it to our Event Hub. |
| | 59 | |
|
| 0 | 60 | | using EventDataBatch eventBatch = await producerClient.CreateBatchAsync(); |
| 0 | 61 | | eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("Hello, Event Hubs!"))); |
| | 62 | |
|
| | 63 | | // When the producer sends the event, it will receive an acknowledgment from the Event Hubs service; so |
| | 64 | | // long as there is no exception thrown by this call, the service is now responsible for delivery. Your |
| | 65 | | // event data will be published to one of the Event Hub partitions, though there may be a (very) slight |
| | 66 | | // delay until it is available to be consumed. |
| | 67 | |
|
| 0 | 68 | | await producerClient.SendAsync(eventBatch); |
| | 69 | |
|
| 0 | 70 | | Console.WriteLine("The simple event batch has been published."); |
| 0 | 71 | | } |
| | 72 | |
|
| | 73 | | // At this point, our client has passed its "using" scope and has safely been disposed of. We |
| | 74 | | // have no further obligations. |
| | 75 | |
|
| 0 | 76 | | Console.WriteLine(); |
| 0 | 77 | | } |
| | 78 | | } |
| | 79 | | } |