// Port of https://www.rabbitmq.com/tutorials/tutorial-three-python.html. Start this // example in one shell, then run the pubsub_emit_log example in another. use amiquip::{ Connection, ConsumerMessage, ConsumerOptions, ExchangeDeclareOptions, ExchangeType, FieldTable, QueueDeclareOptions, Result, }; fn main() -> Result<()> { env_logger::init(); // Open connection. let mut connection = Connection::insecure_open("amqp://guest:guest@localhost:5672")?; // Open a channel - None says let the library choose the channel ID. let channel = connection.open_channel(None)?; // Declare the fanout exchange we will bind to. let exchange = channel.exchange_declare( ExchangeType::Fanout, "logs", ExchangeDeclareOptions::default(), )?; // Declare the exclusive, server-named queue we will use to consume. let queue = channel.queue_declare( "", QueueDeclareOptions { exclusive: true, ..QueueDeclareOptions::default() }, )?; println!("created exclusive queue {}", queue.name()); // Bind our queue to the logs exchange. queue.bind(&exchange, "", FieldTable::new())?; // Start a consumer. Use no_ack: true so the server doesn't wait for us to ack // the messages it sends us. let consumer = queue.consume(ConsumerOptions { no_ack: true, ..ConsumerOptions::default() })?; println!("Waiting for logs. Press Ctrl-C to exit."); for (i, message) in consumer.receiver().iter().enumerate() { match message { ConsumerMessage::Delivery(delivery) => { let body = String::from_utf8_lossy(&delivery.body); println!("({:>3}) {}", i, body); } other => { println!("Consumer ended: {:?}", other); break; } } } connection.close() }