use std::io;
use std::time::Duration;
use ruststream::runtime::{App, AppInfo, HandlerResult, Out, RustStream};
use ruststream::{Headers, IncomingMessage, OutgoingMessage, Publisher, RequestReply, subscriber};
use ruststream_amqp::{AmqpAddress, AmqpBroker, AmqpPublish, AmqpPublisher};
#[subscriber(AmqpAddress::queue("greeter"), raw)]
async fn greet(name: &[u8], ctx: &mut Context<'_>, Out(out): Out<AmqpPublisher>) -> HandlerResult {
let Some(reply_to) = ctx.headers().reply_to().map(str::to_owned) else {
return HandlerResult::drop();
};
let mut headers = Headers::new();
if let Some(correlation_id) = ctx.headers().correlation_id() {
headers.insert("correlation-id", correlation_id.to_owned());
}
let payload = format!("hello, {}", String::from_utf8_lossy(name));
let reply = OutgoingMessage::new(&reply_to, payload.as_bytes()).with_headers(headers);
if out.publish(reply).await.is_err() {
return HandlerResult::retry();
}
HandlerResult::Ack
}
#[ruststream::app]
fn app() -> impl App {
RustStream::new(AppInfo::new("request-reply", "0.1.0")).with_broker(
AmqpBroker::new("amqp://artemis:artemis@localhost:5672")
.container_id("request-reply-example"),
|b| {
b.include(greet).publisher(AmqpPublish);
b.after_startup(AmqpPublish, async move |publisher| -> io::Result<()> {
let reply = publisher
.request(
OutgoingMessage::new("greeter", b"world".as_slice()),
Duration::from_secs(5),
)
.await
.map_err(io::Error::other)?;
println!("reply: {}", String::from_utf8_lossy(reply.payload()));
Ok(())
});
},
)
}