| Crates.io | amq-rpc |
| lib.rs | amq-rpc |
| version | 0.1.0 |
| created_at | 2025-06-15 13:41:42.591334+00 |
| updated_at | 2025-06-15 13:41:42.591334+00 |
| description | RabbitMQ RPC library |
| homepage | https://github.com/UseBundle/amq-rpc |
| repository | https://github.com/UseBundle/amq-rpc.git |
| max_upload_size | |
| id | 1713315 |
| size | 82,053 |
Lightweight RPC library for RabbitMQ in Rust.
Add to your Cargo.toml:
[dependencies]
amq-rpc = "0.1.0"
use serde_json::Value;
use async_trait::async_trait;
use amq_rpc::{AmqpConnection, RpcServer, CommandHandler, RpcResult};
struct MathHandler;
#[async_trait]
impl CommandHandler for MathHandler {
async fn handle(&self, command: &str, args: Vec<Value>) -> RpcResult<Value> {
match command {
"add" => {
let a = args[0].as_f64().unwrap_or(0.0);
let b = args[1].as_f64().unwrap_or(0.0);
Ok(Value::Number(serde_json::Number::from_f64(a + b).unwrap()))
}
_ => Err(amq_rpc::RpcError::InvalidCommand {
message: format!("Unknown command: {}", command),
}),
}
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let connection = AmqpConnection::new("amqp://guest:guest@localhost:5672");
let server = RpcServer::new(connection, "math_queue".to_string());
server.register_handler("add", MathHandler).await;
server.start().await?;
tokio::signal::ctrl_c().await?;
server.close().await?;
Ok(())
}
use serde_json::json;
use amq_rpc::{AmqpConnection, RpcClient};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let connection = AmqpConnection::new("amqp://guest:guest@localhost:5672");
let client = RpcClient::new(connection, Some(30000));
client.start().await?;
let result = client.send_command(
"math_queue",
"add",
vec![json!(5), json!(3)]
).await?;
println!("Result: {}", result);
client.close().await?;
Ok(())
}
Full examples are available in the examples/ directory:
examples/server.rs - RPC server exampleexamples/client.rs - RPC client exampleMIT