Search code examples
rabbitmqconnection-timeout

RabbitMQ .NET Client and connection timeouts


I'm trying to test the AutomaticRecoveryEnabled property of the RabbitMQ ConnectionFactory. I'm connecting to a RabbitMQ instance on a local VM and on the client I'm publishing messages in a loop. The problem is if I intentionally break the connection, the client just waits forever and doesn't time out. How do I set the time out value? RequestedConnectionTimeout doesn't appear to have any effect.

I'm using the RabbitMQ client 3.5.4

Rudimentary publish loop:

// 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);
}

The Publish method inside the wrapper:

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;
}

The connection and connection factory:

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

Solution

  • It appears this is a bug in version 3.5.4. Version 3.6.3 does not wait indefinitely.