/// <summary>
        /// Initializes a new instance of the <see cref="ApacheQpidConnectionProvider"/> class.
        /// </summary>
        /// <param name="hostName">Name of the host.</param>
        /// <param name="port">The port.</param>
        public ApacheQpidConnectionProvider(string hostName, string port)
        {
            string broker = string.Format("{0}:{1}", hostName, port);

            connection = null;
            try
            {
                connection = new Org.Apache.Qpid.Messaging.Connection(broker, "{protocol: amqp1.0}");
                connection.SetOption("sasl_mechanisms", "PLAIN");
                connection.SetOption("username", "guest");
                connection.SetOption("password", "guest");
                connection.Open();
                session = connection.CreateSession();
            }
            catch (Exception e)
            {
                Logger.Error("Could not connect to " + hostName + ":" + port);
                Console.WriteLine("Exception {0}.", e);
                if (null != connection)
                {
                    connection.Close();
                }

                return;
            }

            callback = new CallbackServer(session, this);
        }
Exemple #2
0
        /// <summary>
        /// Check and ensure that the connection object is connected.
        /// New connections are established for the first time.
        /// Subsequent calls verify that connection is connected and is not closed or closing.
        /// This function returns only if connection is successfully opened else
        /// a ConnectionClosedException is thrown.
        /// </summary>
        internal void CheckConnected()
        {
            if (closed.Value || closing.Value)
            {
                throw new ConnectionClosedException();
            }
            if (connected.Value)
            {
                return;
            }
            DateTime timeoutTime = DateTime.Now + this.RequestTimeout;
            int      waitCount   = 1;

            while (!connected.Value && !closed.Value && !closing.Value)
            {
                if (Monitor.TryEnter(connectedLock))
                {
                    try // strictly for Monitor unlock
                    {
                        // Create and open the Qpid connection
                        try
                        {
                            // TODO: embellish the brokerUri with other connection options
                            // Allocate a Qpid connection
                            if (qpidConnection == null)
                            {
                                Tracer.DebugFormat("Amqp: create qpid connection to: {0}", this.BrokerUri.ToString());
                                qpidConnection =
                                    new Org.Apache.Qpid.Messaging.Connection(
                                        brokerUri.ToString(),
                                        ConstructConnectionOptionsString(connectionProperties));
                            }

                            // Open the connection
                            if (!qpidConnection.IsOpen)
                            {
                                qpidConnection.Open();
                            }

                            connected.Value = true;
                        }
                        catch (Org.Apache.Qpid.Messaging.QpidException e)
                        {
                            Tracer.DebugFormat("Amqp: create qpid connection to: {0} failed with {1}",
                                               this.BrokerUri.ToString(), e.Message);
                            throw new ConnectionClosedException(e.Message);
                        }
                    }
                    finally
                    {
                        Monitor.Exit(connectedLock);
                    }
                }

                if (connected.Value || closed.Value || closing.Value ||
                    (DateTime.Now > timeoutTime && this.RequestTimeout != InfiniteTimeSpan))
                {
                    break;
                }

                // Back off from being overly aggressive.  Having too many threads
                // aggressively trying to connect to a down broker pegs the CPU.
                Thread.Sleep(5 * (waitCount++));
            }

            if (!connected.Value)
            {
                throw new ConnectionClosedException();
            }
        }
Exemple #3
0
        /// <summary>
        /// Check and ensure that the connection object is connected.  
        /// New connections are established for the first time.
        /// Subsequent calls verify that connection is connected and is not closed or closing.
        /// This function returns only if connection is successfully opened else
        /// a ConnectionClosedException is thrown.
        /// </summary>
        internal void CheckConnected()
        {
            if (closed.Value || closing.Value)
            {
                throw new ConnectionClosedException();
            }
            if (connected.Value)
            {
                return;
            }
            DateTime timeoutTime = DateTime.Now + this.RequestTimeout;
            int waitCount = 1;

            while (!connected.Value && !closed.Value && !closing.Value)
            {
                if (Monitor.TryEnter(connectedLock))
                {
                    try // strictly for Monitor unlock
                    {
                        // Create and open the Qpid connection
                        try
                        {
                            // TODO: embellish the brokerUri with other connection options
                            // Allocate a Qpid connection
                            if (qpidConnection == null)
                            {
                                qpidConnection =
                                    new Org.Apache.Qpid.Messaging.Connection(
                                        brokerUri.ToString(),
                                        ConstructConnectionOptionsString(connectionProperties));
                            }

                            // Open the connection
                            if (!qpidConnection.IsOpen)
                            {
                                qpidConnection.Open();
                            }

                            connected.Value = true;
                        }
                        catch (Org.Apache.Qpid.Messaging.QpidException e)
                        {
                            throw new ConnectionClosedException(e.Message);
                        }
                    }
                    finally
                    {
                        Monitor.Exit(connectedLock);
                    }
                }

                if (connected.Value || closed.Value || closing.Value
                    || (DateTime.Now > timeoutTime && this.RequestTimeout != InfiniteTimeSpan))
                {
                    break;
                }

                // Back off from being overly aggressive.  Having too many threads
                // aggressively trying to connect to a down broker pegs the CPU.
                Thread.Sleep(5 * (waitCount++));
            }

            if (!connected.Value)
            {
                throw new ConnectionClosedException();
            }
        }