コード例 #1
0
        public QueueDetails GetQueueDetails()
        {
            var loggingStringBuilder = new StringBuilder();
            var restItems            = FindRelationAndFollow("http://api.sportingsolutions.com/rels/stream/amqp", "GetAmqpStream HTTP error", loggingStringBuilder);

            if (restItems == null)
            {
                return(null);
            }
            var amqpLink = restItems.SelectMany(restItem => restItem.Links).First(restLink => restLink.Relation == "amqp");

            var amqpUri = new Uri(amqpLink.Href);

            var queueDetails = new QueueDetails {
                Host = amqpUri.Host
            };

            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));
                }
                queueDetails.UserName = userPass[0];
                if (userPass.Length == 2)
                {
                    queueDetails.Password = userPass[1];
                }
            }

            var path = amqpUri.AbsolutePath;

            if (!String.IsNullOrEmpty(path))
            {
                queueDetails.Name = path.Substring(path.IndexOf('/', 1) + 1);
                var virtualHost = path.Substring(1, path.IndexOf('/', 1) - 1);

                queueDetails.VirtualHost = virtualHost;
                _virtualHost             = queueDetails.VirtualHost;
            }

            var port = amqpUri.Port;

            if (port != -1)
            {
                queueDetails.Port = port;
            }

            return(queueDetails);
        }
        private void Connect(IConsumer consumer)
        {
            if (State != ConnectionState.CONNECTED)
            {
                // this prevents any other execution of EstablishConnection().
                // EstablishConnection() wakes up any sleeping threads when it finishes

                lock (_connectionLock)
                {
                    while (State == ConnectionState.CONNECTING)
                    {
                        // wait until the connection is established
                        Monitor.Wait(_connectionLock);
                    }

                    if (State == ConnectionState.CONNECTED || _cancellationTokenSource.IsCancellationRequested)
                    {
                        return;
                    }

                    State = ConnectionState.CONNECTING;
                }


                // GetQueueDetails() returns the credentials for connecting to the AMQP server
                // but it also asks the server to create an AMQP queue for the caller.
                // As the time to establish a connection could take a while (just think about
                // a not reliable network), the created AMQP queue could expire before we
                // call BasicConsume() on it.
                //
                // To prevent this situation, we call GetQueueDetails() twice for the consumer
                // who establish the connection, one here, and the second time on
                // AddConsumeToQueue()

                QueueDetails queue = null;
                try
                {
                    queue = consumer.GetQueueDetails();
                    if (queue == null || string.IsNullOrEmpty(queue.Name))
                    {
                        throw new Exception("queue's name is not valid for consumerId=" + consumer.Id);
                    }
                }
                catch (Exception e)
                {
                    _logger.Error("Error acquiring queue details for consumerId=" + consumer.Id, e);
                    OnConnectionStatusChanged(ConnectionState.DISCONNECTED);
                    throw;
                }


                var factory = new ConnectionFactory {
                    RequestedHeartbeat = UDAPI.Configuration.AMQPMissedHeartbeat,
                    HostName           = queue.Host,
                    Port        = queue.Port,
                    UserName    = queue.UserName,
                    Password    = queue.Password,
                    VirtualHost = "/" + queue.VirtualHost // this is not used anymore, we keep it for retro-compatibility
                };

                EstablishConnection(factory);
            }
        }
        public QueueDetails GetQueueDetails()
        {

            var loggingStringBuilder = new StringBuilder();
            var restItems = FindRelationAndFollow("http://api.sportingsolutions.com/rels/stream/amqp", "GetAmqpStream HTTP error", loggingStringBuilder);
            var amqpLink = restItems.SelectMany(restItem => restItem.Links).First(restLink => restLink.Relation == "amqp");

            var amqpUri = new Uri(amqpLink.Href);

            var queueDetails = new QueueDetails { Host = amqpUri.Host };

            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));
                }
                queueDetails.UserName = userPass[0];
                if (userPass.Length == 2)
                {
                    queueDetails.Password = userPass[1];
                }
            }

            var path = amqpUri.AbsolutePath;
            if (!String.IsNullOrEmpty(path))
            {
                queueDetails.Name = path.Substring(path.IndexOf('/', 1) + 1);
                var virtualHost = path.Substring(1, path.IndexOf('/', 1) - 1);

                queueDetails.VirtualHost = virtualHost;
                _virtualHost = queueDetails.VirtualHost;
            }

            var port = amqpUri.Port;
            if (port != -1)
            {
                queueDetails.Port = port;
            }

            return queueDetails;
        }