Checkout the project: https://github.com/gftea/amqprs
To use the library you have to first create the event queues, and their specific handlers. Events are expected to be of type string, and handlers implementations of BaseCallbacks.
To run a local instance of RabbitMQ, use the following command:
zsh
rabbitmq-server
To create a smart-publisher subscriber start by importing the required types.
rust
use tokio;
use alicemq::consumer::{Consumer};
use alicemq::callback::{BaseCallback};
The BaseCallback defines a parameter to determine if manual ack should be implemented.
```rust
async fn main() -> Result<(), Box
let new_event = "my_custom_event".to_string();
let new_callback = BaseCallbackConsumer::new(false);
Ok(())
} ```
To set up a basic consumer, connect to a node, set the queue manager then add the created events, and their handlers.
````rust
async fn main() -> Result<(), Box
//Have the consumer running and in scope, otherwise It'll drop active connections.
consumer
.start_consumer()
.await?;
} ```` The following code, will create the queues on a rabbitMQ node, no_ack.
```rust use alicemq::publisher::Publisher;
async fn main() { let _ = Publisher::new() .connect() .await.unwrap() .build() .unwrap() .sendmessage("testevent".tostring(), data.tostring()).await; } ```
To handle all the incoming messages in a specific manner, you can do an implementation
of the AsyncConsumer
, and pass it as an argument when setting an event callback.
```rust use std::str; use amqprs::channel::{BasicAckArguments, Channel}; use amqprs::consumer::AsyncConsumer; use amqprs::{BasicProperties, Deliver}; use asynctrait::asynctrait; use tracing::info;
pub struct BaseCallbackConsumer { pub no_ack: bool }
impl BaseCallbackConsumer { pub fn new(noack: bool) -> Self { Self { noack } } }
impl AsyncConsumer for BaseCallbackConsumer {
async fn consume(
&mut self,
channel: &Channel,
deliver: Deliver,
basicproperties: BasicProperties,
_content: Vec
info!("got message {:?}", std::str::from_utf8(&_content));
if !self.no_ack {
let args = BasicAckArguments::new(deliver.delivery_tag(), false);
channel.basic_ack(args).await.unwrap();
}
}
}
```
To run any of the examples in the folder, run the following command:
zsh
cargo run --example my_example