[][src]Struct rdkafka::producer::base_producer::BaseProducer

pub struct BaseProducer<C: ProducerContext = DefaultProducerContext> { /* fields omitted */ }

Low 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};
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));

Methods

impl<C: ProducerContext> BaseProducer<C>[src]

pub fn poll<T: Into<Timeout>>(&self, timeout: T) -> i32[src]

Polls the producer. Regular calls to poll are required to process the events and execute the message delivery callbacks. Returns the number of events served.

pub fn send<'a, K: ?Sized, P: ?Sized>(
    &self,
    record: BaseRecord<'a, K, P, C::DeliveryOpaque>
) -> Result<(), (KafkaError, BaseRecord<'a, K, P, C::DeliveryOpaque>)> where
    K: ToBytes,
    P: ToBytes
[src]

Produce 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.

pub fn flush<T: Into<Timeout>>(&self, timeout: T)[src]

Flushes the producer. Should be called before termination. This method will call poll() internally.

pub fn in_flight_count(&self) -> i32[src]

Returns the number of messages waiting to be sent, or sent but not acknowledged yet.

Trait Implementations

impl<C: ProducerContext> Clone for BaseProducer<C>[src]

impl FromClientConfig for BaseProducer<DefaultProducerContext>[src]

fn from_config(
    config: &ClientConfig
) -> KafkaResult<BaseProducer<DefaultProducerContext>>
[src]

Creates a new BaseProducer starting from a configuration.

impl<C: ProducerContext> FromClientConfigAndContext<C> for BaseProducer<C>[src]

fn from_config_and_context(
    config: &ClientConfig,
    context: C
) -> KafkaResult<BaseProducer<C>>
[src]

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

Auto Trait Implementations

impl<C> RefUnwindSafe for BaseProducer<C> where
    C: RefUnwindSafe

impl<C> Send for BaseProducer<C>

impl<C> Sync for BaseProducer<C>

impl<C> Unpin for BaseProducer<C>

impl<C> UnwindSafe for BaseProducer<C> where
    C: RefUnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.