Esempio n. 1
0
        private void ConsumeMessages(string queueName)
        {
            using (IConnection conn = factory.CreateConnection())
            {
                using (IModel channel = conn.CreateModel())
                {
                    var consumer = new QueueingBasicConsumer(channel);
                    channel.BasicConsume(queueName, false, consumer);
                    Trace.WriteLine(string.Format("Waiting for messages from: {0}", queueName));

                    while (true)
                    {
                        BasicDeliverEventArgs ea = null;
                        try
                        {
                            ea = consumer.Queue.Dequeue();
                        }
                        catch (EndOfStreamException endOfStreamException)
                        {
                            Trace.WriteLine(endOfStreamException);
                        }
                        if (ea == null)
                        {
                            break;
                        }
                        var body            = ea.Body;
                        var receivedMessage = _serializer.Deserialie <NotificationMessage>(body);
                        Trace.WriteLine(
                            string.Format(
                                "Message Received: {0} From: {1}, To: {2} on Thread: {3} with CorrelationId: {4} and DeliveryTag: {5}",
                                receivedMessage.Message, receivedMessage.Source, receivedMessage.Target,
                                Thread.CurrentThread.ManagedThreadId, ea.BasicProperties.CorrelationId, ea.DeliveryTag));
                        Thread.Sleep(300);
                        channel.BasicAck(ea.DeliveryTag, false);
                    }
                }
            }
        }