/// <summary> /// Perform a rollback, if appropriate. /// </summary> /// <param name="session">The session to rollback.</param> /// <exception cref="EMSException">In case of a rollback error</exception> protected virtual void RollbackIfNecessary(ISession session) { if (session.Transacted && IsSessionLocallyTransacted(session)) { // Transacted session created by this container -> rollback EmsUtils.RollbackIfNecessary(session); } }
public ISession CreateSession(bool transacted, int acknowledgementMode) { ISession session = singleConnectionFactory.GetSession(target, EmsUtils.ConvertAcknowledgementMode(acknowledgementMode)); if (session != null) { return(session); } return(target.CreateSession(transacted, acknowledgementMode)); }
/// <summary> /// Perform a rollback, if appropriate. /// </summary> /// <param name="session">The session to rollback.</param> /// <exception cref="EMSException">In case of a rollback error</exception> protected virtual void RollbackIfNecessary(ISession session) { if (session.Transacted && IsSessionLocallyTransacted(session)) { // Transacted session created by this container -> rollback EmsUtils.RollbackIfNecessary(session); } else if (IsClientAcknowledge(session)) { session.Recover(); } }
/// <summary> /// Sends the given response message to the given destination. /// </summary> /// <param name="session">The session to operate on.</param> /// <param name="destination">The destination to send to.</param> /// <param name="response">The outgoing message about to be sent.</param> protected virtual void SendResponse(ISession session, Destination destination, Message response) { IMessageProducer producer = session.CreateProducer(destination); try { PostProcessProducer(producer, response); producer.Send(response); } finally { EmsUtils.CloseMessageProducer(producer); } }
/// <summary> /// Creates the shared connection for this container. /// </summary> /// <remarks> /// The default implementation creates a standard Connection /// and prepares it through <see cref="PrepareSharedConnection"/> /// </remarks> /// <returns>the prepared Connection</returns> /// <exception cref="EMSException">if the creation failed.</exception> protected virtual IConnection CreateSharedConnection() { IConnection con = CreateConnection(); try { PrepareSharedConnection(con); return(con); } catch (EMSException) { EmsUtils.CloseConnection(con); throw; } }
/// <summary> /// Perform a commit or message acknowledgement, as appropriate /// </summary> /// <param name="session">The session to commit.</param> /// <param name="message">The message to acknowledge.</param> /// <exception cref="EMSException">In case of commit failure</exception> protected virtual void CommitIfNecessary(ISession session, Message message) { // Commit session or acknowledge message if (session.Transacted) { // Commit necessary - but avoid commit call is Session transaction is externally coordinated. if (IsSessionLocallyTransacted(session)) { EmsUtils.CommitIfNecessary(session); } } else if (IsClientAcknowledge(session)) { message.Acknowledge(); } }
/// <summary> /// Perform a rollback, handling rollback excepitons properly. /// </summary> /// <param name="session">The session to rollback.</param> /// <param name="ex">The thrown application exception.</param> /// <exception cref="EMSException">in case of a rollback error.</exception> protected virtual void RollbackOnExceptionIfNecessary(ISession session, Exception ex) { try { if (session.Transacted && IsSessionLocallyTransacted(session)) { // Transacted session created by this container -> rollback if (logger.IsDebugEnabled) { logger.Debug("Initiating transaction rollback on application exception"); } EmsUtils.RollbackIfNecessary(session); } } catch (EMSException) { logger.Error("Application exception overriden by rollback exception", ex); throw; } }
/// <summary> /// Invoke the specified listener as Spring ISessionAwareMessageListener, /// exposing a new EMS Session (potentially with its own transaction) /// to the listener if demanded. /// </summary> /// <param name="listener">The Spring ISessionAwareMessageListener to invoke.</param> /// <param name="session">The session to operate on.</param> /// <param name="message">The received message.</param> /// <exception cref="EMSException">If thrown by EMS API methods.</exception> /// <see cref="ISessionAwareMessageListener"/> /// <see cref="ExposeListenerSession"/> protected virtual void DoInvokeListener(ISessionAwareMessageListener listener, ISession session, Message message) { IConnection conToClose = null; ISession sessionToClose = null; try { ISession sessionToUse = session; if (!ExposeListenerSession) { //We need to expose a separate Session. conToClose = CreateConnection(); sessionToClose = CreateSession(conToClose); sessionToUse = sessionToClose; } // Actually invoke the message listener if (logger.IsDebugEnabled) { logger.Debug("Invoking listener with message of type [" + message.GetType() + "] and session [" + sessionToUse + "]"); } listener.OnMessage(message, sessionToUse); // Clean up specially exposed Session, if any if (sessionToUse != session) { if (sessionToUse.Transacted && SessionTransacted) { // Transacted session created by this container -> commit. EmsUtils.CommitIfNecessary(sessionToUse); } } } finally { EmsUtils.CloseSession(sessionToClose); EmsUtils.CloseConnection(conToClose); } }
/// <summary> /// Close the message consumers and sessions. /// </summary> /// <throws>EMSException if destruction failed </throws> protected override void DoShutdown() { lock (consumersMonitor) { if (consumers != null) { logger.Debug("Closing EMS MessageConsumers"); foreach (IMessageConsumer messageConsumer in consumers) { EmsUtils.CloseMessageConsumer(messageConsumer); } } if (sessions != null) { logger.Debug("Closing EMS Sessions"); foreach (ISession session in sessions) { EmsUtils.CloseSession(session); } } consumers = null; sessions = null; } }