Condividi tramite


EventHubProducerClient Class

Definition

A client responsible for publishing EventData to a specific Event Hub, grouped together in batches. Depending on the options specified when sending, events may be automatically assigned an available partition or may request a specific partition.

The EventHubProducerClient publishes immediately, ensuring a deterministic outcome for each send operation, though requires that callers own the responsibility of building and managing batches.

In scenarios where it is not important to have events published immediately and where maximizing partition availability is not a requirement, it is recommended to consider using the EventHubBufferedProducerClient, which takes responsibility for building and managing batches to reduce the complexity of doing so in application code.

public class EventHubProducerClient : IAsyncDisposable
type EventHubProducerClient = class
    interface IAsyncDisposable
Public Class EventHubProducerClient
Implements IAsyncDisposable
Inheritance
EventHubProducerClient
Implements

Remarks

Allowing partition assignment is recommended when:

  • The sending of events needs to be highly available.
  • The event data should be evenly distributed among all available partitions.

If no partition is specified, the following rules are used for automatically selecting one:

  1. Distribute the events equally amongst all available partitions using a round-robin approach.
  2. If a partition becomes unavailable, the Event Hubs service will automatically detect it and forward the message to another available partition.

The EventHubProducerClient is safe to cache and use for the lifetime of an application, which is the best practice when the application publishes events regularly or semi-regularly. The producer is responsible for ensuring efficient network, CPU, and memory use. Calling either CloseAsync(CancellationToken) or DisposeAsync() as the application is shutting down will ensure that network resources and other unmanaged objects are properly cleaned up.

Constructors

EventHubProducerClient()

Initializes a new instance of the EventHubProducerClient class.

EventHubProducerClient(EventHubConnection, EventHubProducerClientOptions)

Initializes a new instance of the EventHubProducerClient class.

EventHubProducerClient(String, EventHubProducerClientOptions)

Initializes a new instance of the EventHubProducerClient class.

EventHubProducerClient(String, String, AzureNamedKeyCredential, EventHubProducerClientOptions)

Initializes a new instance of the EventHubProducerClient class.

EventHubProducerClient(String, String, AzureSasCredential, EventHubProducerClientOptions)

Initializes a new instance of the EventHubProducerClient class.

EventHubProducerClient(String, String, EventHubProducerClientOptions)

Initializes a new instance of the EventHubProducerClient class.

EventHubProducerClient(String, String, TokenCredential, EventHubProducerClientOptions)

Initializes a new instance of the EventHubProducerClient class.

EventHubProducerClient(String, String)

Initializes a new instance of the EventHubProducerClient class.

EventHubProducerClient(String)

Initializes a new instance of the EventHubProducerClient class.

Properties

EventHubName

The name of the Event Hub that the producer is connected to, specific to the Event Hubs namespace that contains it.

FullyQualifiedNamespace

The fully qualified Event Hubs namespace that the producer is associated with. This is likely to be similar to {yournamespace}.servicebus.windows.net.

Identifier

A unique name used to identify this producer.

IsClosed

Indicates whether or not this EventHubProducerClient has been closed.

Methods

CloseAsync(CancellationToken)

Closes the producer.

CreateBatchAsync(CancellationToken)

Creates a size-constraint batch to which EventData may be added using a try-based pattern. If an event would exceed the maximum allowable size of the batch, the batch will not allow adding the event and signal that scenario using its return value.

Because events that would violate the size constraint cannot be added, publishing a batch will not trigger an exception when attempting to send the events to the Event Hubs service.

CreateBatchAsync(CreateBatchOptions, CancellationToken)

Creates a size-constraint batch to which EventData may be added using a try-based pattern. If an event would exceed the maximum allowable size of the batch, the batch will not allow adding the event and signal that scenario using its return value.

Because events that would violate the size constraint cannot be added, publishing a batch will not trigger an exception when attempting to send the events to the Event Hubs service.

DisposeAsync()

Performs the task needed to clean up resources used by the EventHubProducerClient, including ensuring that the client itself has been closed.

GetEventHubPropertiesAsync(CancellationToken)

Retrieves information about the Event Hub that the connection is associated with, including the number of partitions present and their identifiers.

GetPartitionIdsAsync(CancellationToken)

Retrieves the set of identifiers for the partitions of an Event Hub.

GetPartitionPropertiesAsync(String, CancellationToken)

Retrieves information about a specific partition for an Event Hub, including elements that describe the available events in the partition event stream.

SendAsync(EventDataBatch, CancellationToken)

Sends a set of events to the associated Event Hub using a batched approach.

SendAsync(IEnumerable<EventData>, CancellationToken)

Sends a set of events to the associated Event Hub as a single operation. To avoid the overhead associated with measuring and validating the size in the client, validation will be delegated to the Event Hubs service and is deferred until the operation is invoked. The call will fail if the size of the specified set of events exceeds the maximum allowable size of a single batch.

SendAsync(IEnumerable<EventData>, SendEventOptions, CancellationToken)

Sends a set of events to the associated Event Hub as a single operation. To avoid the overhead associated with measuring and validating the size in the client, validation will be delegated to the Event Hubs service and is deferred until the operation is invoked. The call will fail if the size of the specified set of events exceeds the maximum allowable size of a single batch.

Applies to

See also