예제 #1
0
        /// <summary>
        /// Flushes the message box message given by <param name="posts"> to the broker.
        /// Intended for use with the Outbox pattern: http://gistlabs.com/2014/05/the-outbox/ <see cref="DepositPostBoxAsync"/>
        /// <param name="posts">The posts to flush</param>
        public async Task ClearPostBoxAsync(IEnumerable <Guid> posts, bool continueOnCapturedContext = false, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (_asyncMessageStore == null)
            {
                throw new InvalidOperationException("No async message store defined.");
            }
            if (_asyncMessageProducer == null)
            {
                throw new InvalidOperationException("No async message producer defined.");
            }

            foreach (var messageId in posts)
            {
                var message = await _asyncMessageStore.GetAsync(messageId, _messageStoreTimeout, cancellationToken);

                if (message == null)
                {
                    throw new NullReferenceException($"Message with Id {messageId} not found in the Message Store");
                }

                _logger.Value.InfoFormat("Decoupled invocation of message: Topic:{0} Id:{1}", message.Header.Topic, messageId.ToString());

                await RetryAndBreakCircuitAsync(
                    async ct => await _asyncMessageProducer.SendAsync(message).ConfigureAwait(continueOnCapturedContext),
                    continueOnCapturedContext, cancellationToken).ConfigureAwait(continueOnCapturedContext);
            }
        }