当前位置:
首页 > Python基础教程 >
-
C#教程之.Net使用RabbitMQ
网上参考大神们的博客,自己做了一个RabbitMQ即时发消息的Demo。
1.使用VS的NuGet安装包管理工具安装RabbitMQ.Client:
2.生产者端代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using RabbitMQ.Client; 7 8 namespace RabbitMQ.Producter 9 { 10 class Program 11 { 12 /// <summary> 13 /// 连接配置 14 /// </summary> 15 private static readonly ConnectionFactory rabbitMqFactory = new ConnectionFactory() 16 { 17 HostName="localhost", 18 UserName = "guest", 19 Password = "guest", 20 Port = 5672, 21 //VirtualHost = "JentVirtualHost" 22 }; 23 /// <summary> 24 /// 路由名称 25 /// </summary> 26 const string ExchangeName = "Jent.Exchange"; 27 /// <summary> 28 /// 队列名称 29 /// </summary> 30 const string QueueName = "Jent.Queue"; 31 static void Main(string[] args) 32 { 33 DirectExchangeSendMsg(); 34 Console.WriteLine("按任意键退出程序!"); 35 Console.ReadKey(); 36 } 37 /// <summary> 38 /// 单点精确路由模式 39 /// </summary> 40 private static void DirectExchangeSendMsg() 41 { 42 using (IConnection conn = rabbitMqFactory.CreateConnection()) 43 { 44 using (IModel channel = conn.CreateModel()) 45 { 46 channel.ExchangeDeclare(ExchangeName, "direct", durable: true, autoDelete: false, arguments: null); 47 channel.QueueDeclare(QueueName, durable: true, exclusive: false, autoDelete: false, arguments: null); 48 channel.QueueBind(QueueName, ExchangeName, routingKey: QueueName); 49 50 var props = channel.CreateBasicProperties(); 51 props.Persistent = true; 52 Console.WriteLine("请输入需要发送的消息:"); 53 string vadata = Console.ReadLine(); 54 while (vadata != "exit") 55 { 56 var msgBody = Encoding.UTF8.GetBytes(vadata); 57 channel.BasicPublish(exchange: ExchangeName, routingKey: QueueName, basicProperties: props, body: msgBody); 58 Console.WriteLine(string.Format("发送时间:{0},发送完毕,输入exit退出消息发送", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); 59 vadata = Console.ReadLine(); 60 } 61 } 62 } 63 } 64 } 65 }
3.消费者端代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using RabbitMQ.Client; 7 8 namespace RabbitMQ.Consumer 9 { 10 class Program 11 { 12 /// <summary> 13 /// 连接配置 14 /// </summary> 15 private static readonly ConnectionFactory rabbitMqFactory = new ConnectionFactory() 16 { 17 HostName = "127.0.0.1", 18 UserName = "guest", 19 Password = "guest", 20 Port = 5672, 21 //VirtualHost = "JentVirtualHost" 22 }; 23 /// <summary> 24 /// 路由名称 25 /// </summary> 26 const string ExchangeName = "Jent.Exchange"; 27 /// <summary> 28 /// 队列名称 29 /// </summary> 30 const string QueueName = "Jent.Queue"; 31 32 static void Main(string[] args) 33 { 34 DirectAcceptExchange(); 35 36 Console.WriteLine("输入任意值退出程序!"); 37 Console.ReadKey(); 38 } 39 40 private static void DirectAcceptExchange() 41 { 42 using (IConnection conn = rabbitMqFactory.CreateConnection()) 43 { 44 using (IModel channel = conn.CreateModel()) 45 { 46 channel.ExchangeDeclare(ExchangeName, "direct", durable: true, autoDelete: false, arguments: null); 47 channel.QueueDeclare(QueueName, durable: true, exclusive: false, autoDelete: false, arguments: null); 48 channel.QueueBind(QueueName, ExchangeName, routingKey: QueueName); 49 50 while (true) 51 { 52 BasicGetResult msgResponse = channel.BasicGet(QueueName, autoAck: false); 53 if (msgResponse != null) 54 { 55 var msgBody = Encoding.UTF8.GetString(msgResponse.Body); 56 Console.WriteLine(string.Format("接收时间:{0},消息内容:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), msgBody)); 57 } 58 //System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1)); 59 } 60 } 61 } 62 } 63 } 64 }
4.程序结果:
注:在第一步之前,你需要安装RabbitMQ客户端,可从http://www.rabbitmq.com/download.html下载,
但是RabbitMQ又是依赖于Erlang OTP平台,所以,安装RabbitMQ之前,需要先从http://www.erlang.org/download.html下载安装erlang
关于这部分的内容,推荐阅读:http://www.cnblogs.com/5ishare/p/6716142.html
此Demo只是‘direct’方式的消息发送接收方式。
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式