RabbitMQ框架学写笔记-20161201

Posted 未将对象引用设置到对象的实例

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 = 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的主要内容,如果未能解决你的问题,请参考以下文章

python2.7 爬虫_爬取小说盗墓笔记章节及URL并导入MySQL数据库_20161201

学写一个 Java Web MVC 框架

分布式服务框架学习笔记10 RabbitMQ在CentOS下的安装

20161201

学写一个 Java Web MVC 框架

分布式服务框架学习笔记10 RabbitMQ在CentOS下的安装