Struct stan::Subscription [−][src]
NATS Streaming subscription
Example:
use nats; use std::{io, str::from_utf8, time}; fn main() -> io::Result<()> { let nc = nats::connect("nats://127.0.0.1:4222")?; let sc = stan::connect(nc, "test-cluster", "rust-client-1")?; let sub1 = sc .subscribe("foo", Default::default())? .with_handler(|msg| { println!("sub1 got {:?}", from_utf8(&msg.data)); msg.ack()?; println!("manually acked!"); Ok(()) }); sc.subscribe("foo", Default::default())? .with_handler(|msg| { println!("sub 2 got {:?}", from_utf8(&msg.data)); Ok(()) }); for msg in sc.subscribe("foo", Default::default())?.messages() { println!("sub 3 got {:?}", from_utf8(&msg.data)); msg.ack()?; } for msg in sc .subscribe("foo", Default::default())? .timeout_iter(time::Duration::from_secs(1)) { println!("sub 4 got {:?}", from_utf8(&msg.data)); msg.ack()?; } Ok(()) }
Implementations
impl Subscription
[src]
pub fn messages(&self) -> Iter<'_>ⓘ
[src]
Returns a blocking message iterator.
Same as calling iter()
.
for msg in sc.subscribe("foo", Default::default())?.messages() { println!("received: {:?}", from_utf8(&msg.data)); msg.ack()?; }
pub fn iter(&self) -> Iter<'_>ⓘ
[src]
Returns a blocking message iterator.
for msg in sc.subscribe("foo", Default::default())?.iter() { println!("received: {:?}", from_utf8(&msg.data)); msg.ack()?; }
pub fn try_iter(&self) -> TryIter<'_>ⓘ
[src]
Returns a non-blocking message iterator.
for msg in sc.subscribe("foo", Default::default())?.try_iter() { println!("received: {:?}", from_utf8(&msg.data)); msg.ack()?; }
pub fn timeout_iter(&self, timeout: Duration) -> TimeoutIter<'_>ⓘNotable traits for TimeoutIter<'a>
impl<'a> Iterator for TimeoutIter<'a> type Item = Message;
[src]
Notable traits for TimeoutIter<'a>
impl<'a> Iterator for TimeoutIter<'a> type Item = Message;
Returns a blocking message iterator with a time deadline for blocking.
for msg in sc .subscribe("foo", Default::default())? .timeout_iter(time::Duration::from_secs(1)) { println!("received: {:?}", from_utf8(&msg.data)); msg.ack()?; }
pub fn with_handler<F>(self, handler: F) -> Handler where
F: Fn(&Message) -> Result<()> + Send + 'static,
[src]
F: Fn(&Message) -> Result<()> + Send + 'static,
Process subscription messages in a separate thread. Messages are automatically acked unless the handler returns an error. Messages can also be manually acked by calling msg.ack().
Examples:
Automatic ack:
sc.subscribe("foo", Default::default())? .with_handler(|msg| { println!("{:?}", from_utf8(&msg.data)); Ok(()) });
Manual ack:
sc.subscribe("foo", Default::default())? .with_handler(|msg| { println!("{:?}", from_utf8(&msg.data)); msg.ack()?; println!("this happens after the ack"); Ok(()) });
pub fn next(&self) -> Option<Message>
[src]
Get the next message with blocking, or None if the subscription has been closed Note: the message needs to be manually acked!
use nats; use std::{io, str::from_utf8}; fn main() -> io::Result<()> { let nc = nats::connect("nats://127.0.0.1:4222")?; let sc = stan::connect(nc, "test-cluster", "rust-client-1")?; sc.publish("foo", "hello from rust 1")?; let sub = sc.subscribe("foo", Default::default())?; if let Some(msg) = sub.next() { println!("received: {:?}", from_utf8(&msg.data)); msg.ack()? } Ok(()) }
pub fn try_next(&self) -> Option<Message>
[src]
Get the next message without blocking, or None if none available Note: the message needs to be manually acked!
use nats; use std::{io, str::from_utf8}; fn main() -> io::Result<()> { let nc = nats::connect("nats://127.0.0.1:4222")?; let sc = stan::connect(nc, "test-cluster", "rust-client-1")?; sc.publish("foo", "hello from rust 1")?; let sub = sc.subscribe("foo", Default::default())?; if let Some(msg) = sub.try_next() { println!("received: {:?}", from_utf8(&msg.data)); msg.ack()? } Ok(()) }
pub fn next_timeout(&self, timeout: Duration) -> Result<Message>
[src]
Get the next message without blocking, or None if none available Note: the message needs to be manually acked!
use nats; use std::{io, str::from_utf8}; fn main() -> io::Result<()> { let nc = nats::connect("nats://127.0.0.1:4222")?; let sc = stan::connect(nc, "test-cluster", "rust-client-1")?; sc.publish("foo", "hello from rust 1")?; let sub = sc.subscribe("foo", Default::default())?; if let Ok(msg) = sub.next_timeout(std::time::Duration::from_secs(1)) { println!("received: {:?}", from_utf8(&msg.data)); msg.ack()? } Ok(()) }
Trait Implementations
impl Clone for Subscription
[src]
fn clone(&self) -> Subscription
[src]
pub fn clone_from(&mut self, source: &Self)
1.0.0[src]
Auto Trait Implementations
impl !RefUnwindSafe for Subscription
impl Send for Subscription
impl Sync for Subscription
impl Unpin for Subscription
impl !UnwindSafe for Subscription
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Same<T> for T
type Output = T
Should always be Self
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,