RabbitMQ和SharedQueue关闭

时间:2014-09-15 07:51:37

标签: c# .net-4.0 rabbitmq console-application

我使用RabbitMQ发送简单的短整数信息,首先我将id发送到一个项目:

    private void SendPgcIdToRabbitMQ(string id) 
    {
        var factory = new ConnectionFactory() { HostName = "localhost" };
        using (var connection = factory.CreateConnection()) 
        {
            using (var channel = connection.CreateModel()) 
            {
                bool durable = true;
                channel.QueueDeclare("XQueue", durable, false, false, null);

                var body = Encoding.UTF8.GetBytes(id);

                channel.BasicPublish("", "XQueue", null, body);
                Console.WriteLine(" [x] Sent {0}", id);
            }
        }
    }

和它的倾听者:

    public void Listener() 
    {
        var factory = new ConnectionFactory() { HostName = "localhost" };
        using (var connection = factory.CreateConnection()) 
        {
            using (var channel = connection.CreateModel()) 
            {
                channel.QueueDeclare("XQueue", true, false, false, null);

                var consumer = new QueueingBasicConsumer(channel);
                channel.BasicConsume("XQueue", false, consumer);

                Console.WriteLine(" [*] Waiting for messages. " +
                                  "To exit press CTRL+C");
                while (true) {
                    var ea =
                        (BasicDeliverEventArgs)consumer.Queue.Dequeue();

                    var body = ea.Body;
                    var message = Encoding.UTF8.GetString(body);
                    Console.WriteLine(" [x] Received {0}", message);

                    AddPGCFileID(message);

                    channel.BasicAck(ea.DeliveryTag, false);

                    Thread.Sleep(500);
                }
            }
        }
    }

它工作正常,所以在收到消息后我处理了一些操作,然后我得到第二个ID并创建其他队列来做同样的事情:

        private void SendSurveyIdToRabbitMQ(int yID) 
    {
        var factory = new ConnectionFactory() { HostName = "localhost" };
        using (var connection = factory.CreateConnection()) {
            using (var channel = connection.CreateModel()) {
                bool durable = true;
                channel.QueueDeclare("YQueue", durable, false, false, null);

                var body = Encoding.UTF8.GetBytes(yID.ToString());

                channel.BasicPublish("", "YQueue", null, body);
                Console.WriteLine(" [x] Sent {0}", yID);
            }
        }
    }

并收到:

        public void InquiryListener() 
    {
        var factory = new ConnectionFactory() { HostName = "localhost" };
        using (var connection = factory.CreateConnection()) {
            using (var channel = connection.CreateModel()) {
                channel.QueueDeclare("YQueue", true, false, false, null);

                var consumer = new QueueingBasicConsumer(channel);
                channel.BasicConsume("YQueue", false, consumer);

                Console.WriteLine(" [*] Waiting for messages. " +
                                  "To exit press CTRL+C");
                while (true) {
                    var ea =
                        (BasicDeliverEventArgs)consumer.Queue.Dequeue();

                    var body = ea.Body;
                    var message = Encoding.UTF8.GetString(body);
                    Console.WriteLine(" [x] Received {0}", message);

                    StartProcessing(Convert.ToInt32(message));

                    channel.BasicAck(ea.DeliveryTag, false);

                    Thread.Sleep(500);
                }
            }
        }
    }

第一个队列发送和接收工作正常,但在第二个我得到: enter image description here

这很奇怪,因为从那时起我就一直在努力解决这个问题。我重新修复rabbitmq,删除所有队列等等,无法找到问题所在。有什么想法吗?

编辑: 我很想知道第二个过程是否正常结束(第二个过程中的失败导致不会导致rabbitmq出现问题)并且它已经过去了,因为YQueue没有出现任何错误,但是在我工作了大约一分钟之后(只有等待,非接收消息,非处理)我在XQueue

上查明同样的异常

1 个答案:

答案 0 :(得分:1)

在执行while(true){ ... }之前,首先检查队列是否为空。