只有当接收方在发送方之前运行时,我才能接收消息。但是我需要能够在接收器未运行时发送消息。并在接收器启动时获取它。可能吗?
C# 示例:
//Send
Channel.ExchangeDeclare(ExchangeName,"direct");
Channel.BasicPublish(ExchangeName, "MyRoute", null, body);
//Receive (other app)
Channel.ExchangeDeclare(ExchangeName,"direct");
String queueName = Channel.QueueDeclare().QueueName;
Channel.QueueBind(queueName, ExchangeName, "MyRoute");
QueueingBasicConsumer consumer = new QueueingBasicConsumer(Channel);
Channel.BasicConsume(queueName, false, consumer);
BasicDeliverEventArgs Message=consumer.Queue.DequeueNoWait(null);
您正在创建一个自动删除队列:
String queueName = Channel.QueueDeclare().QueueName;
因此,当您关闭使用者时,队列会自动删除。
如果要这样做,则必须创建一个持久队列,然后将队列绑定到Exchange,如下所示:
channel.queueDeclare("yourQueue", true, false, false, null);
这样,您就不需要消费者启动并运行。如果使用者关闭,消息将被存储到队列中。