コード例 #1
0
        public RabbitMQProducer(IModel channel, string channelName, RabbitMQOptions options, bool isBroadcast)
        {
            this.channel     = channel;
            this._options    = options;
            this.IsBroadcast = isBroadcast;

            //默认使用通道名称作为Exchange名称
            this.exchange = _options.Exchange?.Name ?? channelName;

            basicProperties            = channel.CreateBasicProperties();
            basicProperties.Persistent = true;
            _options.Events?.OnSetBasicProperties?.Invoke(basicProperties);

            if (isBroadcast)
            {
                //广播模式下,不需要创建queue
            }
            else
            {
                //如果Queue不存在,定义Queue;如果已存在,则忽略此操作。此操作是幂等的。
                var queueName = channel.QueueDeclare(queue: _options.QueueDeclare?.Name ?? channelName,
                                                     //非广播模式下默认持久化
                                                     durable: _options.QueueDeclare?.Durable == null ? true : _options.QueueDeclare.Durable.Value,
                                                     //非广播模式下默认不使用专属queue
                                                     exclusive: _options.QueueDeclare?.Exclusive == null ? false : _options.QueueDeclare.Exclusive.Value,
                                                     //非广播模式下默认不自动删除queue
                                                     autoDelete: _options.QueueDeclare?.AutoDelete == null ? false : _options.QueueDeclare.AutoDelete.Value,
                                                     arguments: _options.QueueDeclare.Arguments).QueueName;

                //如果没有指定routingKey,直接使用queueName作为routingKey
                routingKey = _options.Exchange?.RoutingKey ?? queueName;

                channel.QueueBind(queueName, exchange, routingKey);
            }
        }
コード例 #2
0
        public RabbitMQConsumer(IModel channel, string channelName, RabbitMQOptions options, bool isBroadcast)
        {
            this.channel = channel;
            this._options = options;
            this.IsBroadcast = isBroadcast;

            //默认使用通道名称作为Exchange名称
            string exchange = _options.Exchange?.Name ?? channelName;

            consumer = new EventingBasicConsumer(channel);
            consumer.Received += OnReceived;

            //如果Queue不存在,定义Queue;如果已存在,则忽略此操作。此操作是幂等的。
            if (isBroadcast)
            {
                //广播模式下,默认让系统自动生成队列名称
                queueName = channel.QueueDeclare(queue: _options.QueueDeclare?.Name ?? "",
                         //广播模式下默认不持久化
                         durable: _options.QueueDeclare?.Durable == null ? false : _options.QueueDeclare.Durable.Value,
                         //广播模式下默认使用专属queue
                         exclusive: _options.QueueDeclare?.Exclusive == null ? true : _options.QueueDeclare.Exclusive.Value,
                         //广播模式下默认自动删除queue
                         autoDelete: _options.QueueDeclare?.AutoDelete == null ? true : _options.QueueDeclare.AutoDelete.Value,
                         arguments: _options.QueueDeclare.Arguments).QueueName;
            }
            else
            {
                //非广播模式下,默认使用通道名称作为队列名称
                queueName = channel.QueueDeclare(queue: _options.QueueDeclare?.Name ?? channelName,
                         //非广播模式下默认持久化
                         durable: _options.QueueDeclare?.Durable == null ? true : _options.QueueDeclare.Durable.Value,
                         //非广播模式下默认不使用专属queue
                         exclusive: _options.QueueDeclare?.Exclusive == null ? false : _options.QueueDeclare.Exclusive.Value,
                         //非广播模式下默认不自动删除queue
                         autoDelete: _options.QueueDeclare?.AutoDelete == null ? false : _options.QueueDeclare.AutoDelete.Value,
                         arguments: _options.QueueDeclare.Arguments).QueueName;
            }

            //广播模式下,默认使用空字符串作为routingKey;否则使用队列名称
            var routingKey = _options.Exchange?.RoutingKey ?? (isBroadcast ? "" : queueName);

            channel.QueueBind(queueName, exchange, routingKey);
        }
コード例 #3
0
 public RabbitMQProvider(RabbitMQOptions options)
 {
     this._options = options;
 }