|
| 1 | +use lapin::{ |
| 2 | + message::DeliveryResult, options::*, publisher_confirm::Confirmation, types::FieldTable, |
| 3 | + BasicProperties, Connection, ConnectionProperties, |
| 4 | +}; |
| 5 | +use tracing::info; |
| 6 | + |
| 7 | +fn main() { |
| 8 | + if std::env::var("RUST_LOG").is_err() { |
| 9 | + std::env::set_var("RUST_LOG", "info"); |
| 10 | + } |
| 11 | + |
| 12 | + tracing_subscriber::fmt::init(); |
| 13 | + |
| 14 | + let addr = std::env::var("AMQP_ADDR").unwrap_or_else(|_| "amqp://127.0.0.1:5672/%2f".into()); |
| 15 | + let recovery_config = lapin::experimental::RecoveryConfig::default().auto_recover_channels(); |
| 16 | + |
| 17 | + async_global_executor::block_on(async { |
| 18 | + let conn = Connection::connect( |
| 19 | + &addr, |
| 20 | + ConnectionProperties::default().with_experimental_recovery_config(recovery_config), |
| 21 | + ) |
| 22 | + .await |
| 23 | + .expect("connection error"); |
| 24 | + |
| 25 | + info!("CONNECTED"); |
| 26 | + |
| 27 | + { |
| 28 | + let channel1 = conn.create_channel().await.expect("create_channel"); |
| 29 | + let channel2 = conn.create_channel().await.expect("create_channel"); |
| 30 | + channel1 |
| 31 | + .confirm_select(ConfirmSelectOptions::default()) |
| 32 | + .await |
| 33 | + .expect("confirm_select"); |
| 34 | + channel1 |
| 35 | + .queue_declare( |
| 36 | + "recover-test", |
| 37 | + QueueDeclareOptions::default(), |
| 38 | + FieldTable::default(), |
| 39 | + ) |
| 40 | + .await |
| 41 | + .expect("queue_declare"); |
| 42 | + |
| 43 | + info!("will consume"); |
| 44 | + let channel = channel2.clone(); |
| 45 | + channel2 |
| 46 | + .basic_consume( |
| 47 | + "recover-test", |
| 48 | + "my_consumer", |
| 49 | + BasicConsumeOptions::default(), |
| 50 | + FieldTable::default(), |
| 51 | + ) |
| 52 | + .await |
| 53 | + .expect("basic_consume") |
| 54 | + .set_delegate(move |delivery: DeliveryResult| { |
| 55 | + let channel = channel.clone(); |
| 56 | + async move { |
| 57 | + info!(message=?delivery, "received message"); |
| 58 | + if let Ok(Some(delivery)) = delivery { |
| 59 | + delivery |
| 60 | + .ack(BasicAckOptions::default()) |
| 61 | + .await |
| 62 | + .expect("basic_ack"); |
| 63 | + if &delivery.data[..] == b"after" { |
| 64 | + channel |
| 65 | + .basic_cancel("my_consumer", BasicCancelOptions::default()) |
| 66 | + .await |
| 67 | + .expect("basic_cancel"); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + info!("will publish"); |
| 74 | + let confirm = channel1 |
| 75 | + .basic_publish( |
| 76 | + "", |
| 77 | + "recover-test", |
| 78 | + BasicPublishOptions::default(), |
| 79 | + b"before", |
| 80 | + BasicProperties::default(), |
| 81 | + ) |
| 82 | + .await |
| 83 | + .expect("basic_publish") |
| 84 | + .await |
| 85 | + .expect("publisher-confirms"); |
| 86 | + assert_eq!(confirm, Confirmation::Ack(None)); |
| 87 | + |
| 88 | + info!("before fail"); |
| 89 | + assert!(channel1 |
| 90 | + .queue_declare( |
| 91 | + "fake queue", |
| 92 | + QueueDeclareOptions { |
| 93 | + passive: true, |
| 94 | + ..QueueDeclareOptions::default() |
| 95 | + }, |
| 96 | + FieldTable::default(), |
| 97 | + ) |
| 98 | + .await |
| 99 | + .is_err()); |
| 100 | + info!("after fail"); |
| 101 | + |
| 102 | + info!("publish after"); |
| 103 | + let confirm = channel1 |
| 104 | + .basic_publish( |
| 105 | + "", |
| 106 | + "recover-test", |
| 107 | + BasicPublishOptions::default(), |
| 108 | + b"after", |
| 109 | + BasicProperties::default(), |
| 110 | + ) |
| 111 | + .await |
| 112 | + .expect("basic_publish") |
| 113 | + .await |
| 114 | + .expect("publisher-confirms"); |
| 115 | + assert_eq!(confirm, Confirmation::Ack(None)); |
| 116 | + } |
| 117 | + |
| 118 | + conn.run().expect("conn.run"); |
| 119 | + }); |
| 120 | +} |
0 commit comments