コード例 #1
0
        public void Open(CancellationToken token)
        {
            this.logger.Info($"Connecting to RabbitMQ using [{this.ConnectionString}].");

            var clientProperties = new Dictionary <string, object>
            {
                {
                    "Endpoint",
                    this.endpoint.Address
                },
                { "Machine", Environment.MachineName },
                {
                    "Location",
                    Path.GetDirectoryName(
                        Assembly.GetExecutingAssembly().CodeBase)
                }
            };

            var connectionFactory = new ConnectionFactory
            {
                Uri = this.ConnectionString,
                ClientProperties           = clientProperties,
                RequestedConnectionTimeout = ConnectionTimeout
            };

            var retryCount = 0;

            while (!token.IsCancellationRequested)
            {
                INativeConnection con = null;
                try
                {
                    con = connectionFactory.CreateConnection();
                    con.ConnectionShutdown += this.OnConnectionShutdown;
                    this.connection         = con;
                    this.OnOpened();
                    this.logger.Info($"Connection [{this.Id}] opened at [{this.connection.Endpoint}]");

                    return;
                }
                catch (Exception ex)
                {
                    var secondsToRetry = Math.Min(10, retryCount);

                    this.logger.WarnFormat(
                        "Unable to connect to RabbitMQ. Retrying in {0} seconds...",
                        ex,
                        secondsToRetry);

                    if (con != null)
                    {
                        con.ConnectionShutdown -= this.OnConnectionShutdown;
                        con.Abort(OperationTimeout);
                    }

                    Thread.Sleep(TimeSpan.FromSeconds(secondsToRetry));
                    retryCount++;
                }
            }
        }
コード例 #2
0
        public void Open(CancellationToken token)
        {
            lock (this.syncRoot)
            {
                if (this.connection?.IsOpen ?? false)
                {
                    this.logger.Trace($"Connection [{this.Id}] is already open");
                    return;
                }

                this.logger.Info($"Connecting to RabbitMQ using [{this.ConnectionString}]");

                var retryCount = 0;
                while (true)
                {
                    token.ThrowIfCancellationRequested();

                    INativeConnection con = null;
                    try
                    {
                        con = this.connectionFactory.CreateConnection();
                        con.ConnectionShutdown += this.OnConnectionShutdown;
                        this.connection         = con;
                        this.OnOpened();
                        this.logger.Info($"Connection [{this.Id}] opened at [{this.connection.Endpoint}]");

                        return;
                    }
                    catch (Exception ex)
                    {
                        var secondsToRetry = Math.Min(10, retryCount);

                        this.logger.WarnFormat(
                            "Unable to connect to RabbitMQ. Retrying in {0} seconds...",
                            ex,
                            secondsToRetry);

                        if (con != null)
                        {
                            con.ConnectionShutdown -= this.OnConnectionShutdown;
                            con.Abort(OperationTimeout);
                        }

                        Thread.Sleep(TimeSpan.FromSeconds(secondsToRetry));
                        retryCount++;
                    }
                }
            }
        }