Example #1
0
        /// <inheritdoc />
        /// <summary>
        /// Publishes a message to a topic.
        /// </summary>
        /// <param name="message">The message to publish.</param>
        /// <param name="topicName">The name of the topic.</param>
        /// <param name="cancellationToken">A token used by the caller
        /// to indicate if and when the publish operation has been canceled.</param>
        /// <returns>returns a task that completes when the message has
        /// been successfully published to the topic.</returns>
        public async Task PublishMessage(Message message, TopicName topicName, CancellationToken cancellationToken)
        {
            if (_messageJournal != null)
            {
                await _messageJournal.Append(message, MessageJournalCategory.Published, cancellationToken);
            }

            var subscribers = await _subscriptionTrackingService.GetSubscribers(topicName, cancellationToken);

            var transportTasks = new List <Task>();

            foreach (var subscriber in subscribers)
            {
                IEndpointCredentials subscriberCredentials = null;
                if (_endpoints.TryGetEndpointByAddress(subscriber, out var subscriberEndpoint))
                {
                    subscriberCredentials = subscriberEndpoint.Credentials;
                }

                var perEndpointHeaders = new MessageHeaders(message.Headers)
                {
                    MessageId   = MessageId.Generate(),
                    Destination = subscriber
                };

                var addressedMessage = new Message(perEndpointHeaders, message.Content);
                if (addressedMessage.Headers.Synchronous)
                {
                    transportTasks.Add(TransportMessage(addressedMessage, subscriberCredentials, cancellationToken));
                    continue;
                }

                await _messageQueueingService.EnqueueMessage(_outboundQueueName, addressedMessage, null, cancellationToken);
            }

            await Task.WhenAll(transportTasks);
        }
 /// <inheritdoc />
 public Task <IEnumerable <Uri> > GetSubscribers(TopicName topic, CancellationToken cancellationToken = new CancellationToken())
 {
     CheckDisposed();
     return(_inner.GetSubscribers(topic, cancellationToken));
 }