Class KafkaProducer
Produces the messages to an endpoint.
Inherited Members
Namespace: Silverback.Messaging.Broker
Assembly: Silverback.Integration.Kafka.dll
Syntax
public sealed class KafkaProducer : Producer, IProducer, IDisposable
Constructors
KafkaProducer(string, IConfluentProducerWrapper, KafkaProducerConfiguration, IBrokerBehaviorsProvider<IProducerBehavior>, IServiceProvider, ISilverbackLogger<KafkaProducer>)
Initializes a new instance of the KafkaProducer class.
Declaration
public KafkaProducer(string name, IConfluentProducerWrapper client, KafkaProducerConfiguration configuration, IBrokerBehaviorsProvider<IProducerBehavior> behaviorsProvider, IServiceProvider serviceProvider, ISilverbackLogger<KafkaProducer> logger)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | The producer identifier. |
| IConfluentProducerWrapper | client | |
| KafkaProducerConfiguration | configuration | The configuration containing only the actual endpoint. |
| IBrokerBehaviorsProvider<IProducerBehavior> | behaviorsProvider | |
| IServiceProvider | serviceProvider | The IServiceProvider to be used to resolve the required services. |
| ISilverbackLogger<KafkaProducer> | logger |
Properties
Client
Gets the related IBrokerClient.
Declaration
public IConfluentProducerWrapper Client { get; }
Property Value
| Type | Description |
|---|---|
| IConfluentProducerWrapper |
Configuration
Gets the producer configuration.
Declaration
public KafkaProducerConfiguration Configuration { get; }
Property Value
| Type | Description |
|---|---|
| KafkaProducerConfiguration |
EndpointConfiguration
Gets the endpoint configuration.
Declaration
public KafkaProducerEndpointConfiguration EndpointConfiguration { get; }
Property Value
| Type | Description |
|---|---|
| KafkaProducerEndpointConfiguration |
Methods
ProduceCore(IOutboundEnvelope)
Publishes the specified message and returns its identifier.
Declaration
protected override IBrokerMessageIdentifier? ProduceCore(IOutboundEnvelope envelope)
Parameters
| Type | Name | Description |
|---|---|---|
| IOutboundEnvelope | envelope | The envelope containing the message to be produced. |
Returns
| Type | Description |
|---|---|
| IBrokerMessageIdentifier | The message identifier assigned by the broker (the Kafka offset or similar). |
Overrides
ProduceCoreAsync(IOutboundEnvelope, CancellationToken)
Publishes the specified message and returns its identifier.
Declaration
protected override ValueTask<IBrokerMessageIdentifier?> ProduceCoreAsync(IOutboundEnvelope envelope, CancellationToken cancellationToken)
Parameters
| Type | Name | Description |
|---|---|---|
| IOutboundEnvelope | envelope | The envelope containing the message to be produced. |
| CancellationToken | cancellationToken | The cancellation token that can be used to cancel the operation. |
Returns
| Type | Description |
|---|---|
| ValueTask<IBrokerMessageIdentifier> | A ValueTask<TResult> representing the asynchronous operation. The task result contains the message identifier assigned by the broker (the Kafka offset or similar). |
Overrides
ProduceCore<TState>(IOutboundEnvelope, Action<IBrokerMessageIdentifier, TState>, Action<Exception, TState>, TState)
Publishes the specified message and returns its identifier.
Declaration
protected override void ProduceCore<TState>(IOutboundEnvelope envelope, Action<IBrokerMessageIdentifier, TState> onSuccess, Action<Exception, TState> onError, TState state)
Parameters
| Type | Name | Description |
|---|---|---|
| IOutboundEnvelope | envelope | The envelope containing the message to be produced. |
| Action<IBrokerMessageIdentifier, TState> | onSuccess | The callback to be invoked when the message is successfully produced. |
| Action<Exception, TState> | onError | The callback to be invoked when the produce fails. |
| TState | state | The state object that will be passed to the callbacks. |
Type Parameters
| Name | Description |
|---|---|
| TState | The type of the state object that will be passed to the callbacks. |
Overrides
Remarks
In this implementation, the message is synchronously enqueued but produced asynchronously. The callbacks are called when the message is actually produced (or the produce failed).