/// <summary>
        /// This method checks for a message on the supplied endpoint
        /// </summary>
        /// <param name="endpointName">Name of the endpoint configuration</param>
        /// <returns>True if a message was found on the queue and handled, otherwise false</returns>
        public virtual bool CheckForMessage(string endpointName)
        {
            var retVal = false;
            var endpoint = QueueEndpointProvider.GetEndpointByName(endpointName);
            ValidateEndpoint(endpoint);
            EndpointName = endpointName;
            var subscription = endpoint.Subscription;
            var factory = RabbitMqFactory.GetConnectionFactory(endpoint);
            using (IConnection connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    ChannelConfigurator.ConfigureErrorHandling(endpoint, channel);
                }
                using (var channel = connection.CreateModel())
                {
                    ChannelConfigurator.ConfigureQueue(endpoint, channel);
                    _consumer = RabbitMqFactory.GetBasicConsumer(channel, connection);
                    var result = channel.BasicGet(subscription.QueueName, subscription.NoAck);
                    if (result != null)
                    {
                        HandleReceivedMessage(channel, subscription, result);
                        retVal = true;
                    }
                    _consumer.Close();
                }
            }

            return retVal;
        }
Beispiel #2
0
 public virtual void Start(string endpointName)
 {
     if (Receivers.Count == 0)
         throw new InvalidOperationException(
             "ReceiveListener cannot be started without receivers. Register at least 1 receiver by calling AddReceiver(IReceiver receiver)");
     _isStarted = true;
     var endpoint = QueueEndpointProvider.GetEndpointByName(endpointName);
     EndpointName = endpointName;
     ValidateEndpoint(endpoint);
     var subscription = endpoint.Subscription;
     var factory = RabbitMqFactory.GetConnectionFactory(endpoint);
     using (var connection = factory.CreateConnection())
     {
         using (var channel = connection.CreateModel())
         {
             ChannelConfigurator.ConfigureErrorHandling(endpoint, channel);
         }
         using (var channel = connection.CreateModel())
         {
             ChannelConfigurator.ConfigureQueue(endpoint, channel);
             _consumer = RabbitMqFactory.GetBasicConsumer(channel, connection);
             channel.BasicConsume(endpoint.RoutingKey, subscription.NoAck, _consumer.GetConsumer());
             try
             {
                 while (_isStarted)
                 {
                     var ea =
                         (BasicDeliverEventArgs) _consumer.Dequeue();
                     HandleReceivedMessage(channel, subscription, ea);
                 }
             }
             catch (EndOfStreamException)
             {
                 // This try-catch is not as ugly as it may seem;)
                 // http://www.rabbitmq.com/releases/rabbitmq-dotnet-client/v2.6.1/rabbitmq-dotnet-client-2.6.1-client-htmldoc/html/type-RabbitMQ.Util.SharedQueue.html#method-M:RabbitMQ.Util.SharedQueue.Dequeue
                 var sb = new StringBuilder();
                 sb.AppendLine("An EndOfStreamException was caught.");
                 sb.AppendLine("This behaviour is expected when a connection/channel is closed by the client.");
                 Logger.Log(sb.ToString());
             }
         }
     }
 }