コード例 #1
0
        /// <inheritdoc />
        /// <summary>
        /// Sends a message directly to the application identified by the
        /// <see cref="P:Platibus.IMessageHeaders.Destination" /> header.
        /// </summary>
        /// <param name="message">The message to send.</param>
        /// <param name="credentials">The credentials required to send a
        /// message to the specified destination, if applicable.</param>
        /// <param name="cancellationToken">A token used by the caller to
        /// indicate if and when the send operation has been canceled.</param>
        /// <returns>returns a task that completes when the message has
        /// been successfully sent to the destination.</returns>
        public async Task SendMessage(Message message, IEndpointCredentials credentials = null,
                                      CancellationToken cancellationToken = default(CancellationToken))
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }
            if (message.Headers.Destination == null)
            {
                throw new ArgumentException("Message has no destination");
            }

            if (message.Headers.Synchronous)
            {
                await TransportMessage(message, credentials, cancellationToken);

                return;
            }

            await _messageQueueingService.EnqueueMessage(_outboundQueueName, message, Thread.CurrentPrincipal, cancellationToken);
        }
コード例 #2
0
ファイル: Bus.cs プロジェクト: sweetlandj/Platibus
        private async Task InternalHandleMessage(Message message, IPrincipal principal, CancellationToken cancellationToken = default(CancellationToken))
        {
            await _diagnosticService.EmitAsync(
                new DiagnosticEventBuilder(this, DiagnosticEventType.MessageReceived)
            {
                Message = message
            }.Build(), cancellationToken);

            var tasks   = new List <Task>();
            var isReply = message.Headers.RelatedTo != default(MessageId);

            if (isReply)
            {
                tasks.Add(NotifyReplyReceived(message));
            }

            if (message.Headers.Synchronous)
            {
                tasks.Add(HandleMessageImmediately(message, principal, cancellationToken));
            }
            else
            {
                // Message expiration handled in MessageHandlingListener
                var matchingRules = _handlingRules
                                    .Where(r => r.Specification.IsSatisfiedBy(message))
                                    .ToList();

                if (!matchingRules.Any())
                {
                    await _diagnosticService.EmitAsync(
                        new DiagnosticEventBuilder(this, DiagnosticEventType.NoMatchingHandlingRules)
                    {
                        Message = message
                    }.Build(), cancellationToken);

                    return;
                }

                var handlerQueues = matchingRules.Select(rule => rule.QueueName).Distinct();

                tasks.AddRange(handlerQueues.Select(queue => _messageQueueingService.EnqueueMessage(queue, message, principal, cancellationToken)));
            }

            await Task.WhenAll(tasks);
        }