/// <summary>Executes the specified action.</summary>
        /// <typeparam name="T">Type T.</typeparam>
        /// <param name="action">The action.</param>
        /// <returns>Object of Type T.</returns>
        public T Execute <T>(ConnectionCallbackDelegate <T> action)
        {
            AssertUtils.ArgumentNotNull(action, "Callback object must not be null");
            IConnection con = null;

            try
            {
                con = this.CreateConnection();
                return(action(con));
            }
            catch (OtpException ex)
            {
                throw;
            }
            catch (Exception ex)
            {
                Logger.Error("An error occurred executing the action", ex);
                throw this.ConvertOtpAccessException(ex);
            }
            finally
            {
                ConnectionFactoryUtils.ReleaseConnection(con, this.ConnectionFactory);
            }

            // TODO: physically close and reopen the connection if there is an exception
        }
        /// <summary>
        /// Initializes this container.  Creates a Connection, starts the Connection
        /// (if the property <see cref="AutoStartup"/> hasn't been turned off), and calls
        /// <see cref="DoInitialize"/>.
        /// </summary>
        /// <exception cref="NMSException">If startup failed</exception>
        public virtual void Initialize()
        {
            try
            {
                lock (this.lifecycleMonitor)
                {
                    this.active = true;
                    System.Threading.Monitor.PulseAll(this.lifecycleMonitor);
                }

                if (this.autoStartup)
                {
                    DoStart();
                }

                DoInitialize();
            }
            catch (Exception)
            {
                lock (this.sharedConnectionMonitor)
                {
                    ConnectionFactoryUtils.ReleaseConnection(sharedConnection, ConnectionFactory, autoStartup);
                }
                throw;
            }
        }
 /// <summary>
 /// Refreshes the shared connection that this container holds.
 /// </summary>
 /// <remarks>
 /// Called on startup and also after an infrastructure exception
 /// that occurred during invoker setup and/or execution.
 /// </remarks>
 /// <exception cref="NMSException">If thrown by NMS API methods</exception>
 protected void RefreshSharedConnection()
 {
     lock (sharedConnectionMonitor)
     {
         ConnectionFactoryUtils.ReleaseConnection(sharedConnection, ConnectionFactory, sharedConnectionStarted);
         sharedConnection = CreateSharedConnection();
         if (sharedConnectionStarted)
         {
             sharedConnection.Start();
         }
     }
 }
        private Apache.NMS.IMessage SendAndReceive(IMessage requestMessage)
        {
            IConnection      connection      = CreateConnection();
            ISession         session         = null;
            IMessageProducer messageProducer = null;
            IMessageConsumer messageConsumer = null;
            IDestination     replyTo         = null;

            try
            {
                session = CreateSession(connection);
                Apache.NMS.IMessage jmsRequest = this.messageConverter.ToMessage(requestMessage, session);
                messageProducer = session.CreateProducer(this.GetRequestDestination(session));
                messageProducer.DeliveryMode = deliveryMode;
                messageProducer.Priority     = priority;
                messageProducer.TimeToLive   = timeToLive;
                replyTo = GetReplyDestination(session);
                jmsRequest.NMSReplyTo = replyTo;
                connection.Start();
                messageProducer.Send(jmsRequest);
                if (replyTo is ITemporaryQueue || replyTo is ITemporaryTopic)
                {
                    messageConsumer = session.CreateConsumer(replyTo);
                }
                else
                {
                    String messageId       = jmsRequest.NMSMessageId.Replace("'", "''");
                    String messageSelector = "NMSCorrelationID = '" + messageId + "'";
                    messageConsumer = session.CreateConsumer(replyTo, messageSelector);
                }
                return((TimeSpan.Compare(receiveTimeout, TimeSpan.FromMilliseconds(0)) == 1)
                           ? messageConsumer.Receive(receiveTimeout)
                           : messageConsumer.Receive());
            }
            finally
            {
                NmsUtils.CloseMessageProducer(messageProducer);
                NmsUtils.CloseMessageConsumer(messageConsumer);
                this.DeleteDestinationIfTemporary(session, replyTo);
                NmsUtils.CloseSession(session);

                ConnectionFactoryUtils.ReleaseConnection(connection, this.connectionFactory, true);
            }
        }
        /// <summary>
        /// Stop the shared connection, call <see cref="DoShutdown"/>, and close this container.
        /// </summary>
        public virtual void Shutdown()
        {
            logger.Debug("Shutting down message listener container");
            bool wasRunning = false;

            lock (this.lifecycleMonitor)
            {
                wasRunning   = this.running;
                this.running = false;
                this.active  = false;
                System.Threading.Monitor.PulseAll(this.lifecycleMonitor);
            }

            if (wasRunning && SharedConnectionEnabled)
            {
                try
                {
                    StopSharedConnection();
                } catch (Exception ex)
                {
                    logger.Debug("Could not stop NMS Connection on shutdown", ex);
                }
            }

            // Shut down the invokers
            try
            {
                DoShutdown();
            }
            finally
            {
                lock (this.sharedConnectionMonitor)
                {
                    ConnectionFactoryUtils.ReleaseConnection(this.sharedConnection, ConnectionFactory, false);
                }
            }
        }