/// <summary>
        /// Create a connection using an IEndpointResolver.
        /// </summary>
        /// <param name="endpointResolver">
        /// The endpointResolver that returns the endpoints to use for the connection attempt.
        /// </param>
        /// <param name="clientProvidedName">
        /// Application-specific connection name, will be displayed in the management UI
        /// if RabbitMQ server supports it. This value doesn't have to be unique and cannot
        /// be used as a connection identifier, e.g. in HTTP API requests.
        /// This value is supposed to be human-readable.
        /// </param>
        /// <returns>Open connection</returns>
        /// <exception cref="BrokerUnreachableException">
        /// When no hostname was reachable.
        /// </exception>
        public IConnection CreateConnection(IEndpointResolver endpointResolver, string clientProvidedName)
        {
            IConnection conn;

            try
            {
                if (AutomaticRecoveryEnabled)
                {
                    var autorecoveringConnection = new AutorecoveringConnection(this, clientProvidedName);
                    autorecoveringConnection.Init(endpointResolver);
                    conn = autorecoveringConnection;
                }
                else
                {
                    IProtocol protocol = Protocols.DefaultProtocol;
                    conn = protocol.CreateConnection(this, false, endpointResolver.SelectOne(this.CreateFrameHandler), clientProvidedName);
                }
            }
            catch (Exception e)
            {
                throw new BrokerUnreachableException(e);
            }

            return(conn);
        }
Esempio n. 2
0
        /// <summary>
        /// Create a connection using a list of hostnames. The first reachable
        /// hostname will be used initially. Subsequent hostname picks are determined
        /// by the <see cref="IHostnameSelector" /> configured.
        /// </summary>
        /// <param name="hostnames">
        /// List of hostnames to use for the initial
        /// connection and recovery.
        /// </param>
        /// <param name="clientProvidedName">
        /// Application-specific connection name, will be displayed in the management UI
        /// if RabbitMQ server supports it. This value doesn't have to be unique and cannot
        /// be used as a connection identifier, e.g. in HTTP API requests.
        /// This value is supposed to be human-readable.
        /// </param>
        /// <returns>Open connection</returns>
        /// <exception cref="BrokerUnreachableException">
        /// When no hostname was reachable.
        /// </exception>
        public IConnection CreateConnection(IList <string> hostnames, String clientProvidedName)
        {
            IConnection conn;

            try
            {
                if (AutomaticRecoveryEnabled)
                {
                    var autorecoveringConnection = new AutorecoveringConnection(this, clientProvidedName);
                    autorecoveringConnection.Init(hostnames);
                    conn = autorecoveringConnection;
                }
                else
                {
                    IProtocol protocol     = Protocols.DefaultProtocol;
                    var       selectedHost = this.HostnameSelector.NextFrom(hostnames);
                    conn = protocol.CreateConnection(this, false, CreateFrameHandlerForHostname(selectedHost), clientProvidedName);
                }
            }
            catch (Exception e)
            {
                throw new BrokerUnreachableException(e);
            }
            return(conn);
        }
Esempio n. 3
0
        /// <summary>
        /// Create a connection using a list of hostnames. The first reachable
        /// hostname will be used initially. Subsequent hostname picks are determined
        /// by the <see cref="IHostnameSelector" /> configured.
        /// </summary>
        /// <param name="endpoints">
        /// List of endpoints to use for the initial
        /// connection and recovery.
        /// </param>
        /// <param name="clientProvidedName">
        /// Application-specific connection name, will be displayed in the management UI
        /// if RabbitMQ server supports it. This value doesn't have to be unique and cannot
        /// be used as a connection identifier, e.g. in HTTP API requests.
        /// This value is supposed to be human-readable.
        /// </param>
        /// <returns>Open connection</returns>
        /// <exception cref="BrokerUnreachableException">
        /// When no hostname was reachable.
        /// </exception>
        public IConnection CreateConnection(IList <AmqpTcpEndpoint> endpoints, String clientProvidedName)
        {
            var         eps = endpoints.ToList();
            IConnection conn;

            try
            {
                if (AutomaticRecoveryEnabled)
                {
                    var autorecoveringConnection = new AutorecoveringConnection(this, clientProvidedName);
                    autorecoveringConnection.Init(eps);
                    conn = autorecoveringConnection;
                }
                else
                {
                    IProtocol protocol = Protocols.DefaultProtocol;
                    //We can't make this more elegant without changing the contract of the IHostnameSelector
                    //if there are endpoints with the same hostname but different ports the first match is selected
                    var selectedHost     = HostnameSelector.NextFrom(eps.Select(ep => ep.HostName).ToList());
                    var selectedEndpoint = eps.First(ep => ep.HostName == selectedHost);
                    conn = protocol.CreateConnection(this, false, CreateFrameHandler(selectedEndpoint), clientProvidedName);
                }
            }
            catch (Exception e)
            {
                throw new BrokerUnreachableException(e);
            }
            return(conn);
        }
