Skip to main content

DynProducer

Trait DynProducer 

Source
pub trait DynProducer<K, V>:
    Sealed<K, V>
    + Send
    + Sync {
    // Required methods
    fn init_transactions<'a>(
        &'a self,
    ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>>
       where K: 'a,
             V: 'a;
    fn begin_transaction(&self) -> Result<(), Error>;
    fn send_offsets_to_transaction<'a>(
        &'a self,
        offsets: HashMap<TopicPartition, OffsetAndMetadata>,
        group_metadata: &'a dyn ConsumerGroupMetadata,
    ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>>
       where K: 'a,
             V: 'a;
    fn commit_transaction<'a>(
        &'a self,
    ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>>
       where K: 'a,
             V: 'a;
    fn abort_transaction<'a>(
        &'a self,
    ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>>
       where K: 'a,
             V: 'a;
    fn send<'a>(
        &'a self,
        record: ProducerRecord<K, V>,
    ) -> Pin<Box<dyn Future<Output = Result<KafkaFuture<RecordMetadata>, Error>> + Send + 'a>>
       where K: 'a,
             V: 'a;
    fn send_with_callback<'a>(
        &'a self,
        record: ProducerRecord<K, V>,
        callback: Option<Box<dyn Callback>>,
    ) -> Pin<Box<dyn Future<Output = Result<KafkaFuture<RecordMetadata>, Error>> + Send + 'a>>
       where K: 'a,
             V: 'a;
    fn flush<'a>(
        &'a self,
    ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>>
       where K: 'a,
             V: 'a;
    fn partitions_for<'a>(
        &'a self,
        topic: &'a str,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<PartitionInfo>, Error>> + Send + 'a>>
       where K: 'a,
             V: 'a;
    fn metrics(&self) -> MetricsView;
    fn close<'a>(
        &'a self,
    ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>>
       where K: 'a,
             V: 'a;
    fn close_with_timeout<'a>(
        &'a self,
        timeout: Duration,
    ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>>
       where K: 'a,
             V: 'a;
}
Expand description

A dyn-compatible Producer, for holding different producer implementations behind one type, e.g. Vec<Box<dyn DynProducer<K, V>>>.

Every Producer implements DynProducer automatically, and the trait is sealed, so it cannot be implemented directly: implement Producer instead. Each method forwards to the Producer method of the same name; asynchronous methods return the future boxed, which costs one allocation per call. dyn DynProducer<K, V> implements Producer in turn, so a boxed producer can be passed as &*boxed to generic code taking P: Producer<K, V> + ?Sized.

With both traits in scope a method call on a dyn DynProducer is ambiguous, since both define it; call it as DynProducer::send(&*producer, record), or import only one of the two traits.

use confluent_kafka::producer::{DynProducer, MockProducer};

let producers: Vec<Box<dyn DynProducer<String, String>>> = vec![
    Box::new(MockProducer::<String, String>::with_auto_complete(true)),
    Box::new(MockProducer::<String, String>::with_auto_complete(false)),
];
assert_eq!(2, producers.len());

Required Methods§

Source

fn init_transactions<'a>( &'a self, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>>
where K: 'a, V: 'a,

Source

fn begin_transaction(&self) -> Result<(), Error>

Source

fn send_offsets_to_transaction<'a>( &'a self, offsets: HashMap<TopicPartition, OffsetAndMetadata>, group_metadata: &'a dyn ConsumerGroupMetadata, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>>
where K: 'a, V: 'a,

Source

fn commit_transaction<'a>( &'a self, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>>
where K: 'a, V: 'a,

Source

fn abort_transaction<'a>( &'a self, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>>
where K: 'a, V: 'a,

Source

fn send<'a>( &'a self, record: ProducerRecord<K, V>, ) -> Pin<Box<dyn Future<Output = Result<KafkaFuture<RecordMetadata>, Error>> + Send + 'a>>
where K: 'a, V: 'a,

See Producer::send.

§Arguments
§Returns

As Producer::send.

§Errors

As Producer::send.

