/// <summary> /// Perform a rollback, if appropriate. /// </summary> /// <param name="session">The session to rollback.</param> /// <exception cref="NMSException">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 NmsUtils.RollbackIfNecessary(session); } }
/// <summary> /// Perform a rollback, if appropriate. /// </summary> /// <param name="session">The session to rollback.</param> /// <exception cref="NMSException">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 NmsUtils.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, IDestination destination, IMessage response) { IMessageProducer producer = session.CreateProducer(destination); try { PostProcessProducer(producer, response); producer.Send(response); } finally { NmsUtils.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="NMSException">if the creation failed.</exception> protected virtual IConnection CreateSharedConnection() { IConnection con = CreateConnection(); try { PrepareSharedConnection(con); return(con); } catch (Exception) { NmsUtils.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="NMSException">In case of commit failure</exception> protected virtual void CommitIfNecessary(ISession session, IMessage message) { // Commit session or acknowledge message if (session.Transacted) { // Commit necessary - but avoid commit call is Session transaction is externally coordinated. if (IsSessionLocallyTransacted(session)) { NmsUtils.CommitIfNecessary(session); } } else if (IsClientAcknowledge(session)) { message.Acknowledge(); } }
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> /// 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="NMSException">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"); } NmsUtils.RollbackIfNecessary(session); } } catch (NMSException) { logger.Error("Application exception overriden by rollback exception", ex); throw; } }
/// <summary> /// Invoke the specified listener as Spring SessionAwareMessageListener, /// exposing a new NMS 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="NMSException">If thrown by NMS API methods.</exception> /// <see cref="ISessionAwareMessageListener"/> /// <see cref="ExposeListenerSession"/> protected virtual void DoInvokeListener(ISessionAwareMessageListener listener, ISession session, IMessage 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. NmsUtils.CommitIfNecessary(sessionToUse); } } } finally { NmsUtils.CloseSession(sessionToClose); NmsUtils.CloseConnection(conToClose); } }
/// <summary> /// Close the message consumers and sessions. /// </summary> /// <throws>NMSException if destruction failed </throws> protected override void DoShutdown() { lock (consumersMonitor) { if (consumers != null) { logger.Debug("Closing NMS MessageConsumers"); foreach (IMessageConsumer messageConsumer in consumers) { NmsUtils.CloseMessageConsumer(messageConsumer); } } if (sessions != null) { logger.Debug("Closing NMS Sessions"); foreach (ISession session in sessions) { NmsUtils.CloseSession(session); } } consumers = null; sessions = null; } }