Пример #1
0
        public async Task Start()
        {
            var topicName        = _azureServiceBusConsumerConfiguration.TopicName;
            var policyName       = _azureServiceBusConsumerConfiguration.PolicyName;
            var subscriptionName = _azureServiceBusConsumerConfiguration.SubscriptionName;

            _subscriptionClient = await _subscriptionClientFactory.Create(topicName, subscriptionName, policyName);

            RegisterHandlers();
        }
Пример #2
0
        public async Task ConfigureSubscriptionsAsync()
        {
            foreach (var client in _clients)
            {
                await client.CloseAsync();
            }

            var clients     = new List <ISubscriptionClient>();
            var topicExists = await _managementClient.TopicExistsAsync(_options.Value.Topic.Name);

            if (!topicExists)
            {
                await _managementClient.CreateTopicAsync(_options.Value.Topic.Name, _options.Value.Topic.DeleteOnIdleAfter, _options.Value.Topic.TimeToLive);
            }

            foreach (var subscription in _options.Value.Subscriptions)
            {
                var client             = _subscriptionClientFactory.Create(subscription);
                var subscriptionExists = await _managementClient.SubscriptionExistsAsync(client.SubscriptionName, client.TopicPath);

                if (!subscriptionExists)
                {
                    await _managementClient.CreateSubscriptionAsync(client.SubscriptionName, client.TopicPath);
                }

                var rules = await client.GetRulesAsync();

                if (subscription.Events.Any() && rules.Any(rule => rule.Name == RuleDescription.DefaultRuleName))
                {
                    await client.RemoveRuleAsync(RuleDescription.DefaultRuleName);
                }

                var rulesToAdd    = subscription.Events.Where(@event => !rules.Any(rule => rule.Name == @event.Name));
                var rulesToRemove = rules.Where(rule => !subscription.Events.Any(@event => @event.Name == rule.Name))
                                    .Where(rule => rule.Name != RuleDescription.DefaultRuleName);

                foreach (var type in rulesToAdd)
                {
                    await client.AddRuleAsync(new RuleDescription(filter : new CorrelationFilter {
                        Label = type.Name
                    }, name : type.Name));
                }

                foreach (var rule in rulesToRemove)
                {
                    await client.RemoveRuleAsync(rule.Name);
                }

                clients.Add(client);
            }

            _clients = clients;
        }
Пример #3
0
        public void Dispose_WhenHaventAlreadyBeenDisposed_CloseAllSubscriptionClients()
        {
            // Arrange
            var subscriptionClient1 = fixture.Create <ISubscriptionClient>();
            var subscriptionClient2 = fixture.Create <ISubscriptionClient>();

            subscriptionClientFactory.Create(configuration.ServiceBusConnectionString, TopicName1, Subscriptioname1,
                                             RetryPolicyBase.DefaultRetry)
            .Returns(subscriptionClient1);
            subscriptionClientFactory.Create(configuration.ServiceBusConnectionString, TopicName2, Subscriptioname2,
                                             RetryPolicyBase.DefaultRetry)
            .Returns(subscriptionClient2);

            sut.StartReceivingEvents();

            // Act
            sut.Dispose();

            // Assert
            subscriptionClient1.Received(1).CloseAsync();
            subscriptionClient2.Received(1).CloseAsync();
        }
Пример #4
0
        private void InitializeSubscription(ISubscriptionInfo subscriptionInfo)
        {
            if (disposed)
            {
                throw new ObjectDisposedException(nameof(subscriptionClients));
            }

            lock (thisLock)
            {
                if (subscriptionClients.ContainsKey(subscriptionInfo.TopicName))
                {
                    return;
                }
            }

            var subscriptionClient = subscriptionClientFactory.Create(
                configuration.ServiceBusConnectionString,
                subscriptionInfo.TopicName,
                subscriptionInfo.SubscriptionName,
                subscriptionInfo.RetryPolicy);

            // Register the function that processes messages.
            subscriptionClient.RegisterMessageHandler(
                async(message, token) =>
            {
                await ProcessMessagesAsync(message).ConfigureAwait(false);
                await subscriptionClient.CompleteAsync(message.SystemProperties.LockToken).ConfigureAwait(false);
            },
                new MessageHandlerOptions(ExceptionReceivedHandlerAsync)
            {
                // Maximum number of concurrent calls to the callback ProcessMessagesAsync(), set to 1 for simplicity.
                // Set it according to how many messages the application wants to process in parallel.
                MaxConcurrentCalls = 1,

                // Indicates whether the message pump should automatically complete the messages after returning from user callback.
                // False below indicates the complete operation is handled by the user callback as in ProcessMessagesAsync().
                AutoComplete = false
            });


            lock (thisLock)
            {
                subscriptionClients.Add(subscriptionInfo.TopicName, subscriptionClient);
            }
        }
        public async Task <IReadOnlyList <ISubscriptionClient> > ConfigureAsync()
        {
            // If we've already been configured, just close the existing clients for now.
            foreach (var client in _clients)
            {
                await client.CloseAsync();
            }

            var clients     = new List <ISubscriptionClient>();
            var topicExists = await _managementClient.TopicExistsAsync(_options.Value.Topic.Name);

            if (!topicExists)
            {
                await _managementClient.CreateTopicAsync(_options.Value.Topic.Name, _options.Value.Topic.DeleteOnIdleAfter, _options.Value.Topic.TimeToLive);
            }

            foreach (var subscription in _options.Value.Subscriptions)
            {
                var client             = _subscriptionClientFactory.Create(subscription);
                var subscriptionExists = await _managementClient.SubscriptionExistsAsync(client.SubscriptionName, client.TopicPath);

                if (!subscriptionExists)
                {
                    await _managementClient.CreateSubscriptionAsync(client.SubscriptionName, client.TopicPath);
                }

                var rules = await client.GetRulesAsync();

                if (subscription.Events.Any() && rules.Any(rule => rule.Name == RuleDescription.DefaultRuleName))
                {
                    await client.RemoveRuleAsync(RuleDescription.DefaultRuleName);
                }

                var rulesToAdd    = subscription.Events.Where(@event => !rules.Any(rule => rule.Name == @event.Name));
                var rulesToRemove = rules.Where(rule => !subscription.Events.Any(@event => @event.Name == rule.Name))
                                    .Where(rule => rule.Name != RuleDescription.DefaultRuleName);

                foreach (var type in rulesToAdd)
                {
                    await client.AddRuleAsync(new RuleDescription(filter : new CorrelationFilter {
                        Label = type.Name
                    }, name : type.Name));
                }

                foreach (var rule in rulesToRemove)
                {
                    await client.RemoveRuleAsync(rule.Name);
                }

                client.RegisterMessageHandler((message, cancelationToken) => _messageReceiver.ReceiveAsync(client, message, cancelationToken),
                                              new MessageHandlerOptions(_messageReceiver.OnErrorAsync)
                {
                    AutoComplete = false
                });

                clients.Add(client);
            }

            _clients = clients;

            return(_clients.AsReadOnly());
        }