Esempio n. 4
0
        /// <summary>
        /// Create a connection to the specified endpoint.
        /// </summary>
        public virtual IConnection CreateConnection()
        {
            IConnection connection;

            try
            {
                if (AutomaticRecoveryEnabled)
                {
                    var autorecoveringConnection = new AutorecoveringConnection(this);
                    autorecoveringConnection.init();
                    connection = autorecoveringConnection;
                }
                else
                {
                    IProtocol protocol = Protocols.DefaultProtocol;
                    connection = protocol.CreateConnection(this, false, CreateFrameHandler());
                }
            }
            catch (Exception e)
            {
                throw new BrokerUnreachableException(e);
            }

            return(connection);
        }
Esempio n. 5
0
        protected virtual IConnection FollowRedirectChain
            (int maxRedirects,
            IDictionary connectionAttempts,
            IDictionary connectionErrors,
            ref AmqpTcpEndpoint[] mostRecentKnownHosts,
            AmqpTcpEndpoint endpoint)
        {
            AmqpTcpEndpoint candidate = endpoint;

            try {
                while (true)
                {
                    int attemptCount =
                        connectionAttempts.Contains(candidate)
                        ? (int)connectionAttempts[candidate]
                        : 0;
                    connectionAttempts[candidate] = attemptCount + 1;
                    bool insist = attemptCount >= maxRedirects;

                    try {
                        IProtocol     p  = candidate.Protocol;
                        IFrameHandler fh = p.CreateFrameHandler(candidate);
                        // At this point, we may be able to create
                        // and fully open a successful connection,
                        // in which case we're done, and the
                        // connection should be returned.
                        return(p.CreateConnection(m_parameters, insist, fh));
                    } catch (RedirectException re) {
                        if (insist)
                        {
                            // We've been redirected, but we insisted that
                            // we shouldn't be redirected! Well-behaved
                            // brokers should never do this.
                            string message = string.Format("Server {0} ignored 'insist' flag, redirecting us to {1}",
                                                           candidate,
                                                           re.Host);
                            throw new ProtocolViolationException(message);
                        }
                        else
                        {
                            // We've been redirected. Follow this new link
                            // in the chain, by setting
                            // mostRecentKnownHosts (in case the chain
                            // runs out), and updating candidate for the
                            // next time round the loop.
                            connectionErrors[candidate] = re;
                            mostRecentKnownHosts        = re.KnownHosts;
                            candidate = re.Host;
                        }
                    }
                }
            } catch (Exception e) {
                connectionErrors[candidate] = e;
                return(null);
            }
        }
        ///<summary>Create a connection to the specified endpoint.</summary>
        public virtual IConnection CreateConnection()
        {
            IConnection conn = null;

            try
            {
                if (this.AutomaticRecoveryEnabled)
                {
                    AutorecoveringConnection ac = new AutorecoveringConnection(this);
                    ac.init();
                    conn = ac;
                }
                else
                {
                    IProtocol p = Protocols.DefaultProtocol;
                    conn = p.CreateConnection(this, false, this.CreateFrameHandler());
                }
            } catch (Exception e)
            {
                throw new BrokerUnreachableException(e);
            }

            return(conn);
        }