/// <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
        public void Init(IEndpointResolver endpoints)
        {
            this.endpoints = endpoints;
            var fh = endpoints.SelectOne(m_factory.CreateFrameHandler);

            this.Init(fh);
        }
Esempio n. 3
0
        private bool TryRecoverConnectionDelegate()
        {
            try
            {
                var fh = endpoints.SelectOne(m_factory.CreateFrameHandler);
                m_delegate = new Connection(m_factory, false, fh, this.ClientProvidedName);
                return(true);
            }
            catch (Exception e)
            {
                ESLog.Error("Connection recovery exception.", e);
                // Trigger recovery error events
                var handler = m_connectionRecoveryError;
                if (handler != null)
                {
                    var args = new ConnectionRecoveryErrorEventArgs(e);
                    foreach (EventHandler <ConnectionRecoveryErrorEventArgs> h in handler.GetInvocationList())
                    {
                        try
                        {
                            h(this, args);
                        }
                        catch (Exception ex)
                        {
                            var a = new CallbackExceptionEventArgs(ex);
                            a.Detail["context"] = "OnConnectionRecoveryError";
                            m_delegate.OnCallbackException(a);
                        }
                    }
                }
            }

            return(false);
        }
Esempio n. 4
0
        public void Init(IEndpointResolver endpoints)
        {
            _endpoints = endpoints;
            IFrameHandler fh = endpoints.SelectOne(_factory.CreateFrameHandler);

            Init(fh);
        }
        private bool TryRecoverConnectionDelegate()
        {
            try
            {
                IFrameHandler fh = _endpoints.SelectOne(_factory.CreateFrameHandler);
                _delegate = new Connection(_factory, false, fh, ClientProvidedName);
                return(true);
            }
            catch (Exception e)
            {
                ESLog.Error("Connection recovery exception.", e);
                // Trigger recovery error events
                foreach (EventHandler <ConnectionRecoveryErrorEventArgs> h in ConnectionRecoveryError?.GetInvocationList() ?? Array.Empty <Delegate>())
                {
                    try
                    {
                        h(this, new ConnectionRecoveryErrorEventArgs(e));
                    }
                    catch (Exception ex)
                    {
                        var a = new CallbackExceptionEventArgs(ex);
                        a.Detail["context"] = "OnConnectionRecoveryError";
                        _delegate.OnCallbackException(a);
                    }
                }
            }

            return(false);
        }
        public async Task Init(IEndpointResolver endpoints)
        {
            this.endpoints = endpoints;
            var fh = await endpoints.SelectOne(m_factory.CreateFrameHandler);

            this.Init(fh);
        }
        /// <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)
        {
            ConnectionConfig config = CreateConfig(clientProvidedName);

            try
            {
                if (AutomaticRecoveryEnabled)
                {
                    return(new AutorecoveringConnection(config, endpointResolver));
                }

                return(new Connection(config, endpointResolver.SelectOne(CreateFrameHandler)));
            }
            catch (Exception e)
            {
                throw new BrokerUnreachableException(e);
            }
        }
        protected bool RecoverConnectionDelegate()
        {
            while (!ManuallyClosed)
            {
                try
                {
                    var fh = endpoints.SelectOne(m_factory.CreateFrameHandler);
                    m_delegate = new Connection(m_factory, false, fh, this.ClientProvidedName);
                    return(true);
                }
                catch (Exception e)
                {
                    ESLog.Error("Connection recovery exception.", e);
                    // Trigger recovery error events
                    var handler = m_connectionRecoveryError;
                    if (handler != null)
                    {
                        var args = new ConnectionRecoveryErrorEventArgs(e);
                        foreach (EventHandler <ConnectionRecoveryErrorEventArgs> h in handler.GetInvocationList())
                        {
                            try
                            {
                                h(this, args);
                            }
                            catch (Exception ex)
                            {
                                var a = new CallbackExceptionEventArgs(ex);
                                a.Detail["context"] = "OnConnectionRecoveryError";
                                m_delegate.OnCallbackException(a);
                            }
                        }
                    }

#if NETFX_CORE
                    System.Threading.Tasks.Task.Delay(m_factory.NetworkRecoveryInterval).Wait();
#else
                    Thread.Sleep(m_factory.NetworkRecoveryInterval);
#endif
                }
            }

            return(false);
        }
        protected async Task <bool> RecoverConnectionDelegate()
        {
            while (!ManuallyClosed)
            {
                try
                {
                    var fh = await endpoints.SelectOne(m_factory.CreateFrameHandler);

                    m_delegate = new Connection(m_factory, false, fh, this.ClientProvidedName);
                    await m_delegate.Open();

                    return(true);
                }
                catch (Exception e)
                {
                    ESLog.Error("Connection recovery exception.", e);
                    // Trigger recovery error events
                    var handler = m_connectionRecoveryError;
                    if (handler != null)
                    {
                        var args = new ConnectionRecoveryErrorEventArgs(e);
                        foreach (AsyncEventHandler <ConnectionRecoveryErrorEventArgs> h in handler.GetInvocationList())
                        {
                            try
                            {
                                await h(this, args);
                            }
                            catch (Exception ex)
                            {
                                var a = new CallbackExceptionEventArgs(ex);
                                a.Detail["context"] = "OnConnectionRecoveryError";
                                await m_delegate.OnCallbackException(a);
                            }
                        }
                    }

                    await Task.Delay(m_factory.NetworkRecoveryInterval);
                }
            }

            return(false);
        }
Esempio n. 10
0
        public AutorecoveringConnection(ConnectionConfig config, IEndpointResolver endpoints)
        {
            _config    = config;
            _endpoints = endpoints;

            IFrameHandler fh = _endpoints.SelectOne(_config.FrameHandlerFactory);

            _innerConnection = new Connection(_config, fh);

            Action <Exception, string> onException =
                (exception, context) =>
                _innerConnection.OnCallbackException(CallbackExceptionEventArgs.Build(exception, context));

            _recoverySucceededWrapper              = new EventingWrapper <EventArgs>("OnConnectionRecovery", onException);
            _connectionRecoveryErrorWrapper        = new EventingWrapper <ConnectionRecoveryErrorEventArgs>("OnConnectionRecoveryError", onException);
            _consumerTagChangeAfterRecoveryWrapper = new EventingWrapper <ConsumerTagChangedAfterRecoveryEventArgs>("OnConsumerRecovery", onException);
            _queueNameChangeAfterRecoveryWrapper   = new EventingWrapper <QueueNameChangedAfterRecoveryEventArgs>("OnQueueRecovery", onException);

            ConnectionShutdown += HandleConnectionShutdown;
        }
Esempio n. 11
0
        protected void RecoverConnectionDelegate()
        {
            bool recovering = true;

            while (recovering)
            {
                try
                {
                    var fh     = endpoints.SelectOne(m_factory.CreateFrameHandler);
                    m_delegate = new Connection(m_factory, false, fh, this.ClientProvidedName);
                    recovering = false;
                }
                catch (Exception)
                {
#if NETFX_CORE
                    System.Threading.Tasks.Task.Delay(m_factory.NetworkRecoveryInterval).Wait();
#else
                    Thread.Sleep(m_factory.NetworkRecoveryInterval);
#endif
                    // TODO: provide a way to handle these exceptions
                }
            }
        }
 public void Init(IEndpointResolver endpoints)
 {
     this.endpoints = endpoints;
     var fh = endpoints.SelectOne(m_factory.CreateFrameHandler);
     this.Init(fh);
 }