Esempio n. 1
0
        /// <summary>
        /// 广播消息,需要在初始化时为exchangeName赋值
        /// </summary>
        /// <typeparam name="TMessage"></typeparam>
        /// <param name="message"></param>
        public void PublishFanout <TMessage>(string exchange, TMessage message)
        {
            if (!string.IsNullOrWhiteSpace(exchange))
            {
                exchangeName = exchange;
            }
            const string ROUTING_KEY = "";

            channel.ExchangeDeclare(this.exchangeName, "fanout");//广播
            var json  = SerializeMemoryHelper.JsonSerializer(message);
            var bytes = Encoding.UTF8.GetBytes(json);

            channel.BasicPublish(this.exchangeName, ROUTING_KEY, null, bytes);//不需要指定routing key,设置了fanout,指了也没有用.
            Console.WriteLine(DateTime.Now + " 向服务器{0}推消息", _uri);
        }
Esempio n. 2
0
        /// <summary>
        /// 将消息推送到服务器
        /// </summary>
        /// <typeparam name="TMessage"></typeparam>
        /// <param name="queue"></param>
        /// <param name="message"></param>
        public void Publish <TMessage>(string exchange, string queue, TMessage message)
        {
            if (!string.IsNullOrWhiteSpace(exchange))
            {
                exchangeName = exchange;
            }
            channel.QueueDeclare(queue: queue,      //队列名
                                 durable: false,    //是否持久化
                                 exclusive: false,  //排它性
                                 autoDelete: false, //一旦客户端连接断开则自动删除queue
                                 arguments: null);  //如果安装了队列优先级插件则可以设置优先级

            var json  = SerializeMemoryHelper.JsonSerializer(message);
            var bytes = Encoding.UTF8.GetBytes(json);

            Console.WriteLine("向服务器{0}推消息", _uri);
            channel.BasicPublish(exchange: this.exchangeName, routingKey: queue, basicProperties: null, body: bytes);
        }