Struct nats::jetstream::push_subscription::PushSubscription

source ·
pub struct PushSubscription(/* private fields */);
Expand description

A PushSubscription receives Messages published to specific NATS Subjects.

Implementations§

source§

impl PushSubscription

source

pub fn next(&self) -> Option<Message>

Get the next message non-protocol message, or None if the subscription has been unsubscribed or the connection closed.

§Example

if let Some(message) = subscription.next() {
    println!("Received {}", message);
}
source

pub fn try_next(&self) -> Option<Message>

Try to get the next non-protocol message, or None if no messages are present or if the subscription has been unsubscribed or the connection closed.

§Example
if let Some(message) = subscription.try_next() {
    println!("Received {}", message);
}
source

pub fn next_timeout(&self, timeout: Duration) -> Result<Message>

Get the next message, or a timeout error if no messages are available for timeout.

§Example
if let Ok(message) = subscription.next_timeout(std::time::Duration::from_secs(1)) {
    println!("Received {}", message);
}
source

pub fn messages(&self) -> Iter<'_>

Returns a blocking message iterator. Same as calling iter().

§Example
for message in subscription.messages() {
    println!("Received message {:?}", message);
}
source

pub fn iter(&self) -> Iter<'_>

Returns a blocking message iterator.

§Example
for message in subscription.iter() {}
source

pub fn try_iter(&self) -> TryIter<'_>

Returns a non-blocking message iterator.

§Example
for message in subscription.try_iter() {}
source

pub fn timeout_iter(&self, timeout: Duration) -> TimeoutIter<'_>

Returns a blocking message iterator with a time deadline for blocking.

§Example
for message in subscription.timeout_iter(std::time::Duration::from_secs(1)) {
    println!("Received message {:?}", message);
}
source

pub fn with_handler<F>(self, handler: F) -> Handler
where F: Fn(Message) -> Result<()> + Send + 'static,

Attach a closure to handle messages. This closure will execute in a separate thread. The result of this call is a Handler which can not be iterated and must be unsubscribed or closed directly to unregister interest. A Handler will not unregister interest with the server when drop(&mut self) is called.

§Example
context
    .subscribe("with_handler")?
    .with_handler(move |message| {
        println!("received {}", &message);
        Ok(())
    });
source

pub fn with_process_handler<F>(self, handler: F) -> Handler
where F: Fn(&Message) -> Result<()> + Send + 'static,

Attach a closure to process and acknowledge messages. This closure will execute in a separate thread.

The result of this call is a Handler which can not be iterated and must be unsubscribed or closed directly to unregister interest. A Handler will not unregister interest with the server when drop(&mut self) is called.

§Example
context
    .subscribe("with_process_handler")?
    .with_process_handler(|message| {
        println!("Received {}", &message);

        Ok(())
    });
source

pub fn process<R, F: Fn(&Message) -> Result<R>>(&mut self, f: F) -> Result<R>

Process and acknowledge a single message, waiting indefinitely for one to arrive.

Does not acknowledge the processed message if the closure returns an Err.

Example

let mut subscription = context.subscribe("process")?;
subscription.process(|message| {
    println!("Received message {:?}", message);

    Ok(())
})?;
source

pub fn process_timeout<R, F: Fn(&Message) -> Result<R>>( &mut self, timeout: Duration, f: F ) -> Result<R>

Process and acknowledge a single message, waiting up to timeout configured timeout before returning a timeout error.

Does not ack the processed message if the internal closure returns an Err.

Example

let mut subscription = context.subscribe("process_timeout")?;
subscription.process_timeout(std::time::Duration::from_secs(1), |message| {
    println!("Received message {:?}", message);

    Ok(())
})?;
source

pub fn consumer_info(&self) -> Result<ConsumerInfo>

Sends a request to fetch current information about the target consumer.

§Example
let subscription = context.subscribe("consumer_info")?;
let info = subscription.consumer_info()?;
source

pub fn unsubscribe(self) -> Result<()>

Unsubscribe a subscription immediately without draining. Use drain instead if you want any pending messages to be processed by a handler, if one is configured.

§Example
let subscription = context.subscribe("unsubscribe")?;
subscription.unsubscribe()?;
source

pub fn close(self) -> Result<()>

Close a subscription. Same as unsubscribe

Use drain instead if you want any pending messages to be processed by a handler, if one is configured.

§Example
let subscription = context.subscribe("close")?;
subscription.close()?;
source

pub fn drain(&self) -> Result<()>

Send an unsubscription and flush the connection, allowing any unprocessed messages to be handled by a Subscription

After the flush returns, we know that a round-trip to the server has happened after it received our unsubscription, so we shut down the subscriber afterwards.

A similar method exists on the Connection struct which will drain all subscriptions for the NATS client, and transition the entire system into the closed state afterward.

§Example
let mut subscription = context.subscribe("drain")?;

context.publish("drain", "foo")?;
context.publish("drain", "bar")?;
context.publish("drain", "baz")?;

subscription.drain()?;

assert!(subscription.next().is_some());

Trait Implementations§

source§

impl Clone for PushSubscription

source§

fn clone(&self) -> PushSubscription

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for PushSubscription

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a> IntoIterator for &'a PushSubscription

§

type Item = Message

The type of the elements being iterated over.
§

type IntoIter = Iter<'a>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Iter<'a>

Creates an iterator from a value. Read more
source§

impl IntoIterator for PushSubscription

§

type Item = Message

The type of the elements being iterated over.
§

type IntoIter = IntoIter

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

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> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

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

§

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>,

§

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.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more