码迷,mamicode.com
首页 > Windows程序 > 详细

在C#中使用消息队列RabbitMQ

时间:2017-08-30 13:01:33      阅读:434      评论:0      收藏:0      [点我收藏+]

标签:data   一个   sicp   获取   int   ons   basic   消费   效果   

参考文章:http://www.cnblogs.com/qy1141/p/4054135.html

开发环境&工具: VS2017 RabbitMq Erlang运行环境

先安装Erlang运行环境然后再安装RabbitMq

安装和配置就不说了

默认安装路径:C:\Program Files\RabbitMQ Server,在rabbitmq_server-3.6.11\sbin文件夹下有bat文件

默认配置文件路径: C:\Users\wangshibang\AppData\Roaming\RabbitMQ\rabbitmq.config

下面直接说代码

客户端Client把发送的消息储存到RabbitMq中,服务器开启的时候会从Rabbitmq中读取储存的消息

项目结构图

技术分享

 

Clinet端代码(记得添加 RabbitMQ.Client的Nuget包)

 1 using Newtonsoft.Json;
 2 using RabbitMQ.Client;
 3 using System;
 4 using System.Text;
 5 using RabbitMqLib;
 6 
 7 namespace RabbitMqClient
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             try
14             {
15                 ConnectionFactory factory = new ConnectionFactory();
16                 factory.HostName = Constants.MqHost;
17                 factory.Port = Constants.MqPort;
18                 factory.UserName = Constants.MqPwd;
19                 using (IConnection conn = factory.CreateConnection())
20                 {
21                     using (IModel channel = conn.CreateModel())
22                     {
23                         channel.QueueDeclare("MyFirstQueue", true, false, false, null);
24                         while (true)
25                         {
26                             string customStr = Console.ReadLine();
27                             RequestMsg requestMsg = new RequestMsg
28                             {
29                                 Name = $"Name_{customStr}",
30                                 Code = $"Code_{customStr}"
31                             };
32                             string jsonStr = JsonConvert.SerializeObject(requestMsg);
33                             byte[] bytes = Encoding.UTF8.GetBytes(jsonStr);
34 
35                             //设置消息持久化
36                             IBasicProperties properties = channel.CreateBasicProperties();
37                             properties.DeliveryMode = 2;
38                             channel.BasicPublish("", "MyFirstQueue", properties, bytes);
39 
40                             Console.WriteLine("消息已经发送:" + requestMsg.ToString());
41                         }
42                     }
43                 }
44             }
45             catch (Exception ex)
46             {
47                 Console.WriteLine(ex.ToString());
48             }
49             Console.ReadLine();
50         }
51     }
52 }

Server端代码(记得添加 RabbitMQ.Client Nuget包)

 1 using Newtonsoft.Json;
 2 using RabbitMQ.Client;
 3 using RabbitMQ.Client.Events;
 4 using RabbitMqLib;
 5 using System;
 6 using System.Text;
 7 
 8 namespace RabbitMqServer
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             try
15             {
16                 ConnectionFactory factory = new ConnectionFactory();
17                 factory.HostName = Constants.MqHost;
18                 factory.Port = Constants.MqPort;
19                 factory.UserName = Constants.MqUserName;
20                 factory.Password = Constants.MqPwd;
21                 using (IConnection conn = factory.CreateConnection())
22                 {
23                     using (IModel channel = conn.CreateModel())
24                     {
25                         //在MQ上定义一个持久化队列,如果名称相同不会重复创建
26                         channel.QueueDeclare("MyFirstQueue", true, false, false, null);
27                         //输入1,那如果接收一个消息,但是没有应答,则客户端不会收到下一个消息
28                         channel.BasicQos(0, 1, false);
29                         Console.WriteLine("Listening...");
30                         //在队列上定义一个消费者
31                         QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);
32                         //消费队列,并设置应答模式为程序主动应答
33                         channel.BasicConsume("MyFirstQueue", false, consumer);
34 
35                         while (true)
36                         {
37                             //阻塞函数,获取队列中的消息
38                             BasicDeliverEventArgs ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
39                             byte[] bytes = ea.Body;
40                             string str = Encoding.UTF8.GetString(bytes);
41                             RequestMsg msg = JsonConvert.DeserializeObject<RequestMsg>(str);
42                             Console.WriteLine("HandleMsg:" + msg.ToString());
43                             //回复确认
44                             channel.BasicAck(ea.DeliveryTag, false);
45                         }
46                     }
47                 }
48             }
49             catch (Exception ex)
50             {
51                 Console.WriteLine(ex.ToString());
52             }
53             Console.ReadLine();
54         }
55     }
56 }

客户端显示效果

技术分享

服务端显示效果

技术分享

最后还有类库的两个类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace RabbitMqLib
 8 {
 9     public static class Constants
10     {
11         public static string MqHost { get; } = "localhost";
12 
13         public static int MqPort { get; } = 5672;
14 
15         public static string MqPwd { get; } = "guest";
16 
17         public static string MqUserName { get; } = "guest";
18     }
19 }
 1 namespace RabbitMqLib
 2 {
 3     public class RequestMsg
 4     {
 5         public string Name { get; set; }
 6 
 7         public string Code { get; set; }
 8 
 9         public override string ToString()
10         {
11             return $"Name: {Name}, Code: {Code}";
12         }
13     }
14 }

Constants的具体配置可以参考这篇文章

http://www.rabbitmq.com/dotnet-api-guide.html

技术分享

 

在C#中使用消息队列RabbitMQ

标签:data   一个   sicp   获取   int   ons   basic   消费   效果   

原文地址:http://www.cnblogs.com/wangyulong/p/7452465.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!