Пример #1
0
            public object GetLink(AmqpChannelProperties channelProperties, bool sessionSharing, string inputQueue, string outputQueue)
            {
                AmqpConnection connection    = null;
                AmqpSession    session       = null;
                Object         link          = null;
                bool           newConnection = false;
                //bool newSession = false;
                bool success = false;

                // when called in the non-shared case, only stack variables should be used for holding connections/sessions/links

                if (this.shared)
                {
                    Monitor.Enter(this); // lock
                }

                try
                {
                    if (this.shared)
                    {
                        // TODO: check shared connection not closed (i.e. network drop) and refresh this instance if needed
                        if (sessionSharing)
                        {
                            throw new NotImplementedException("shared session");

                            /* * ... once we have a defined shared session config parameter:
                             *
                             * // lazilly create
                             * if (this.sharedSessions == null)
                             * {
                             *  this.sharedSessions = new Dictionary<string, AmqpSession>();
                             * }
                             *
                             * alreadydeclaredstring sessionKey = channelProperties.name_of_key_goes_here;
                             * this.sharedSessions.TryGetValue(sessionKey, out session);
                             *
                             * */
                        }

                        if (this.sharedConnection != null)
                        {
                            connection = this.sharedConnection;
                        }
                    }

                    if (connection == null)
                    {
                        if (channelProperties.AmqpSecurityMode != AmqpSecurityMode.None)
                        {
                            string user      = null;
                            string passwd    = null;
                            bool   ssl       = false;
                            bool   saslPlain = false;

                            AmqpTransportSecurity tsec = channelProperties.AmqpTransportSecurity;
                            if (tsec.UseSSL)
                            {
                                ssl = true;
                            }

                            if (tsec.CredentialType == AmqpCredentialType.Plain)
                            {
                                saslPlain = true;
                                AmqpCredential plainCred = channelProperties.AmqpCredential;
                                if (plainCred != null)
                                {
                                    user   = plainCred.UserName;
                                    passwd = plainCred.Password;
                                }
                            }

                            connection = new AmqpConnection(channelProperties.BrokerHost, channelProperties.BrokerPort,
                                                            ssl, saslPlain, user, passwd);
                        }
                        else
                        {
                            connection = new AmqpConnection(channelProperties.BrokerHost, channelProperties.BrokerPort);
                        }

                        newConnection = true;
                        if (this.shared)
                        {
                            connection.OnConnectionIdle += new ConnectionIdleEventHandler(this.IdleConnectionHandler);
                        }
                        else
                        {
                            connection.OnConnectionIdle += new ConnectionIdleEventHandler(UnsharedIdleConnectionHandler);
                        }
                    }

                    if (session == null)
                    {
                        session = connection.CreateSession();
                        //newSession = true;
                    }

                    if (inputQueue != null)
                    {
                        link = session.CreateInputLink(inputQueue);
                    }
                    else
                    {
                        link = session.CreateOutputLink(outputQueue);
                    }

                    if (this.shared)
                    {
                        if (newConnection)
                        {
                            this.sharedConnection = connection;
                        }

                        /*
                         * if (newSession)
                         * {
                         *  sharedSessions.Add(foo, session);
                         * }
                         * */
                    }

                    success = true;
                }
                finally
                {
                    if (this.shared)
                    {
                        Monitor.Exit(this);
                    }
                    if (!success)
                    {
                        /*
                         * if (newSession)
                         * {
                         *  session.Close();
                         * }
                         */
                        if (newConnection)
                        {
                            connection.Close();
                        }
                    }
                }

                return(link);
            }