Esempio n. 1
0
        public RabbitMQQueue(QueueName queueName, IQueueListener listener, IConnection connection,
            Encoding encoding = null, QueueOptions options = default(QueueOptions))
        {
            if (queueName == null) throw new ArgumentNullException("queueName");
            if (listener == null) throw new ArgumentNullException("listener");
            if (connection == null) throw new ArgumentNullException("connection");

            _queueName = queueName;
            _queueExchange = _queueName.GetExchangeName();
            _retryQueueName = queueName.GetRetryQueueName();
            _retryExchange = _queueName.GetRetryExchangeName();
            _deadLetterExchange = _queueName.GetDeadLetterExchangeName();

            _listener = listener;
            _connection = connection;
            _encoding = encoding ?? Encoding.UTF8;
            _ttl = options.TTL;
            _maxAttempts = Math.Max(options.MaxAttempts, 1);
            _retryDelay = options.RetryDelay < TimeSpan.Zero ? TimeSpan.Zero : options.RetryDelay;
            _cancellationTokenSource = new CancellationTokenSource();

            var autoAcknowledge = options.AutoAcknowledge;
            var concurrencyLimit = Math.Max(options.ConcurrencyLimit, 1);
            _consumers = new DurableConsumer[concurrencyLimit];
            for (var i = 0; i < _consumers.Length; i++)
            {
                var consumerTag = _queueName + "_" + i;
                _consumers[i] = new DurableConsumer(_connection, queueName, HandleDelivery, consumerTag,
                    autoAcknowledge);
            }
        }
        protected override async Task GivenExistingQueuedMessage(QueueName queueName, Message message, IPrincipal principal)
        {
            var connectionFactory = new ConnectionFactory {
                Uri = _uri
            };

            using (var connection = connectionFactory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    // We have to declare the queue as a persistence queue because this is
                    // called before the queue is created by the RabbitMQQueueingService
                    var deadLetterExchange = queueName.GetDeadLetterExchangeName();
                    var queueArgs          = new Dictionary <string, object>
                    {
                        { "x-dead-letter-exchange", deadLetterExchange }
                    };
                    channel.ExchangeDeclare(deadLetterExchange, "direct", false, false, null);
                    channel.QueueDeclare(queueName, false, false, false, queueArgs);
                    await RabbitMQHelper.PublishMessage(message, Thread.CurrentPrincipal, channel, queueName);
                }
        }
Esempio n. 3
0
        /// <summary>
        /// Initializes a new <see cref="RabbitMQQueue"/>
        /// </summary>
        /// <param name="connection">The connection to the RabbitMQ server</param>
        /// <param name="queueName">The name of the queue</param>
        /// <param name="listener">The listener that will receive new messages off of the queue</param>
        /// <param name="encoding">(Optional) The encoding to use when converting serialized message
        ///     content to byte streams</param>
        /// <param name="options">(Optional) Queueing options</param>
        /// <param name="diagnosticService">(Optional) The service through which diagnostic events
        ///     are reported and processed</param>
        /// <param name="securityTokenService">(Optional) The message security token
        ///     service to use to issue and validate security tokens for persisted messages.</param>
        /// <param name="messageEncryptionService"></param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="queueName"/>,
        /// <paramref name="listener"/>, or <paramref name="connection"/> is <c>null</c></exception>
        public RabbitMQQueue(IConnection connection,
                             QueueName queueName, IQueueListener listener,
                             Encoding encoding, QueueOptions options,
                             IDiagnosticService diagnosticService,
                             ISecurityTokenService securityTokenService,
                             IMessageEncryptionService messageEncryptionService)
        {
            _queueName          = queueName ?? throw new ArgumentNullException(nameof(queueName));
            _queueExchange      = _queueName.GetExchangeName();
            _retryQueueName     = queueName.GetRetryQueueName();
            _retryExchange      = _queueName.GetRetryExchangeName();
            _deadLetterExchange = _queueName.GetDeadLetterExchangeName();

            _listener             = listener ?? throw new ArgumentNullException(nameof(listener));
            _connection           = connection ?? throw new ArgumentNullException(nameof(connection));
            _securityTokenService = securityTokenService ?? throw new ArgumentNullException(nameof(securityTokenService));
            _encoding             = encoding ?? Encoding.UTF8;

            var myOptions = options ?? new QueueOptions();

            _ttl             = myOptions.TTL;
            _autoAcknowledge = myOptions.AutoAcknowledge;
            _maxAttempts     = myOptions.MaxAttempts;
            _retryDelay      = myOptions.RetryDelay;
            _isDurable       = myOptions.IsDurable;

            var concurrencyLimit = myOptions.ConcurrencyLimit;

            _cancellationTokenSource  = new CancellationTokenSource();
            _diagnosticService        = diagnosticService ?? DiagnosticService.DefaultInstance;
            _messageEncryptionService = messageEncryptionService;

            var consumerTag = _queueName;

            _consumer = new DurableConsumer(_connection, queueName, HandleDelivery, consumerTag,
                                            concurrencyLimit, _autoAcknowledge, _diagnosticService);
        }
        private static void DeleteQueue(QueueName queueName)
        {
            var connectionFactory = new ConnectionFactory { Uri = RabbitMQUri.ToString() };
            using (var connection = connectionFactory.CreateConnection())
            using (var channel = connection.CreateModel())
            {
                // We have to declare the queue as a persistence queue because this is 
                // called before the queue is created by the RabbitMQQueueingService
                var queueExchange = queueName.GetExchangeName();
                var retryExchange = queueName.GetRetryExchangeName();
                var deadLetterExchange = queueName.GetDeadLetterExchangeName();
                var retryQueueName = queueName.GetRetryQueueName();

                channel.QueueDeleteNoWait(queueName, false, false);
                channel.QueueDeleteNoWait(retryQueueName, false, false);
                channel.ExchangeDeleteNoWait(queueExchange, false);
                channel.ExchangeDeleteNoWait(retryExchange, false);
                channel.ExchangeDeleteNoWait(deadLetterExchange, false);
            }
        }
 private static async Task StageExistingMessage(Message message, QueueName queueName)
 {
     var connectionFactory = new ConnectionFactory {Uri = RabbitMQUri.ToString()};
     using (var connection = connectionFactory.CreateConnection())
     using (var channel = connection.CreateModel())
     {
         // We have to declare the queue as a persistence queue because this is 
         // called before the queue is created by the RabbitMQQueueingService
         var deadLetterExchange = queueName.GetDeadLetterExchangeName();
         var queueArgs = new Dictionary<string, object>
         {
             {"x-dead-letter-exchange", deadLetterExchange}
         };
         channel.ExchangeDeclare(deadLetterExchange, "direct", true, false, null);
         channel.QueueDeclare(queueName, true, false, false, queueArgs);
         await RabbitMQHelper.PublishMessage(message, Thread.CurrentPrincipal, channel, queueName);
     }
 }