Example #1
0
        /// <summary>
        /// Performs final initialization of the transport service
        /// </summary>
        /// <param name="cancellationToken">A cancellation token that can be used by the caller
        /// can use to cancel the initialization process</param>
        /// <returns>Returns a task that completes when initialization is complete</returns>
        public async Task Init(CancellationToken cancellationToken = default(CancellationToken))
        {
            if (Interlocked.Exchange(ref _initialized, 1) == 0)
            {
                try
                {
                    await _messageQueueingService.CreateQueue(_outboundQueueName, this,
                                                              cancellationToken : cancellationToken);

                    await _diagnosticService.EmitAsync(
                        new DiagnosticEventBuilder(this, DiagnosticEventType.ComponentInitialization)
                    {
                        Detail = "HTTP transport service initialized"
                    }.Build(), cancellationToken);
                }
                catch (Exception ex)
                {
                    _diagnosticService.Emit(
                        new DiagnosticEventBuilder(this, DiagnosticEventType.ComponentInitializationError)
                    {
                        Detail    = "Error initializating outbound transport queue",
                        Exception = ex
                    }.Build());
                    throw;
                }
            }
        }
Example #2
0
        /// <summary>
        /// Initializes the bus instance
        /// </summary>
        /// <param name="cancellationToken">(Optional) A cancellation token provided by the
        /// caller that can be used to indicate that initialization should be canceled</param>
        /// <returns>Returns a task that will complete when bus initialization is complete</returns>
        /// <remarks>
        /// During initialization all handler queues are initialized and listeners are started.
        /// Additionally, subscriptions are initiated through the <see cref="ITransportService"/>.
        /// </remarks>
        public async Task Init(CancellationToken cancellationToken = default(CancellationToken))
        {
            var handlingRulesGroupedByQueueName = _handlingRules
                                                  .GroupBy(r => r.QueueName)
                                                  .ToDictionary(grp => grp.Key, grp => grp);

            foreach (var ruleGroup in handlingRulesGroupedByQueueName)
            {
                var queueName = ruleGroup.Key;
                var rules     = ruleGroup.Value;
                var handlers  = rules.Select(r => r.MessageHandler);

                var queueOptions = rules
                                   .OrderBy(r => QueueOptionPrecedence(r.QueueOptions))
                                   .Select(r => r.QueueOptions)
                                   .FirstOrDefault();

                var queueListener = new MessageHandlingListener(this, _messageHandler, handlers);

                await _messageQueueingService.CreateQueue(queueName, queueListener, queueOptions, cancellationToken);
            }

            foreach (var subscription in _subscriptions)
            {
                var endpoint = _endpoints[subscription.Endpoint];

                // The returned task will no complete until the subscription is
                // canceled via the supplied cancelation token, so we shouldn't
                // await it.

                var subscriptionTask = _transportService.Subscribe(endpoint, subscription.Topic, subscription.TTL,
                                                                   _cancellationTokenSource.Token);

                _subscriptionTasks.Add(subscriptionTask);
            }

            await _diagnosticService.EmitAsync(
                new DiagnosticEventBuilder(this, DiagnosticEventType.BusInitialized).Build(),
                cancellationToken);
        }