RabbitMQ Service
A library to accelerate RabbitMQ deployment and management in Rust applications. This library provides a simple API for interacting with RabbitMQ, including queue creation, message publishing, and receiving responses.
Features
- Connect to RabbitMQ using a specified URI.
- Declare two queues:
<queue_name>_requestsand<queue_name>_responses. - Publish messages to the
_requestsqueue. - Receive responses from the
_responsesqueue. - Message serialization and deserialization with
serde. - Automatic message acknowledgment.
Installation
To include this crate in your project, add it to your Cargo.toml:
[]
= { = "https://github.com/SentineLLM-1/rabbitmq_service" }
Usage
Below is a basic example of how to use the RabbitMQService in your application.
Example
use RabbitMQService;
use tokio;
async
API
new(uri: &str, queue_name: &str) -> Result<Self, Error>
Constructs a new RabbitMQService instance connected to the specified RabbitMQ URI and initializes the two required queues: <queue_name>_requests and <queue_name>_responses.
-
Arguments:
uri: The URI of the RabbitMQ server.queue_name: The base name for the request/response queues.
-
Returns: A
ResultwithRabbitMQServiceor anErrorif the connection or queue declaration fails.
send_message(&self, queue_name: &str, message: String) -> Result<(), Error>
Sends a serialized message to the specified _requests queue.
-
Arguments:
queue_name: The base name of the queue (will append_requests).message: The message content to send.
-
Returns: A
Resultindicating whether the message was successfully sent.
receive_response(&self, queue_name: String) -> Result<String, Error>
Receives a response from the specified _responses queue.
-
Arguments:
queue_name: The base name of the queue (will append_responses).
-
Returns: A
Resultcontaining the received response as aString, or an error if the response cannot be received or processed.
Error Handling
The library uses the Error type to handle various issues that may arise, such as connection errors, queue declaration errors, or message processing errors. Errors include but are not limited to:
- Connection failures.
- Queue declaration failures.
- Message serialization/deserialization errors.
- Timeouts or missing messages in the response queue.
Dependencies
rabbit_mqrfor RabbitMQ management.serdeandserde_jsonfor message serialization and deserialization.uuidfor generating unique message IDs.tokiofor asynchronous runtime.
License
This project is licensed under the Apache-2.0 License.
Contributing
Contributions are welcome! Please feel free to fork the repository, make changes, and submit pull requests.
Author
- Femure maxime.femery@gmail.com
Feel free to modify or expand upon the README as needed!