• 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();
            }
        }
    }
  • 相关阅读:
    【网页前端】JS呈现时间戳为与当前时间比较结果
    【Django工具】Django_debug_toolbar使用
    【Django Models】Django数据查询 汇聚
    【Django Models】虚拟化提取Models公共的功能
    【网页在线编辑】图文发送的模式
    【Django后台数据管理】后台数据新建或者保存,经常遇到提示This field is required
    【BBS】Discuz部署
    【Django】QuerySet的分页和排序
    【Django数据库】如何将一个表自定义的key列还原成id列作为key
    18.6
  • 原文地址:https://www.cnblogs.com/x-poior/p/6123891.html
Copyright © 2020-2023  润新知