Beispiel #1
0
        /// <summary>
        /// Deletes all messages in the input queue
        /// </summary>
        public void PurgeInputQueue()
        {
            if (!MsmqUtil.QueueExists(_inputQueueName))
            {
                _log.Info("Purging {0} (but the queue doesn't exist...)", _inputQueueName);
                return;
            }

            _log.Info("Purging {0}", _inputQueueName);
            MsmqUtil.PurgeQueue(_inputQueueName);
        }
Beispiel #2
0
        MessageQueue GetInputQueue()
        {
            if (_inputQueue != null)
            {
                return(_inputQueue);
            }

            lock (this)
            {
                if (_inputQueue != null)
                {
                    return(_inputQueue);
                }

                var inputQueuePath = MsmqUtil.GetPath(_inputQueueName);

                if (_newQueueCallbacks.Any())
                {
                    MsmqUtil.EnsureQueueExists(inputQueuePath, _log, messageQueue =>
                    {
                        _newQueueCallbacks.ForEach(callback => callback(messageQueue));
                    });
                }
                else
                {
                    MsmqUtil.EnsureQueueExists(inputQueuePath, _log);
                }
                MsmqUtil.EnsureMessageQueueIsTransactional(inputQueuePath);

                _inputQueue = new MessageQueue(inputQueuePath, QueueAccessMode.SendAndReceive)
                {
                    MessageReadPropertyFilter = new MessagePropertyFilter
                    {
                        Id        = true,
                        Extension = true,
                        Body      = true,
                    }
                };
            }

            return(_inputQueue);
        }
Beispiel #3
0
        /// <summary>
        /// Creates a queue with the given address, unless the address is of a remote queue - in that case,
        /// this call is ignored
        /// </summary>
        public void CreateQueue(string address)
        {
            if (!MsmqUtil.IsLocal(address))
            {
                return;
            }

            var inputQueuePath = MsmqUtil.GetPath(address);

            if (_newQueueCallbacks.Any())
            {
                MsmqUtil.EnsureQueueExists(inputQueuePath, _log, messageQueue =>
                {
                    _newQueueCallbacks.ForEach(callback => callback(messageQueue));
                });
            }
            else
            {
                MsmqUtil.EnsureQueueExists(inputQueuePath, _log);
            }

            MsmqUtil.EnsureMessageQueueIsTransactional(inputQueuePath);
        }
Beispiel #4
0
        /// <summary>
        /// Sends the given transport message to the specified destination address using MSMQ. Will use the existing <see cref="MessageQueueTransaction"/> stashed
        /// under the <see cref="CurrentTransactionKey"/> key in the given <paramref name="context"/>, or else it will create one and add it.
        /// </summary>
        public async Task Send(string destinationAddress, TransportMessage message, ITransactionContext context)
        {
            if (destinationAddress == null)
            {
                throw new ArgumentNullException(nameof(destinationAddress));
            }
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var logicalMessage = CreateMsmqMessage(message);

            var messageQueueTransaction = context.GetOrAdd(CurrentTransactionKey, () =>
            {
                var transaction = new MessageQueueTransaction();

                transaction.Begin();

                context.OnCommitted(async ctx => transaction.Commit());

                return(transaction);
            });

            var sendQueues = context.GetOrAdd(CurrentOutgoingQueuesKey, () =>
            {
                var messageQueues = new ConcurrentDictionary <string, MessageQueue>(StringComparer.InvariantCultureIgnoreCase);

                context.OnDisposed(ctx =>
                {
                    foreach (var messageQueue in messageQueues.Values)
                    {
                        messageQueue.Dispose();
                    }
                });

                return(messageQueues);
            });

            var path = MsmqUtil.GetFullPath(destinationAddress);

            var sendQueue = sendQueues.GetOrAdd(path, _ =>
            {
                var messageQueue = new MessageQueue(path, QueueAccessMode.Send);

                return(messageQueue);
            });

            try
            {
                sendQueue.Send(logicalMessage, messageQueueTransaction);
            }
            catch (Exception exception)
            {
                throw new RebusApplicationException(exception, $"Could not send to MSMQ queue with path '{sendQueue.Path}'");
            }
        }
        int GetCount()
        {
            var path = MsmqUtil.GetPath(_queueName);

            return(MsmqUtil.GetCount(path));
        }