Rabbitmq C# 客户端超时错误

Rabbitmq C# client timeout error

以下是在 visual studio 2013 年构建为 windows 表单的 Rabbitmq 消费者应用程序的一部分。此表单连接到在另一个程序中设置的交换并侦听发送的消息通过三个路由键之一:"info"、"error" 和 "warning"。用户通过在 windows 表单上选中它们然后单击收听按钮来选择他们想要收听的路由密钥。

private void listenButton_Click(object sender, EventArgs e)
    {
        var factory = new ConnectionFactory() { HostName = "localhost" };
        using (var connection = factory.CreateConnection())
        using (var channel = connection.CreateModel())
        {
            channel.ExchangeDeclare("direct_logs", "direct");

            var queueName = channel.QueueDeclare().QueueName;

            if (infoCheckBox.Checked)
            {
                channel.QueueBind(queueName, "direct_logs", "info");
            }
            if (errorCheckBox.Checked)
            {
                channel.QueueBind(queueName, "direct_logs", "error");
            }
            if (warningCheckBox.Checked)
            {
                channel.QueueBind(queueName, "direct_logs", "warning");
            }

            var consumer = new EventingBasicConsumer(channel);
            consumer.Received += (model, ea) =>
            {
                var body = ea.Body;
                var message = Encoding.UTF8.GetString(body);
                messageTextBox.Text += "\n" + message;
            };
            channel.BasicConsume(queueName, true, consumer);
        }
    }

但是,为了能够执行 QueueBind 方法,我需要 queueName。但是程序总是在行 var queueName = channel.QueueDeclare().QueueName; 上失败。它总是冻结大约 10 秒,然后出现超时异常。我在控制台应用程序中使用了几乎完全相同的代码。使用 windows 表格时,我需要考虑哪些不同之处吗?预先感谢您的帮助。

如果您总是让 RabbitMQ 在 QueueDeclare 上超时,您可能只是在长时间 运行 作业中遇到了内存问题。按照此 google groups discussion 中的建议,尝试 运行:

rabbitmqctl eval 'rabbit_diagnostics:maybe_stuck().'

此工具仅检查是否有任何作业在特定时间内没有任何堆栈跟踪更改 window,但对于查找挂起的作业很有用。如果出现问题,您将得到如下输出:

2017-03-27 13:59:27 There are 269 processes.
2017-03-27 13:59:27 Investigated 9 processes this round, 5000ms to go.
2017-03-27 13:59:28 Investigated 9 processes this round, 4500ms to go.
2017-03-27 13:59:29 Investigated 9 processes this round, 4000ms to go.
2017-03-27 13:59:29 Investigated 9 processes this round, 3500ms to go.
2017-03-27 13:59:30 Investigated 9 processes this round, 3000ms to go.
2017-03-27 13:59:30 Investigated 9 processes this round, 2500ms to go.
2017-03-27 13:59:31 Investigated 9 processes this round, 2000ms to go.
2017-03-27 13:59:31 Investigated 9 processes this round, 1500ms to go.
2017-03-27 13:59:32 Investigated 9 processes this round, 1000ms to go.
2017-03-27 13:59:32 Investigated 9 processes this round, 500ms to go.
2017-03-27 13:59:33 Found 9 suspicious processes.
2017-03-27 13:59:33 [{pid,<10469.54.0>},
                     {registered_name,user},
                     {current_stacktrace,
                         [{user,get_chars,8,[{file,"user.erl"},{line,613}]},
                          {user,do_io_request,5,
                              [{file,"user.erl"},{line,183}]},
                          {user,server_loop,2,[{file,"user.erl"},
etc

我通常只用 rabbitmqctl stop_apprabbitmqctl start_app 就能成功清理可疑作业,但您也可以使用

终止特定作业
rabbitmqctl eval 'erlang:exit(c:pid(0,54,0),kill).'