pub struct InMemoryQueueHandle { /* private fields */ }
Expand description
An in-memory implementation of QueueHandle
.
§Example
§Exactly Once Mode
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
struct MyStruct {
field: String,
}
#[tokio::main]
async fn main() -> Result<()> {
let connection = InMemoryConnection::new(Serializer::default());
// Declare a queue
let handle = connection.declare_queue("my_queue", QueueOptions {
syndication_mode: SyndicationMode::ExactlyOnce,
delivery_mode: DeliveryMode::Persistent,
..Default::default()
}).await?;
// Publish a message
handle.publish(&MyStruct { field: "Hello, World!".to_string() }).await?;
// Consume the message
let mut consumer = handle.declare_consumer::<MyStruct>("my_consumer").await?;
if let Some((message, acker)) = consumer.next().await {
acker.ack().await?;
assert_eq!(message, MyStruct { field: "Hello, World!".to_string() });
}
}
§Broadcast Mode
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
struct MyStruct {
field: String,
}
#[tokio::main]
async fn main() -> Result<()> {
let connection = InMemoryConnection::new(Serializer::default());
// Declare a queue
let handle = connection.declare_queue("my_queue", QueueOptions {
syndication_mode: SyndicationMode::Broadcast,
..Default::default()
}).await?;
let mut consumer_1 = handle.declare_consumer::<MyStruct>("consumer_1").await?;
let mut consumer_2 = handle.declare_consumer::<MyStruct>("consumer_2").await?;
// Consume the message in the first consumer
let consumer_task_1 = tokio::spawn(async move {
let (message, acker) = consumer_1.next().await.unwrap();
acker.ack().await.unwrap();
message
});
// Consume the message in the second consumer
let consumer_task_2 = tokio::spawn(async move {
let (message, acker) = consumer_2.next().await.unwrap();
acker.ack().await.unwrap();
message
});
// Publish a message
handle.publish(&MyStruct { field: "Hello, World!".to_string() }).await?;
assert_eq!(consumer_task_1.await.unwrap(), MyStruct { field: "Hello, World!".to_string() });
assert_eq!(consumer_task_2.await.unwrap(), MyStruct { field: "Hello, World!".to_string() });
}
Implementations§
Source§impl InMemoryQueueHandle
impl InMemoryQueueHandle
pub fn new(serializer: Serializer, options: QueueOptions) -> Self
Trait Implementations§
Source§impl Clone for InMemoryQueueHandle
impl Clone for InMemoryQueueHandle
Source§fn clone(&self) -> InMemoryQueueHandle
fn clone(&self) -> InMemoryQueueHandle
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Default for InMemoryQueueHandle
impl Default for InMemoryQueueHandle
Source§fn default() -> InMemoryQueueHandle
fn default() -> InMemoryQueueHandle
Returns the “default value” for a type. Read more
Source§impl QueueHandle for InMemoryQueueHandle
impl QueueHandle for InMemoryQueueHandle
type Acker = NoopAcker
type Consumer<PayloadTarget: Serializable> = InMemoryConsumer<PayloadTarget>
type Publisher<PayloadTarget: Serializable> = InMemoryPublisher<PayloadTarget>
fn publisher<PayloadTarget: Serializable>( &self, ) -> Self::Publisher<PayloadTarget>
Source§fn declare_consumer<'life0, 'life1, 'async_trait, PayloadTarget>(
&'life0 self,
consumer_name: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<Self::Consumer<PayloadTarget>>> + Send + 'async_trait>>where
PayloadTarget: 'async_trait + Serializable,
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn declare_consumer<'life0, 'life1, 'async_trait, PayloadTarget>(
&'life0 self,
consumer_name: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<Self::Consumer<PayloadTarget>>> + Send + 'async_trait>>where
PayloadTarget: 'async_trait + Serializable,
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Declare a queue consumer.
fn publish<'life0, 'life1, 'async_trait, PayloadTarget>(
&'life0 self,
payload: &'life1 PayloadTarget,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self::Publisher<PayloadTarget>: Send,
PayloadTarget: 'async_trait + Serializable,
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Auto Trait Implementations§
impl Freeze for InMemoryQueueHandle
impl !RefUnwindSafe for InMemoryQueueHandle
impl Send for InMemoryQueueHandle
impl Sync for InMemoryQueueHandle
impl Unpin for InMemoryQueueHandle
impl !UnwindSafe for InMemoryQueueHandle
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more