コード例 #1
0
 /// <summary>
 /// Determines if this queue has any related routing keys
 /// </summary>
 /// <returns></returns>
 public bool HasRoutingKeys()
 {
     if (RoutingKeys == null)
     {
         return(false);
     }
     return(RoutingKeys.Any());
 }
コード例 #2
0
        private async Task StartQueueReader()
        {
            lock (_sync)
            {
                Data      = new List <CustomQueueItem <T> >();
                _isActive = true;
            }

            var factory = new ConnectionFactory
            {
                Uri = new Uri(_connectionString)
            };

            using (var connection = factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    channel.BasicQos(0, (ushort)_prefetchCount, false);

                    channel.QueueDeclare(QueueName, true, false, IsQueueAutoDelete);

                    if (RoutingKeys.Any())
                    {
                        foreach (var key in RoutingKeys)
                        {
                            channel.QueueBind(QueueName, ExchangeName, key);
                        }
                    }
                    else
                    {
                        channel.QueueBind(QueueName, ExchangeName, "");
                    }
                    var consumer = new EventingBasicConsumer(channel);

                    consumer.Received += CreateOnMessageReceivedEventHandler(channel);

                    var tag = channel.BasicConsume(QueueName, false, consumer);

                    var writerTask = StartHandler(connection);

                    while (!_cancellationTokenSource.IsCancellationRequested && connection.IsOpen)
                    {
                        await Task.Delay(100);
                    }

                    await writerTask;

                    channel.BasicCancel(tag);
                    connection.Close();
                }
        }