Struct Consumer

Source
pub struct Consumer<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

Implementations§

Source§

impl<T, F> Consumer<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, Error>

Creates a new Consumer from group id and bootstrap servers

§Arguments
  • group_id - The group_id of the consumer
  • bootstrap_servers - The comma separated bootstrap servers
§Example
use simple_kafka::{Consumer};
let consumer = Consumer::from("group_id", "localhost:9092").unwrap();
Source

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

Creates a new Consumer from consumer options

§Arguments
  • options - A ConsumerOptions struct that holds the consumer options
§Example
use simple_kafka::{Consumer, 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 = Consumer::with_options(options).unwrap();
Source

pub async fn subscribe_to_topic<H>(&mut self, topic: &str, handler: H)
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 mut consumer = Consumer::from("group_id", "localhost:9092");
let handler = consumer.subscribe_to_topic("topic".to_string(), |data: Data, medatad: Metadata| async move {
   info!("data: {:?}, metadata: {:?}", data, medatad);
});
handler.await;

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<T, F> !UnwindSafe for Consumer<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.