摘要
方法,应用程序通过读写出入队列的消息来通信,而无需专用连接来链接它们。消息传递指的是程序之间通过在消息中发送数据进行通信,而不是通过直接调用彼此通信。
订阅队列中的消息。
安装
。
一个例子
代码如下:
Main( type =
(type=== =
factory.Port = (IConnection conn = (IModel channel =名称相同不会重复创建
channel.QueueDeclare(,,, ( message = .Format([] buffer === cpublish(, +
ConnectionFactory factory = =
factory.Port = (IConnection conn = (IModel channel =名称相同不会重复创建
channel.QueueDeclare(,
</span><span style="color: #008000;">//</span><span style="color: #008000;">输入1,那如果接收一个消息,但是没有应答,则客户端不会收到下一个消息</span>
channel.BasicQos(<span style="color: #800080;">0</span>,<span style="color: #800080;">1</span>,<span style="color: #0000ff;">false</span><span style="color: #000000;">);
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Listening...</span><span style="color: #800000;">"</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">在队列上定义一个消费者</span>
QueueingBasicConsumer consumer = <span style="color: #0000ff;">new</span><span style="color: #000000;"> QueueingBasicConsumer(channel);
</span><span style="color: #008000;">//</span><span style="color: #008000;">消费队列,并设置应答模式为程序主动应答</span>
channel.BasicConsume(<span style="color: #800000;">"</span><span style="color: #800000;">MyRabbitMQ</span><span style="color: #800000;">"</span>,<span style="color: #0000ff;">false</span><span style="color: #000000;">,consumer);
</span><span style="color: #0000ff;">while</span> (<span style="color: #0000ff;">true</span><span style="color: #000000;">)
{
</span><span style="color: #008000;">//</span><span style="color: #008000;">阻塞<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" class="keywords">函数</a>,<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>队列中的消息</span>
BasicDeliverEventArgs ea =<span style="color: #000000;"> (BasicDeliverEventArgs)consumer.Queue.Dequeue();
</span><span style="color: #0000ff;">byte</span>[] bytes =<span style="color: #000000;"> ea.Body;
</span><span style="color: #0000ff;">string</span> str =<span style="color: #000000;"> Encoding.UTF8.GetString(bytes);
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">队列消息:</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> str.ToString());
</span><span style="color: #008000;">//</span><span style="color: #008000;"><a href="https://www.jb51.cc/tag/huifu/" target="_blank" class="keywords">回复</a>确认</span>
channel.BasicAck(ea.DeliveryTag,<span style="color: #0000ff;">false</span><span style="color: #000000;">);
}
}
}
}
}
}</span></pre>
4048.png" alt="">
文章
原文链接:https://www.f2er.com/csharp/191519.html