2016-07-14 198 views
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(); 

回答

0

看来这是版本3.5.4中的一个错误。 3.6.3版本不会无限期地等待。