0
我試圖測試RabbitMQ ConnectionFactory的AutomaticRecoveryEnabled
屬性。我正在連接到本地虛擬機上的RabbitMQ實例,以及我在循環中發佈消息的客戶端。問題是,如果我故意破壞連接,客戶端就會永遠等待並且不會超時。如何設置超時值? RequestedConnectionTimeout
似乎沒有任何效果。RabbitMQ .NET客戶端和連接超時
我使用的RabbitMQ客戶端3.5.4
簡陋發佈循環:
// Client is a wrapper around the RabbitMQ client
for (var i = 0; i < 1000; ++i)
{
// Publish sequentially numbered messages
client.Publish("routingkey", GetContent(i)));
Thread.Sleep(100);
}
發佈的包裝袋中方法:
public bool Publish(string routingKey, byte[] body)
{
try
{
using (var channel = _connection.CreateModel())
{
var basicProps = new BasicProperties
{
Persistent = true,
};
channel.ExchangeDeclare(_exchange, _exchangeType);
channel.BasicPublish(_exchange, routingKey, basicProps, body);
return true;
}
}
catch (Exception e)
{
_logger.Log(e);
}
return false;
}
連接和連接工廠:
_connectionFactory = new ConnectionFactory
{
UserName = _userName,
Password = _password,
HostName = _hostName,
Port = _port,
Protocol = Protocols.DefaultProtocol,
VirtualHost = _virtualHost,
// Doesn't seem to have any effect on broken connections
RequestedConnectionTimeout = 2000,
// The behaviour appears to be the same with or without these included
// AutomaticRecoveryEnabled = true,
// NetworkRecoveryInterval = TimeSpan.FromSeconds(10),
};
_connection = _connectionFactory.CreateConnection();