コード例 #1
0
        /// <summary>
        /// Send the message queue selecting the appropriate transactional delivery options.
        /// </summary>
        /// <remarks>
        /// <para>
        /// If the message queue is transactional and there is an ambient MessageQueueTransaction
        /// in thread local storage (put there via the use of Spring's MessageQueueTransactionManager
        /// or TransactionalMessageListenerContainer), the message will be sent transactionally using the
        /// MessageQueueTransaction object in thread local storage. This lets you group together multiple
        /// messaging operations within the same transaction without having to explicitly pass around the
        /// MessageQueueTransaction object.
        /// </para>
        /// <para>
        /// If the message queue is transactional but there is no ambient MessageQueueTransaction,
        /// then a single message transaction is created on each messaging operation.
        /// (MessageQueueTransactionType = Single).
        /// </para>
        /// <para>
        /// If there is an ambient System.Transactions transaction then that transaction will
        /// be used (MessageQueueTransactionType = Automatic).
        /// </para>
        /// <para>
        /// If the queue is not transactional, then a non-transactional send
        /// (MessageQueueTransactionType = None) is used.
        /// </para>
        /// </remarks>
        /// <param name="mq">The mq.</param>
        /// <param name="msg">The MSG.</param>
        protected virtual void DoSendMessageQueue(MessageQueue mq, Message msg)
        {
            MessageQueueTransaction transactionToUse = QueueUtils.GetMessageQueueTransaction(null);

            if (metadataCache != null)
            {
                MessageQueueMetadata mqMetadata = metadataCache.Get(mq.Path);
                if (mqMetadata != null)
                {
                    if (mqMetadata.RemoteQueue)
                    {
                        if (mqMetadata.RemoteQueueIsTransactional)
                        {
                            // DefaultMessageQueue transaction is externally managed.
                            DoSendMessageTransaction(mq, transactionToUse, msg);
                            return;
                        }
                        DoSendMessageQueueNonTransactional(mq, transactionToUse, msg);
                        return;
                    }
                }
            }
            else
            {
                if (LOG.IsWarnEnabled)
                {
                    LOG.Warn("MetadataCache has not been initialized.  Set the MetadataCache explicitly in standalone usage and/or " +
                             "configure the MessageQueueTemplate in an ApplicationContext.  If deployed in an ApplicationContext by default " +
                             "the MetadataCache will automaticaly populated.");
                }
            }
            // Handle assuming these are local queues.

            if (mq.Transactional)
            {
                // DefaultMessageQueue transaction is externally managed.
                DoSendMessageTransaction(mq, transactionToUse, msg);
            }
            else
            {
                DoSendMessageQueueNonTransactional(mq, transactionToUse, msg);
            }
        }