Struct PausableConsumer

Source
pub struct PausableConsumer<T, F>
where T: for<'b> Deserialize<'b>, F: Future<Output = ()> + Send + Sync + 'static,
{ /* private fields */ }
Expand description

A Consumer that can be used to consume messages from kafka and has the ability to pause and resume

Implementations§

Source§

impl<T, F> PausableConsumer<T, F>
where T: for<'a> Deserialize<'a>, F: Future<Output = ()> + Send + Sync + 'static,

Source

pub fn from( group_id: &str, bootstrap_servers: &str, ) -> Result<(Self, Arc<Mutex<bool>>), Error>

Creates a new PausableConsumer from a group_id and bootstrap_servers

§Arguments
  • group_id - The group_id of the consumer
  • bootstrap_servers - The comma separated bootstrap servers
§Returns

A tuple of PausableConsumer and a Arc<Mutex>. The Arc<Mutex> is used to pause and resume the consumer

§Example
use simple_kafka::{PausableConsumer};

let (consumer, paused) = PausableConsumer::from("group_id", "localhost:9092").unwrap();
Source

pub fn with_options( options: ConsumerOptiopns<'_>, ) -> Result<(Self, Arc<Mutex<bool>>), Error>

Creates a new PausedConsumer from consumer options

§Arguments
  • options - A ConsumerOptions struct that holds the consumer options
§Returns

A tuple of PausableConsumer and a Arc<Mutex>. The Arc<Mutex> is used to pause and resume the consumer

§Example
use simple_kafka::{PausableConsumer, ConsumerOptiopns};

let options = ConsumerOptiopns {
    bootstrap_servers: "localhost:9092".to_string(),
    group_id: "group_id".to_string(),
    session_timeout_ms: "6000".to_string(),
    enable_auto_commit: true,
    enable_partition_eof: false,
};
let consumer = PausableConsumer::with_options(options)?;
Source

pub async fn subscribe_to_topic<H>( &mut self, topic: &str, handler: H, ) -> Result<(), Error>
where H: Fn(KafkaResult<T>) -> F,

Subscribe to a given topic and calls the given function for each message

§Arguments
  • topic - The topic to subscribe to
  • handler - The handler function that will be called for each message for the give topic
§Example
use simple_kafka::{Consumer};
#[derive(Serialize, Deserialize, Debug)]
 struct Data {
    attra_one: String,
    attra_two: i8,
}

let consumer = Consumer::from("group_id", "localhost:9092");
let handler_1 = | data: Data, metadata: Metadata| async move {
    println!("Handler One ::: data: {:?}, metadata: {:?}", data, metadata);
};
consumer.subscribe_to_topic("topic_1".to_string(), handler_1).await.unwrap();
Source

pub async fn pause(&self)

Pause the consumer. Consumer will not request new messages but will keep the connection to the broker alive This is useful when you want to pause the consumer for a while and resume it later without having to reconnect to the broker

§Example
use simple_kafka::{Consumer};

#[tokio::main]
async fn main() {
    let (mut consumer, is_runnig)  = PausableConsumer::from("group_id", "localhost:9092").unwrap();
    let handler = consumer.subscribe_to_topic("topic".to_string(), |data: Data, medatad: Metadata| async move {
        info!("data: {:?}, metadata: {:?}", data, medatad);
    });
    consumer.pause().await;
}
Source

pub async fn resume(&self)

Resume the consumer

§Example
use simple_kafka::{Consumer};

#[tokio::main]
async fn main() {
    let (mut consumer, is_runnig)  = PausableConsumer::from("group_id", "localhost:9092").unwrap();
    let handler = consumer.subscribe_to_topic("topic".to_string(), |data: Data, medatad: Metadata| async move {
        info!("data: {:?}, metadata: {:?}", data, medatad);
    });
    consumer.pause().await;
    consumer.resume().await;
}

Auto Trait Implementations§

§

impl<T, F> Freeze for PausableConsumer<T, F>

§

impl<T, F> !RefUnwindSafe for PausableConsumer<T, F>

§

impl<T, F> !Send for PausableConsumer<T, F>

§

impl<T, F> !Sync for PausableConsumer<T, F>

§

impl<T, F> Unpin for PausableConsumer<T, F>

§

impl<T, F> !UnwindSafe for PausableConsumer<T, F>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.