示例#1
0
        private void Reconnect()
        {
            _logger.WarnFormat("Attempting to reconnect stream for fixtureName=\"{0}\" fixtureId={1}, Attempt {2}", Name, Id, _disconnections + 1);
            var success = false;

            while (!success && _isStreaming)
            {
                try
                {
                    var restItems = FindRelationAndFollow("http://api.sportingsolutions.com/rels/stream/amqp");
                    var amqpLink  = restItems.SelectMany(restItem => restItem.Links).First(restLink => restLink.Relation == "amqp");

                    var amqpUri = new Uri(amqpLink.Href);

                    _connectionFactory.RequestedHeartbeat = 5;

                    var host = amqpUri.Host;
                    if (!String.IsNullOrEmpty(host))
                    {
                        _connectionFactory.HostName = host;
                    }
                    var port = amqpUri.Port;
                    if (port != -1)
                    {
                        _connectionFactory.Port = port;
                    }
                    var userInfo = amqpUri.UserInfo;
                    userInfo = HttpUtility.UrlDecode(userInfo);
                    if (!String.IsNullOrEmpty(userInfo))
                    {
                        var userPass = userInfo.Split(':');
                        if (userPass.Length > 2)
                        {
                            throw new ArgumentException(string.Format("Bad user info in AMQP URI: {0}", userInfo));
                        }
                        _connectionFactory.UserName = userPass[0];
                        if (userPass.Length == 2)
                        {
                            _connectionFactory.Password = userPass[1];
                        }
                    }
                    _queueName = "";
                    var path = amqpUri.AbsolutePath;
                    if (!String.IsNullOrEmpty(path))
                    {
                        _queueName   = path.Substring(path.IndexOf('/', 1) + 1);
                        _virtualHost = path.Substring(1, path.IndexOf('/', 1) - 1);

                        _connectionFactory.VirtualHost = "/" + _virtualHost;
                    }

                    if (_channel != null)
                    {
                        _channel.Close();
                        _channel = null;
                    }

                    try
                    {
                        if (_connection != null)
                        {
                            if (_connection.IsOpen)
                            {
                                _connection.Close();
                            }
                            _connection = null;
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.Error(ex);
                    }

                    _connection = _connectionFactory.CreateConnection();
                    _logger.InfoFormat("Successfully connected to Streaming Server for fixtureName=\"{0}\" fixtureId={1}", Name, Id);

                    StartEcho();

                    if (StreamConnected != null)
                    {
                        StreamConnected(this, new EventArgs());
                    }

                    _channel  = _connection.CreateModel();
                    _consumer = new QueueingCustomConsumer(_channel);
                    _channel.BasicConsume(_queueName, true, _consumer);
                    _channel.BasicQos(0, 10, false);
                    success = true;
                }
                catch (Exception)
                {
                    if (_disconnections > _maxRetries)
                    {
                        _logger.ErrorFormat("Failed to reconnect Stream for fixtureName=\"{0}\" fixtureId={1} ", Name, Id);
                        StopStreaming();
                    }
                    else
                    {
                        // give time for load balancer to notice the node is down
                        Thread.Sleep(500);
                        _disconnections++;
                        _logger.WarnFormat("Failed to reconnect stream for fixtureName=\"{0}\" fixtureId={1}, Attempt {2}", Name, Id, _disconnections);
                    }
                }
            }
        }
        private void Reconnect()
        {
            _logger.WarnFormat("Attempting to reconnect stream for fixtureName=\"{0}\" fixtureId={1}, Attempt {2}",Name,Id,_disconnections+1);
            var success = false;
            while (!success && _isStreaming)
            {
                try
                {
                    var restItems = FindRelationAndFollow("http://api.sportingsolutions.com/rels/stream/amqp");
                    var amqpLink = restItems.SelectMany(restItem => restItem.Links).First(restLink => restLink.Relation == "amqp");

                    var amqpUri = new Uri(amqpLink.Href);

                    _connectionFactory.RequestedHeartbeat = 5;

                    var host = amqpUri.Host;
                    if (!String.IsNullOrEmpty(host))
                    {
                        _connectionFactory.HostName = host;
                    }
                    var port = amqpUri.Port;
                    if (port != -1)
                    {
                        _connectionFactory.Port = port;
                    }
                    var userInfo = amqpUri.UserInfo;
                    userInfo = HttpUtility.UrlDecode(userInfo);
                    if (!String.IsNullOrEmpty(userInfo))
                    {
                        var userPass = userInfo.Split(':');
                        if (userPass.Length > 2)
                        {
                            throw new ArgumentException(string.Format("Bad user info in AMQP URI: {0}", userInfo));
                        }
                        _connectionFactory.UserName = userPass[0];
                        if (userPass.Length == 2)
                        {
                            _connectionFactory.Password = userPass[1];
                        }
                    }
                    _queueName = "";
                    var path = amqpUri.AbsolutePath;
                    if (!String.IsNullOrEmpty(path))
                    {
                        _queueName = path.Substring(path.IndexOf('/', 1) + 1);
                        _virtualHost = path.Substring(1, path.IndexOf('/', 1) - 1);

                        _connectionFactory.VirtualHost = "/" + _virtualHost;
                    }

                    if (_channel != null)
                    {
                        _channel.Close();
                        _channel = null;
                    }

                    try
                    {
                        if (_connection != null)
                        {
                            if (_connection.IsOpen)
                            {
                                _connection.Close();
                            }
                            _connection = null;
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.Error(ex);
                    }

                    _connection = _connectionFactory.CreateConnection();
                    _logger.InfoFormat("Successfully connected to Streaming Server for fixtureName=\"{0}\" fixtureId={1}", Name, Id);

                    StartEcho();

                    if (StreamConnected != null)
                    {
                        StreamConnected(this, new EventArgs());
                    }

                    _channel = _connection.CreateModel();
                    _consumer = new QueueingCustomConsumer(_channel);
                    _channel.BasicConsume(_queueName, true, _consumer);
                    _channel.BasicQos(0, 10, false);
                    success = true;
                }
                catch(Exception)
                {
                    if (_disconnections > _maxRetries)
                    {
                        _logger.ErrorFormat("Failed to reconnect Stream for fixtureName=\"{0}\" fixtureId={1} ",Name, Id);
                        StopStreaming();
                    }
                    else
                    {
                        // give time for load balancer to notice the node is down
                        Thread.Sleep(500);
                        _disconnections++;
                        _logger.WarnFormat("Failed to reconnect stream for fixtureName=\"{0}\" fixtureId={1}, Attempt {2}", Name, Id, _disconnections);
                    }
                }
            }
        }