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>_requests
and<queue_name>_responses
. - Publish messages to the
_requests
queue. - Receive responses from the
_responses
queue. - 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
Result
withRabbitMQService
or anError
if 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
Result
indicating 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
Result
containing 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_mqr
for RabbitMQ management.serde
andserde_json
for message serialization and deserialization.uuid
for generating unique message IDs.tokio
for 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!