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; }
protected void OnPermanentConnectionFailureFailure(BrokerUnreachableException e) { var copy = PermanentConnectionFailureHandler; //see http://stackoverflow.com/questions/786383/c-sharp-events-and-thread-safety //this behavior allow thread safety and allow to expose event publicly if (copy != null) try { copy(e); } catch (Exception ee) { OnEventHandlerFailure(ee); } }
private bool Connect(string queueName) { try { if (connection == null || !connection.IsOpen) { logger.Debug(m => m("RMQMessagingGateway: Creating connection to Rabbit MQ on AMPQUri {0}", configuration.AMPQUri.Uri.ToString())); connection = Connect(connectionFactory); logger.Debug(m => m("RMQMessagingGateway: Opening channel to Rabbit MQ on connection {0}", configuration.AMPQUri.Uri.ToString())); channel = OpenChannel(connection); channel.BasicQos(0, 1, false); logger.Debug(m =>m("RMQMessagingGateway: Declaring exchange {0} on connection {1}", configuration.Exchange.Name, configuration.AMPQUri.Uri.ToString())); DeclareExchange(channel, configuration); logger.Debug(m =>m("RMQMessagingGateway: Declaring queue {0} on connection {1}", queueName, configuration.AMPQUri.Uri.ToString())); channel.QueueDeclare(queueName, false, false, false, null); channel.QueueBind(queueName, configuration.Exchange.Name, queueName); } } catch (BrokerUnreachableException e) { connectionFailure = e; return false; } return true; }