Checkout the project: https://github.com/gftea/amqprs
This is a fun side project for learning, and implement my current knowledge in Rustlang.
To use the library you have to first create the event queues, and their specific handlers.
Every handler must a function that receives the type String
only.
To run a local instance of RabbitMQ, use the following command:
rabbitmq-server
To create a smart-publisher subscriber start by importing the required types.
use tracing::{debug, Level};
use alicemq::clients::consumer_client::ConsumerManager;
use alicemq::consumers::base_consumer::BaseConsumer;
use tracing_subscriber::FmtSubscriber;
Currently, non-blocking async consumers, are supported. To define a handler follow the subsequent structure:
async fn my_callback(data: Vec<u8>) {
debug!("Received data: {:?}", String::from_utf8(data));
}
fn set_tracing_subscriber() {
let subscriber = FmtSubscriber::builder()
.with_max_level(Level::TRACE)
.finish();
tracing::subscriber::set_global_default(subscriber)
.expect("setting default subscriber failed");
}
To create a consumer, use the consumer manager, and define if it's long-lived. Define a queue and its respective callback handler.
#[tokio::main]
async fn main() {
let _ = set_tracing_subscriber();
let mut _manager: ConsumerManager = ConsumerManager::new_instance()
.connect().await;
_manager.set_queue("test_queue", BaseConsumer::new(my_callback), None).await;
_manager.set_queue("another_test_queue", BaseConsumer::new(my_callback), None).await;
_manager.run(true).await;
}
To create a smart publisher simply create an instance of a smart publisher. Supported data to deliver, strings only.
use tracing::Level;
use tracing_subscriber::FmtSubscriber;
use alicemq::clients::publisher_client::Publisher;
fn set_tracing_subscriber() {
let subscriber = FmtSubscriber::builder()
.with_max_level(Level::TRACE)
.finish();
tracing::subscriber::set_global_default(subscriber)
.expect("setting default subscriber failed");
}
#[tokio::main]
async fn main() {
let _ = set_tracing_subscriber();
for i in 1..10 {
let _ = Publisher::send_message(
format!("This message contains the id {}", i), String::from("test_queue")
).await;
}
}
To run any of the examples in the folder, run the following command:
cargo run --example my_example