C# 消息队列RabbitMQ的简单使用

Message Queue消息队列,简称MQ,是一种应用程序对应用程序的通信方法,应用程序通过读写出入队列的消息来通信,而无需专用连接来链接它们。消息传递指的是程序之间通过在消息中发送数据进行通信,而不是通过直接调用彼此通信。MQ是消费-生产者模型的一个典型代表,一端往消息队列中不断写入消息,而另一端则可以读取或者订阅队列中的消息。

一、安装

下载RabbitMQ并安装,安装成功后会在服务中看到该服务。

二、使用Nuget引入RabbitMQ

三、简单程序

 1     /// <summary>
 2     /// RabbitMQ
 3     /// </summary>
 4     class Program
 5     {
 6         static void Main(string[] args)
 7         {
 8             string type = Console.ReadLine();
 9             //生产者
10             if (type=="1")
11             {
12                 ConnectionFactory factory = new ConnectionFactory();
13                 factory.HostName = "127.0.0.1";
14                 //默认端口
15                 factory.Port = 5672;
16                 using (IConnection conn = factory.CreateConnection())
17                 {
18                     using (IModel channel = conn.CreateModel())
19                     {
20                         //在MQ上定义一个持久化队列,如果名称相同不会重复创建
21                         channel.QueueDeclare("MyRabbitMQ", true, false, false, null);
22                         while (true)
23                         {
24                             string message = string.Format("Message_{0}", Console.ReadLine());
25                             byte[] buffer = Encoding.UTF8.GetBytes(message);
26                             IBasicProperties properties = channel.CreateBasicProperties();
27                             properties.DeliveryMode = 2;
28                             channel.BasicPublish("", "MyRabbitMQ", properties, buffer);
29                             Console.WriteLine("消息发送成功:" + message);
30                         }
31                     }
32                 }
33             }
34             else
35             {
36                 //消费者
37                 ConnectionFactory factory = new ConnectionFactory();
38                 factory.HostName = "127.0.0.1";
39                 //默认端口
40                 factory.Port = 5672;
41                 using (IConnection conn = factory.CreateConnection())
42                 {
43                     using (IModel channel = conn.CreateModel())
44                     {
45                         //在MQ上定义一个持久化队列,如果名称相同不会重复创建
46                         channel.QueueDeclare("MyRabbitMQ", true, false, false, null);
47 
48                         //输入1,那如果接收一个消息,但是没有应答,则客户端不会收到下一个消息
49                         channel.BasicQos(0, 1, false);
50 
51                         Console.WriteLine("Listening...");
52 
53                         //在队列上定义一个消费者
54                         QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);
55                         //消费队列,并设置应答模式为程序主动应答
56                         channel.BasicConsume("MyRabbitMQ", false, consumer);
57 
58                         while (true)
59                         {
60                             //阻塞函数,获取队列中的消息
61                             BasicDeliverEventArgs ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
62                             byte[] bytes = ea.Body;
63                             string str = Encoding.UTF8.GetString(bytes);
64 
65                             Console.WriteLine("队列消息:" + str.ToString());
66                             //回复确认
67                             channel.BasicAck(ea.DeliveryTag, false);
68                         }
69                     }
70                 }
71             }
72           
73         }
74     }
 *****************************
 *** Keep learning and growing. ***
 *****************************

猜你喜欢

转载自www.cnblogs.com/gangle/p/9303080.html