/// <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);
        }
Exemple #2
0
        /// <summary>
        ///  触发消费行为
        /// </summary>
        /// <param name="queue">队列名称</param>
        /// <param name="callback">回调方法</param>
        public void Subscribe <TMessage>(string exchange, string queue, Action <TMessage> callback = null)
        {
            if (!string.IsNullOrWhiteSpace(exchange))
            {
                exchangeName = exchange;
            }
            // 使用自定义的队队
            if (!string.IsNullOrWhiteSpace(queue))
            {
                queueName = queue;
            }
            if (!string.IsNullOrWhiteSpace(queueName))          //分发模式
            {
                channel.ExchangeDeclare(exchangeName, "topic"); //广播
                channel.QueueDeclare(
                    queue: queueName,
                    durable: false,    //持久化
                    exclusive: false,  //独占,只能被一个consumer使用
                    autoDelete: false, //自己删除,在最后一个consumer完成后删除它
                    arguments: null);
                channel.QueueBind(queueName, exchangeName, queueName);
            }
            else
            {
                //广播模式
                channel.ExchangeDeclare(this.exchangeName, "fanout");     //广播
                QueueDeclareOk queueOk = channel.QueueDeclare();          //每当Consumer连接时,我们需要一个新的,空的queue,如果在声明queue时不指定,那么RabbitMQ会随机为我们选择这个名字
                queueName = queueOk.QueueName;                            //得到RabbitMQ帮我们取了名字
                channel.QueueBind(queueName, exchangeName, string.Empty); //不需要指定routing key,设置了fanout,指了也没有用.
            }
            var consumer = new EventingBasicConsumer(channel);

            consumer.Received += (sender, e) =>
            {
                var body = e.Body;
                var json = Encoding.UTF8.GetString(body.ToArray());
                callback(SerializeMemoryHelper.JsonDeserialize <TMessage>(json));
                channel.BasicAck(e.DeliveryTag, multiple: false);
            };
            channel.BasicConsume(queue: queueName,
                                 autoAck: true,
                                 consumer: consumer);
            queueName = null;
            Console.WriteLine(" [*] Waiting for messages." + "To exit press CTRL+C");
        }
        /// <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);
        }