pub struct BaseProducer<C = DefaultProducerContext> where
    C: ProducerContext
{ /* private fields */ }
Expand description

Lowest level Kafka producer.

The BaseProducer needs to be polled at regular intervals in order to serve queued delivery report callbacks (for more information, refer to the module-level documentation). This producer can be cheaply cloned to create a new reference to the same underlying producer.

Example usage

This code will send a message to Kafka. No custom ProducerContext is specified, so the DefaultProducerContext will be used. To see how to use a producer context, refer to the examples in the examples folder.

use rdkafka::config::ClientConfig;
use rdkafka::producer::{BaseProducer, BaseRecord, Producer};
use std::time::Duration;

let producer: BaseProducer = ClientConfig::new()
    .set("bootstrap.servers", "kafka:9092")
    .create()
    .expect("Producer creation error");

producer.send(
    BaseRecord::to("destination_topic")
        .payload("this is the payload")
        .key("and this is a key"),
).expect("Failed to enqueue");

// Poll at regular intervals to process all the asynchronous delivery events.
for _ in 0..10 {
    producer.poll(Duration::from_millis(100));
}

// And/or flush the producer before dropping it.
producer.flush(Duration::from_secs(1));

Implementations

Polls the producer, returning the number of events served.

Regular calls to poll are required to process the events and execute the message delivery callbacks.

Sends a message to Kafka.

Message fields such as key, payload, partition, timestamp etc. are provided to this method via a BaseRecord. If the message is correctly enqueued in the producer’s memory buffer, the method will take ownership of the record and return immediately; in case of failure to enqueue, the original record is returned, alongside an error code. If the message fails to be produced after being enqueued in the buffer, the ProducerContext::delivery method will be called asynchronously, with the provided ProducerContext::DeliveryOpaque.

When no partition is specified the underlying Kafka library picks a partition based on a hash of the key. If no key is specified, a random partition will be used. To correctly handle errors, the delivery callback should be implemented.

Note that this method will never block.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Creates a new BaseProducer starting from a configuration.

Creates a new BaseProducer starting from a configuration and a context.

Returns the Client underlying this producer.

Flushes any pending messages. Read more

Returns the number of messages that are either waiting to be sent or are sent but are waiting to be acknowledged. Read more

Enable sending transactions with this producer. Read more

Begins a new transaction. Read more

Associates an offset commit operation with this transaction. Read more

Commits the current transaction. Read more

Aborts the current transaction. Read more

Returns a reference to the ProducerContext used to create this producer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.