ZIMQ
Table of Contents
Quick-pitch
Tokio-based asynchronous in-process message queue.
This library prioritizes simplicity and ease of use. Future development will focus solely on bug fixes and dependency updates, with no new features planned.
Features
- Lightweight, high‑performance, simple to use, and in‑process.
- Supports both immediate and delayed messages.
- Supports custom IDs for delayed messages. If a message with the same ID has not yet been consumed, subsequent attempts to send a message with that ID will immediately return an error.
- Automatically reloads unconsumed delayed messages after an application restart.
- Supports graceful shutdown, waiting for all immediate or matured delayed messages in the queue to be processed before exiting.
Quick-start
Cargo Install
cargo add zimq
Basic Example
use std::collections::HashMap;
use std::time::Duration;
use zimq::message::Message;
#[tokio::main]
async fn main() {
zimq::init("zimq").await;
zimq::subscribe("user", user_consume).await;
zimq::start().await;
let mut data = HashMap::new();
data.insert("id".to_string(), "123456".to_string());
if let Err(err) = zimq::send_immediate("user", data.clone()).await {
println!("{}", err);
}
if let Err(err) = zimq::send_delay("user", data.clone(), Duration::from_secs(5)).await {
println!("{}", err);
}
let msgid = "msgid123".to_string();
for _ in 0..2 {
if let Err(err) = zimq::send_delay_ex("user", msgid.clone(), data.clone(), Duration::from_secs(10)).await {
println!("{}", err);
}
}
tokio::time::sleep(Duration::from_secs(11)).await;
zimq::cleanup().await;
}
async fn user_consume(msg: Message) {
println!("msg: {:?}", msg);
if msg.msgid == "msgid123".to_string() {
tokio::time::sleep(Duration::from_secs(5)).await;
}
}