代码:
using (var conn = RabbitmqHelper.GetConnection()) { using (var channel = conn.CreateModel()) { //这两个事件如果是using写法要写到创建交换器和队列并且进行绑定之前,否则不会触发 channel.BasicNacks += (sender, e) => { //生产者发送消息到broker(服务器)后失败被生产者的listener监听到,就走无应答方法 Console.WriteLine(" --no ack-- "); }; channel.BasicAcks += (sender, e) => { //有应答 Console.WriteLine(" --ack-- "); }; channel.ExchangeDeclare("log_topic", ExchangeType.Topic); var msg = $"send {(args.Length == 0 ? "default" : args[0] ?? "default")} topic message"; var body = Encoding.UTF8.GetBytes(msg); channel.ConfirmSelect();//不管是串行还是并行,都要加入声明确认这句话 channel.BasicPublish("log_topic", (args.Length < 1 ? "#" : args[1] ?? "#"), null, body); Console.WriteLine(" [x] Sent {0}", msg); //串行的写法 //if (channel.WaitForConfirms()) //{ // Console.WriteLine("[x] Sent success"); //} //else //{ // Console.WriteLine("[x] Sent faild"); //} Console.WriteLine(" Press [enter] to exit."); Console.ReadLine(); } }
更多应答出现的问题: