Crate rsmq_async_lite[−][src]
This is a port of the nodejs Redis Simple Message Queue package. It is a 1-to-1 conversion using async.
use rsmq_async::{Rsmq, RsmqError}; let mut rsmq = Rsmq::<String>::new(Default::default()).await?; let message = rsmq.receive_message("myqueue", None).await?; if let Some(message) = message { rsmq.delete_message("myqueue", &message.id).await?; }
Main object documentation in: Rsmq
Realtime
When initializing RSMQ you can enable the realtime PUBLISH for
new messages. On every new message that gets sent to RSQM via sendMessage
a
Redis PUBLISH will be issued to {rsmq.ns}rt:{qname}
.
Example for RSMQ with default settings:
- The queue
testQueue
already contains 5 messages. - A new message is being sent to the queue
testQueue
. - The following Redis command will be issued:
PUBLISH rsmqrt:testQueue 6
How to use the realtime option
Besides the PUBLISH when a new message is sent to RSMQ nothing else will happen.
Your app could use the Redis SUBSCRIBE command to be notified of new messages
and issue a receiveMessage
then. However make sure not to listen with multiple
workers for new messages with SUBSCRIBE to prevent multiple simultaneous
receiveMessage
calls.
Guarantees
If you want to implement “at least one delivery” guarantee, you need to receive the messages using “receive_message” and then, once the message is successfully processed, delete it with “delete_message”.
use rsmq_async::Rsmq; let mut rsmq = Rsmq::<String>::new(Default::default()) .await .expect("connection failed"); rsmq.create_queue("myqueue", None, None, None) .await .expect("failed to create queue"); rsmq.send_message("myqueue", &"testmessage".to_string(), None) .await .expect("failed to send message"); let message = rsmq .receive_message("myqueue", None) .await .expect("cannot receive message"); if let Some(message) = message { rsmq.delete_message("myqueue", &message.id).await; }
Executor compatibility
Since version 0.16 redis dependency supports tokio and async_std executors.
By default it will guess what you are using when creating the connection.
You can check redis
Cargo.tolm
for the flags async-std-comp
and tokio-comp
Structs
Rsmq | THe main object of this library. Creates/Handles the redis connection and contains all the methods |
RsmqMessage | A new RSMQ message. You will get this when using pop_message or receive_message methods |
RsmqOptions | Options for creating a new RSMQ instance. |
RsmqQueueAttributes | Struct defining a queue. They are set on “create_queue” and “set_queue_attributes” |
Enums
RsmqError | RsmqError |
Type Definitions
RsmqResult | RsmqResult |