/// <summary>
        /// Creates a new channel on the specified broker.
        /// </summary>
        /// <param name="brokerName">The broker to create channel.</param>
        /// <returns>The created connection channel.</returns>
        public IModel CreateChannel(string brokerName)
        {
            Check.NotNullOrWhiteSpace(brokerName, nameof(brokerName));

            BrokerConnection brokerConn = GetBrokerConnection(brokerName);

            IModel channel = null;

            try
            {
                channel = brokerConn.Connection.CreateModel();
            }
            catch (BrokerUnreachableException)
            {
                ConnectToBroker(brokerConn);
                channel = brokerConn.Connection.CreateModel();
            }
            catch (AlreadyClosedException)
            {
                ConnectToBroker(brokerConn);
                channel = brokerConn.Connection.CreateModel();
            }

            return(channel);
        }
        /// <summary>
        /// Determines if there is currently an open connection to the broker.
        /// </summary>
        /// <param name="brokerName">The name of the broker.</param>
        /// <returns>True if connection is open.  Otherwise, False.</returns>
        public bool IsBrokerConnected(string brokerName)
        {
            Check.NotNullOrWhiteSpace(brokerName, nameof(brokerName));

            BrokerConnection brokerConn = GetBrokerConnection(brokerName);

            return(brokerConn.Connection != null && brokerConn.Connection.IsOpen);
        }
        private BrokerConnection GetBrokerConnection(string brokerName)
        {
            BrokerConnection brokerConn = _connections.GetOptionalValue(brokerName);

            if (brokerConn == null)
            {
                throw new BrokerException(
                          $"An existing broker with the name of: {brokerName} does not exist.");
            }
            return(brokerConn);
        }
        /// <summary>
        /// Reestablishes the connection to the broker if not open.
        /// </summary>
        /// <param name="brokerName">The name of the broker.</param>
        public bool ReconnectToBroker(string brokerName)
        {
            Check.NotNullOrWhiteSpace(brokerName, nameof(brokerName));

            BrokerConnection brokerConn = GetBrokerConnection(brokerName);

            if (brokerConn.Connection == null || !brokerConn.Connection.IsOpen)
            {
                ConnectToBroker(brokerConn);
                return(true);
            }
            return(false);
        }
        protected virtual void ConnectToBroker(BrokerConnection brokerConn)
        {
            IConnectionFactory connFactory = CreateConnFactory(brokerConn.Settings);

            try
            {
                ExecuteAction.WithRetry <BrokerUnreachableException>(
                    _brokerSettings.NumConnectionRetries,
                    (retryCount) => {
                    LogConnectionException(brokerConn.Settings, retryCount);
                    brokerConn.Connection = connFactory.CreateConnection();
                });
            }
            catch (BrokerUnreachableException ex)
            {
                LogConnectionException(brokerConn.Settings, connEx: ex);
            }
        }