/// <summary> Execute the action specified by the given action object within a /// NMS Session. /// </summary> /// <remarks> Generalized version of <code>execute(ISessionCallback)</code>, /// allowing the NMS Connection to be started on the fly. /// <p>Use <code>execute(ISessionCallback)</code> for the general case. /// Starting the NMS Connection is just necessary for receiving messages, /// which is preferably achieved through the <code>receive</code> methods.</p> /// </remarks> /// <param name="action">callback object that exposes the session /// </param> /// <param name="startConnection">Start the connection before performing callback action. /// </param> /// <returns> the result object from working with the session /// </returns> /// <throws> NMSException if there is any problem </throws> public virtual object Execute(ISessionCallback action, bool startConnection) { AssertUtils.ArgumentNotNull(action, "Callback object must not be null"); IConnection conToClose = null; ISession sessionToClose = null; try { ISession sessionToUse = ConnectionFactoryUtils.DoGetTransactionalSession(ConnectionFactory, transactionalResourceFactory, startConnection); if (sessionToUse == null) { conToClose = CreateConnection(); sessionToClose = CreateSession(conToClose); if (startConnection) { conToClose.Start(); } sessionToUse = sessionToClose; } if (logger.IsDebugEnabled) { logger.Debug("Executing callback on NMS ISession [" + sessionToUse + "]"); } return action.DoInNms(sessionToUse); } finally { NmsUtils.CloseSession(sessionToClose); ConnectionFactoryUtils.ReleaseConnection(conToClose, ConnectionFactory, startConnection); } }