Пример #1
0
        internal bool InternalStart(int maxConnectionRetry, ConnectionFailureException callReason = null)
        {
            var ok         = false;
            var retries    = 0;
            var exceptions = new Dictionary <AmqpTcpEndpoint, Exception>(1);
            var attempts   = new Dictionary <AmqpTcpEndpoint, int>(1);

            if (HasAlreadyStartedOnce)
            {
                OnTemporaryConnectionFailureFailure(callReason);
            }
            while (!ok && (retries++ <= maxConnectionRetry || maxConnectionRetry == -1 || maxConnectionRetry == Timeout.Infinite) && !Cancellation.IsCancellationRequested)
            {
                try
                {
                    if (Model != null)
                    {
                        try
                        {
                            Model.Close();
                        }
                        catch
                        {
                            // best effort to close the previous channel, ignore errors
                        }
                    }

                    Connection           = CreateConnection();
                    Model                = Connection.CreateModel();
                    Connection.AutoClose = true;
                    TryRedeclareTopology();
                    SpecificRestart(Model);
                    ok = true;
                    HasAlreadyStartedOnce = true;
                }
                catch (Exception e)
                {
                    var endpoint = new AmqpTcpEndpoint();
                    if (_settings.ConnectionFactory != null && _settings.ConnectionFactory.Endpoint != null)
                    {
                        endpoint = _settings.ConnectionFactory.Endpoint;
                    }
                    exceptions[endpoint] = e;
                    attempts[endpoint]   = retries;
                    OnTemporaryConnectionFailureFailure(e);
                    Thread.Sleep(_settings.IntervalConnectionTries);
                }
            }
            if (!ok)
            {
                var e = new BrokerUnreachableException(attempts, exceptions);
                OnPermanentConnectionFailureFailure(e);
            }
            return(ok);
        }
Пример #2
0
 public ConsumerEventHandler GetDeleteHandler()
 {
     //Will restart everything, that is the connection, the model, the consumer.
     //All messages that were already in treatment are lost and will be delivered again,
     //unless you have taken the responsability to ack messages
     return((_, consumerEventArgs) =>
     {
         var e = new ConnectionFailureException(consumerEventArgs);
         Start(_mySettings.MaxConnectionRetry, e);
     });
 }
Пример #3
0
 private bool Start(int maxConnectionRetry, ConnectionFailureException e)
 {
     lock (_starting)
     {
         var succeeded = InternalStart(maxConnectionRetry);
         if (succeeded)
         {
             // The false for noHack is mandatory. Otherwise it will simply dequeue messages all the time.
             if (_myConsumerTag != null)
             {
                 Model.BasicConsume(_myQueue.Name, false, _myConsumerTag, MyConsumer);
             }
             else
             {
                 _myConsumerTag = Model.BasicConsume(_myQueue.Name, false, MyConsumer);
             }
         }
         return(succeeded);
     }
 }
        internal bool InternalStart(int maxConnectionRetry, ConnectionFailureException callReason = null)
        {
            var ok = false;
            var retries = 0;
            var exceptions = new Dictionary<AmqpTcpEndpoint, Exception>(1);
            var attempts = new Dictionary<AmqpTcpEndpoint, int>(1);
            if (HasAlreadyStartedOnce)
                OnTemporaryConnectionFailureFailure(callReason);
            while (!ok && (retries++ <= maxConnectionRetry || maxConnectionRetry == -1 || maxConnectionRetry == Timeout.Infinite) && !Cancellation.IsCancellationRequested)
            {
                try
                {
                    if (Model != null)
                    {
                        try
                        {
                            Model.Close();
                        }
                        catch
                        {
                            // best effort to close the previous channel, ignore errors
                        }
                    }

                    Connection = CreateConnection();
                    Model = Connection.CreateModel();
                    Connection.AutoClose = true;
                    TryRedeclareTopology();
                    SpecificRestart(Model);
                    ok = true;
                    HasAlreadyStartedOnce = true;
                }
                catch (Exception e)
                {
                    var endpoint = new AmqpTcpEndpoint();
                    if (_settings.ConnectionFactory != null && _settings.ConnectionFactory.Endpoint != null)
                        endpoint = _settings.ConnectionFactory.Endpoint;
                    exceptions[endpoint] = e;
                    attempts[endpoint] = retries;
                    OnTemporaryConnectionFailureFailure(e);
                    Thread.Sleep(_settings.IntervalConnectionTries);
                }
            }
            if (!ok)
            {
                var e = new BrokerUnreachableException(attempts, exceptions);
                OnPermanentConnectionFailureFailure(e);
            }
            return ok;
        }
Пример #5
0
 private bool Start(int maxConnectionRetry, ConnectionFailureException e)
 {
     lock (_starting)
     {
         var succeeded = InternalStart(maxConnectionRetry);
         if (succeeded)
         {
             // The false for noHack is mandatory. Otherwise it will simply dequeue messages all the time.
             if (_myConsumerTag != null)
                 Model.BasicConsume(_myQueue.Name, false, _myConsumerTag, MyConsumer);
             else
                 _myConsumerTag = Model.BasicConsume(_myQueue.Name, false, MyConsumer);
         }
         return succeeded;
     }
 }
Пример #6
0
 public ConsumerShutdownEventHandler GetShutdownHandler()
 {
     //Will restart everything, that is the connection, the model, the consumer.
     //All messages that were already in treatment are lost and will be delivered again,
     //unless you have taken the responsability to ack messages
     return (_, shutdownEventArgs) =>
         {
             var e = new ConnectionFailureException(shutdownEventArgs);
             Start(_mySettings.MaxConnectionRetry, e);
         };
 }