以下是在visual studio 2013中作为Windows窗体构建的Rabbitmq使用者应用程序的一部分。此表单连接到在另一个程序中设置的交换,并侦听通过三个路由键之一发送的消息:“info “,”错误“和”警告“。用户通过在窗体上检查它们然后单击监听按钮来选择他们想要收听的路由键中的哪一个。
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窗体时,我需要考虑哪些不同的东西?在此先感谢您的帮助。
答案 0 :(得分:2)
如果您总是在QueueDeclare
上暂停RabbitMQ,那么您可能只会遇到长时间运行的内存问题。正如本google groups discussion中所述,请尝试运行:
rabbitmqctl eval 'rabbit_diagnostics:maybe_stuck().'
此工具仅检查是否有任何作业在特定时间窗口内没有任何堆栈跟踪更改,但可用于查找挂起作业。如果出现问题,您将获得如下输出:
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_app
和rabbitmqctl start_app
来清理可疑的工作,但你也可以使用
rabbitmqctl eval 'erlang:exit(c:pid(0,54,0),kill).'