public async Task BufferedConfiguration() { await using var scope = await EventHubScope.CreateAsync(1); #region Snippet:EventHubs_Sample04_BufferedConfiguration #if SNIPPET var connectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>"; var eventHubName = "<< NAME OF THE EVENT HUB >>"; #else var connectionString = EventHubsTestEnvironment.Instance.EventHubsConnectionString; var eventHubName = scope.EventHubName; #endif var options = new EventHubBufferedProducerClientOptions { MaximumWaitTime = TimeSpan.FromSeconds(1), MaximumConcurrentSends = 5, MaximumConcurrentSendsPerPartition = 1, MaximumEventBufferLengthPerPartition = 5000, EnableIdempotentRetries = true }; var producer = new EventHubBufferedProducerClient(connectionString, eventHubName, options); // The failure handler is required and invoked after all allowable // retries were applied. producer.SendEventBatchFailedAsync += args => { Debug.WriteLine($"Publishing failed for { args.EventBatch.Count } events. Error: '{ args.Exception.Message }'"); return(Task.CompletedTask); }; // The success handler is optional. producer.SendEventBatchSucceededAsync += args => { Debug.WriteLine($"{ args.EventBatch.Count } events were published to partition: '{ args.PartitionId }."); return(Task.CompletedTask); }; try { for (var index = 0; index < 5; ++index) { var eventData = new EventData($"Event #{ index }"); await producer.EnqueueEventAsync(eventData); } } finally { // Closing the producer will flush any // enqueued events that have not been published. await producer.CloseAsync(); } #endregion }
public async Task PartitionIdBuffered() { await using var scope = await EventHubScope.CreateAsync(1); #region Snippet:EventHubs_Sample04_PartitionIdBuffered #if SNIPPET var connectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>"; var eventHubName = "<< NAME OF THE EVENT HUB >>"; #else var connectionString = EventHubsTestEnvironment.Instance.EventHubsConnectionString; var eventHubName = scope.EventHubName; #endif var producer = new EventHubBufferedProducerClient(connectionString, eventHubName); // The failure handler is required and invoked after all allowable // retries were applied. producer.SendEventBatchFailedAsync += args => { Debug.WriteLine($"Publishing failed for { args.EventBatch.Count } events. Error: '{ args.Exception.Message }'"); return(Task.CompletedTask); }; // The success handler is optional. producer.SendEventBatchSucceededAsync += args => { Debug.WriteLine($"{ args.EventBatch.Count } events were published to partition: '{ args.PartitionId }."); return(Task.CompletedTask); }; try { string firstPartition = (await producer.GetPartitionIdsAsync()).First(); var enqueueOptions = new EnqueueEventOptions { PartitionId = firstPartition }; for (var index = 0; index < 5; ++index) { var eventData = new EventData($"Event #{ index }"); await producer.EnqueueEventAsync(eventData, enqueueOptions); } } finally { // Closing the producer will flush any // enqueued events that have not been published. await producer.CloseAsync(); } #endregion }
/// <summary> /// Enqueues events into the buffer to be published. /// </summary> /// /// <param name="testOptions">The set of options governing test execution.</param> /// private async Task EnqueueEvents(CancellationToken cancellationToken) { var eventBody = EventGenerator.CreateRandomBody(Options.BodySize); try { while (!cancellationToken.IsCancellationRequested) { var enqueueOptions = CreateEnqueueOptions(); // Generate events using the same body. This will result in publishing a set of events // of equal size. The events will only differ by the id property that is assigned to them. if (Options.BatchSize > 1) { await _producer.EnqueueEventsAsync(EventGenerator.CreateEventsFromBody(Options.BatchSize, eventBody), enqueueOptions, cancellationToken).ConfigureAwait(false); } else { await _producer.EnqueueEventAsync(EventGenerator.CreateEventFromBody(eventBody), enqueueOptions, cancellationToken).ConfigureAwait(false); } } } catch (OperationCanceledException) { // Expected } catch (EventHubsException ex) when((cancellationToken.IsCancellationRequested) && (ex.IsTransient)) { // If the enqueue is canceled during a retry loop, the most recent exception is thrown. // If the exception is transient, it should be ignored. } catch (Exception ex) { ErrorRaised(ex); } }
public async Task CustomMetadata() { await using var scope = await EventHubScope.CreateAsync(1); #region Snippet:EventHubs_Sample04_CustomMetadata #if SNIPPET var connectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>"; var eventHubName = "<< NAME OF THE EVENT HUB >>"; #else var connectionString = EventHubsTestEnvironment.Instance.EventHubsConnectionString; var eventHubName = scope.EventHubName; #endif var producer = new EventHubBufferedProducerClient(connectionString, eventHubName); // The failure handler is required and invoked after all allowable // retries were applied. producer.SendEventBatchFailedAsync += args => { Debug.WriteLine($"Publishing failed for { args.EventBatch.Count } events. Error: '{ args.Exception.Message }'"); return(Task.CompletedTask); }; // The success handler is optional. producer.SendEventBatchSucceededAsync += args => { Debug.WriteLine($"{ args.EventBatch.Count } events were published to partition: '{ args.PartitionId }."); return(Task.CompletedTask); }; try { var eventData = new EventData("Hello, Event Hubs!") { MessageId = "H1", ContentType = "application/json" }; eventData.Properties.Add("EventType", "com.microsoft.samples.hello-event"); eventData.Properties.Add("priority", 1); eventData.Properties.Add("score", 9.0); await producer.EnqueueEventAsync(eventData); eventData = new EventData("Goodbye, Event Hubs!") { MessageId = "G1", ContentType = "application/json" }; eventData.Properties.Add("EventType", "com.microsoft.samples.goodbye-event"); eventData.Properties.Add("priority", "17"); eventData.Properties.Add("blob", true); await producer.EnqueueEventAsync(eventData); } finally { // Closing the producer will flush any // enqueued events that have not been published. await producer.CloseAsync(); } #endregion }