首页 > 代码库 > RabbitMQ框架学写笔记-20161201
RabbitMQ框架学写笔记-20161201
1.RabbitMQ NUget .Net客户端4.1.1版本 对应的 .netframework版本是 4.5.1 以及以上
2.本地实现的效果图
3.实践代码:
P-生产者:
using RabbitMQ.Client; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RabbitMQClient { /// <summary> /// RabbitMQ的Topic /// </summary> class Program { private readonly static ConnectionFactory rabbitMqFactory = new ConnectionFactory() { HostName = "127.0.0.1", UserName = "---", Password = "514600", Port = 5672 }; /// <summary> /// 路由名称 /// </summary> const string TopExchangeName = "topic.justin.exchange"; const string TopQueueName = "topic.justin.queue"; public static void TopicExchangeSendMsg() { using(IConnection conn=rabbitMqFactory.CreateConnection()) { using(IModel channel = conn.CreateModel()) { channel.ExchangeDeclare(TopExchangeName,"topic",durable:true,autoDelete:false,arguments:null); channel.QueueDeclare(TopQueueName,durable:true,autoDelete:false,exclusive:false,arguments:null); channel.QueueBind(TopQueueName,TopExchangeName,routingKey:TopQueueName); Console.WriteLine("请输入要发送的消息!"); string vadata =http://www.mamicode.com/ Console.ReadLine(); while(vadata!="exit") { var msgBody = Encoding.UTF8.GetBytes(vadata); channel.BasicPublish(exchange:TopExchangeName,routingKey:TopQueueName,basicProperties:null,body:msgBody); Console.WriteLine(string.Format("**发送时间:{0},发送完成,输入exit退出消息发送",DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); vadata = Console.ReadLine(); } } } } static void Main(string[] args) { TopicExchangeSendMsg(); } } }
C-消费者代码:
using RabbitMQ.Client; using RabbitMQ.Client.Events; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RabbitMQClient_C { /// <summary> /// Customer接受消息代码 /// </summary> class Program { private readonly static ConnectionFactory rabbitMqFactory = new ConnectionFactory() { HostName="127.0.0.1", UserName="---",Password="514600",Port=5672 }; /// <summary> /// 路由名称 /// </summary> const string TopExchangeName = "topic.justin.exchange"; /// <summary> /// 队列名称 /// </summary> const string TopQueueName = "topic.justin.queue"; public static void TopicAcceptExchange() { using(IConnection conn = rabbitMqFactory.CreateConnection()) { using(IModel channel = conn.CreateModel()) { channel.ExchangeDeclare(TopExchangeName,"topic",durable:true,autoDelete:false,arguments:null); channel.QueueDeclare(TopQueueName,durable:true,autoDelete:false,exclusive:false,arguments:null); channel.BasicQos(prefetchSize:0,prefetchCount:1,global:false);//作用: channel.QueueBind(TopQueueName,TopExchangeName,routingKey:TopQueueName); var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { var msgBody = Encoding.UTF8.GetString(ea.Body); Console.WriteLine(string.Format("**接收时间:{0},消息内容:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), msgBody)); int dots = msgBody.Split(‘.‘).Length -1; System.Threading.Thread.Sleep(dots*1000); Console.WriteLine(" [x] Done"); channel.BasicAck(deliveryTag:ea.DeliveryTag,multiple:false); }; channel.BasicConsume(TopQueueName,noAck:false,consumer:consumer); Console.WriteLine("按任意键,退出程序"); Console.ReadLine(); } } } static void Main(string[] args) { TopicAcceptExchange(); } } }
RabbitMQ框架学写笔记-20161201
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。