/// <summary> /// Initialize a <see cref="HaConnection"/> with a list of <see cref="ManagedConnectionFactory"/> /// These connection factories are responsibile for creating <see cref="IConnection"/> to nodes in the clusters /// </summary> /// <param name="retryPolicy"></param> /// <param name="watcher"></param> /// <param name="connectionFactories"></param> public HaConnection(IRetryPolicy retryPolicy, IRabbitWatcher watcher, IList <ManagedConnectionFactory> connectionFactories) : base(retryPolicy, watcher) { _connectionFactories = new RoundRobinList <ConnectionFactory>(connectionFactories); ConnectionEstablished handler = (endpoint, virtualHost) => { if (_connectionFactories.All.Any(f => f.Endpoint + f.VirtualHost == endpoint + virtualHost)) { if (!IsConnected) { while (_connectionFactories.Current.Endpoint + _connectionFactories.Current.VirtualHost != endpoint + virtualHost) { //IF there are 2 different Tunnels using 2 HaConnection with 2 lists of cluster nodes in different orders: //Example: // ConnectionString1: host=q1;username=guest;password=guest|host=q2;username=guest;password=guest|host=q3;username=guest;password=guest // ConnectionString2: host=q2;username=guest;password=guest|host=q3;username=guest;password=guest|host=q1;username=guest;password=guest // When the first tunnel established the connection successfully to q1, it fires event and these lines of code is triggered. // The 2nd HaConnection needs to set it's _connectionFactories.Current to q1 established by other tunnel. Before changing, _connectionFactories.Current is q3 by the order in the ConnectionString2 _connectionFactories.GetNext(); } } FireConnectedEvent(); } }; ManagedConnectionFactory.ConnectionEstablished += handler; _unsubscribeEvents = () => { ManagedConnectionFactory.ConnectionEstablished -= handler; }; }
/// <summary> /// Initialize a <see cref="HaConnection"/> with a list of <see cref="ManagedConnectionFactory"/> /// These connection factories are responsibile for creating <see cref="IConnection"/> to nodes in the clusters /// </summary> /// <param name="retryPolicy"></param> /// <param name="watcher"></param> /// <param name="connectionFactories"></param> public HaConnection(IRetryPolicy retryPolicy, IRabbitWatcher watcher, IList <ManagedConnectionFactory> connectionFactories) : base(retryPolicy, watcher, connectionFactories.FirstOrDefault()) { _connectionFactories = new RoundRobinList <ConnectionFactory>(connectionFactories); _retryPolicy = retryPolicy; _watcher = watcher; }