# 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`:
```toml
[dependencies]
rabbitmq_service = { git = "https://github.com/SentineLLM-1/rabbitmq_service" }
```
## Usage
Below is a basic example of how to use the `RabbitMQService` in your application.
### Example
```rust
use rabbitmq_service::RabbitMQService;
use tokio;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a new RabbitMQ service connected to your RabbitMQ server
let service = RabbitMQService::new("amqp://localhost", "my_queue").await?;
// Send a message to the requests queue
service.send_message("my_queue", "Hello, RabbitMQ!".into()).await?;
// Wait for a response from the responses queue
let response = service.receive_response("my_queue".into()).await?;
println!("Received response: {}", response);
Ok(())
}
```
### 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` with `RabbitMQService` or an `Error` 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 a `String`, 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` and `serde_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!