private static void Main(string[] args) { using RabbitMQHelper mq = new(new string[] { "192.168.181.191" }); mq.UserName = "******"; mq.Password = "******"; mq.Port = 5672; mq.Received += (result) => { Console.WriteLine($"message:{result.Body}"); result.Commit(); }; mq.Listen(exchangeName, "test.topic.queue2", new ExchangeConsumeQueueOptions { AutoAck = false }); #if rabbitMQClient Console.WriteLine($"{nameof(TopicConsumer)}:"); // RabbitMQ连接工厂 ConnectionFactory factory = BaseConsumer.CreateRabbitMqConnection(); // 建立连接 using IConnection connection = factory.CreateConnection(); // 创建信道 using IModel channel = connection.CreateModel(); string exchangeName = $"testExchange_topic"; string routeKey = "testExchange_routeKey.*"; //声明交换机并指定类型 channel.ExchangeDeclare(exchange: exchangeName, type: "topic"); string queueName = $"{exchangeName}_{nameof(TopicConsumer)}"; // 声明队列 channel.QueueDeclare(queue: queueName, durable: false, exclusive: false, autoDelete: false, arguments: null); // 将队列与交换机进行绑定 channel.QueueBind(queue: queueName, exchange: exchangeName, routingKey: routeKey); EventingBasicConsumer consumer = new(channel); channel.BasicQos(0, 1, false); consumer.Received += (model, ea) => { string message = Encoding.UTF8.GetString(ea.Body.ToArray()); Console.WriteLine($"Message:{message}"); channel.BasicAck( deliveryTag: ea.DeliveryTag, // 是否一次性确认多条数据 multiple: false); }; channel.BasicConsume(queue: queueName, autoAck: false, consumer: consumer); #endif Console.ReadLine(); }
private static void Main(string[] args) { using RabbitMQHelper mq = new(new string[] { "192.168.181.191" }); mq.UserName = "******"; mq.Password = "******"; mq.Port = 5672; Console.WriteLine("input queueName..."); var input = Console.ReadLine(); switch (input) { case "1": mq.Received += (result) => { Console.WriteLine($"message:{result.Body}"); result.Commit(); }; mq.Listen("test.fanout.queue1", new ConsumeQueueOptions { AutoAck = false }); Console.ReadLine(); break; case "2": mq.Received += (result) => { Console.WriteLine($"message:{result.Body}"); result.Commit(); }; mq.Listen("test.fanout.queue2", new ConsumeQueueOptions { AutoAck = false }); Console.ReadLine(); break; } #if rabbitMQClient ConnectionFactory factory = BaseConsumer.CreateRabbitMqConnection(); using IConnection connection = factory.CreateConnection(); using IModel channel = connection.CreateModel(); EventingBasicConsumer consumer = new(channel); channel.BasicQos(0, 1, false); channel.BasicConsume(queue: "test.fanout.queue1", autoAck: false, consumer: consumer); // 绑定消息接收后的事件委托 consumer.Received += (model, message) => { Console.WriteLine($"Message:{Encoding.UTF8.GetString(message.Body.ToArray())}"); channel.BasicAck( deliveryTag: message.DeliveryTag, // 是否一次性确认多条数据 multiple: false); }; #endif }
private static void Main(string[] args) { Console.WriteLine($"{nameof(SimpleSub)}:"); using RabbitMQHelper mq = new(new string[] { "192.168.181.191" }); mq.UserName = "******"; mq.Password = "******"; mq.Port = 5672; mq.Received += (result) => { Console.WriteLine($"message:{result.Body}"); result.Commit(); }; mq.Listen(queueName, new ConsumeQueueOptions { AutoAck = false }); #if rabbitMQClient // RabbitMQ连接工厂 ConnectionFactory factory = BaseConsumer.CreateRabbitMqConnection(); // 建立连接 using IConnection connection = factory.CreateConnection(); // 创建信道 using IModel channel = connection.CreateModel(); EventingBasicConsumer consumer = new(channel); // 每次只能向消费者发送一条信息,在消费者未确认之前,不再向它发送信息 channel.BasicQos(0, 1, false); channel.BasicConsume(queue: queueName, autoAck: false, consumer: consumer); // 绑定消息接收后的事件委托 consumer.Received += (model, message) => { Console.WriteLine($"Message:{Encoding.UTF8.GetString(message.Body.ToArray())}"); channel.BasicAck( deliveryTag: message.DeliveryTag, // 是否一次性确认多条数据 multiple: false); }; #endif Console.ReadLine(); }