Source

fn send_with_callback<'a>( &'a self, record: ProducerRecord<K, V>, callback: Option<Box<dyn Callback>>, ) -> Pin<Box<dyn Future<Output = Result<KafkaFuture<RecordMetadata>, Error>> + Send + 'a>>
where K: 'a, V: 'a,

Source

fn flush<'a>( &'a self, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>>
where K: 'a, V: 'a,

Source

fn partitions_for<'a>( &'a self, topic: &'a str, ) -> Pin<Box<dyn Future<Output = Result<Vec<PartitionInfo>, Error>> + Send + 'a>>
where K: 'a, V: 'a,

Source

fn metrics(&self) -> MetricsView

Source

fn close<'a>( &'a self, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>>
where K: 'a, V: 'a,

Source

fn close_with_timeout<'a>( &'a self, timeout: Duration, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>>
where K: 'a, V: 'a,

Trait Implementations§

Source§

impl<K, V> Producer<K, V> for dyn DynProducer<K, V> + '_

Source§

fn init_transactions(&self) -> impl Future<Output = Result<(), Error>> + Send

Needs to be called before any other methods when the transactional.id is set in the configuration. Read more
Source§

fn begin_transaction(&self) -> Result<(), Error>

Should be called before the start of each new transaction. Note that prior to the first invocation of this method, you must invoke init_transactions exactly one time. Read more
Source§

async fn send_offsets_to_transaction( &self, offsets: HashMap<TopicPartition, OffsetAndMetadata>, group_metadata: &dyn ConsumerGroupMetadata, ) -> Result<(), Error>

Sends a list of specified offsets to the consumer group coordinator, and also marks those offsets as part of the current transaction. These offsets will be considered committed only if the transaction is committed successfully. Read more
Source§

fn commit_transaction(&self) -> impl Future<Output = Result<(), Error>> + Send

Commits the ongoing transaction. This method will flush any unsent records before actually committing the transaction. Read more
Source§

fn abort_transaction(&self) -> impl Future<Output = Result<(), Error>> + Send

Aborts the ongoing transaction. Any unflushed produce messages will be aborted when this call is made. Read more
Source§

fn send( &self, record: ProducerRecord<K, V>, ) -> impl Future<Output = Result<KafkaFuture<RecordMetadata>, Error>> + Send

Asynchronously send a record to a topic. Equivalent to send_with_callback(record, None). See send_with_callback for details. Read more
Source§

fn send_with_callback( &self, record: ProducerRecord<K, V>, callback: Option<Box<dyn Callback>>, ) -> impl Future<Output = Result<KafkaFuture<RecordMetadata>, Error>> + Send

Asynchronously send a record to a topic and invoke the provided callback when the send has been acknowledged. Read more
Source§

fn flush(&self) -> impl Future<Output = Result<(), Error>> + Send

Invoking this method makes all buffered records immediately available to send (even if linger.ms is greater than 0) and blocks on the completion of the requests associated with these records. The post-condition of flush() is that any previously sent record will have completed (e.g. its future is done and callbacks passed to Self::send_with_callback have been called). A request is considered completed when it is successfully acknowledged according to the acks configuration you have specified or else it results in an error. Read more
Source§

async fn partitions_for(&self, topic: &str) -> Result<Vec<PartitionInfo>, Error>

Get the partition metadata for the given topic. This can be used for custom partitioning. Read more
Source§

fn metrics(&self) -> MetricsView

Get the full set of internal metrics maintained by the producer. Read more
Source§

fn close(&self) -> impl Future<Output = Result<(), Error>> + Send

Close this producer. This method blocks until all previously sent requests complete. This method is equivalent to close_with_timeout(Duration::MAX). Read more
Source§

fn close_with_timeout( &self, timeout: Duration, ) -> impl Future<Output = Result<(), Error>> + Send

This method waits up to timeout for the producer to complete the sending of all incomplete requests. Read more

Implementors§

Source§

impl<K, V, P: Producer<K, V>> DynProducer<K, V> for P