RabbitMQ .NET 客户端和连接超时

RabbitMQ .NET Client and connection timeouts

我正在尝试测试 RabbitMQ ConnectionFactory 的 AutomaticRecoveryEnabled 属性。我正在连接到本地 VM 和客户端上的 RabbitMQ 实例,我正在循环发布消息。问题是如果我故意断开连接,客户端只会永远等待并且不会超时。如何设置超时值? RequestedConnectionTimeout 似乎没有任何效果。

我正在使用 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);
}

包装器内的 Publish 方法:

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();

这似乎是版本 3.5.4 中的错误。 3.6.3版本不无限期等待。