我是RabbitMQ的新手。 我知道,通过使用while循环,我可以查找Queue中新添加的消息。但我不想那样。我还尝试了EventingBasicConsumer Received事件处理程序,但只触发用户代码第一次运行时出现的消息。任何新添加的消息都不会为我触发此代码。如果队列中有新消息,RibbitMQ会通知我吗?我希望我的Web应用程序在有新消息的时候通过RabbitMQ得到通知。如何在RabbitMQ Queue中添加新消息时收到通知?
Receiver :
public static class RabbitMQConsumer2
{
public static void Receive()
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.ExchangeDeclare(exchange: "logs", type: "fanout");
channel.QueueDeclare("AAA", true, false, false, null);
channel.QueueBind(queue: "AAA",
exchange: "logs",
routingKey: "");
Debug.WriteLine(" [*] Waiting for logs.");
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
Debug.WriteLine(" [x] {0}", message);
};
channel.BasicConsume(queue: "AAA",
noAck: true,
consumer: consumer);
Debug.WriteLine(" Press [enter] to exit.");
}
}
}
你见过第二个教程吗?https://www.rabbitmq.com/tutorials/tutorial-two-java.html这很容易,你只需实现'handleDelivery'方法 – cantSleepNow