/// <summary>
        /// Creates the specified subscription.
        /// </summary>
        /// <param name="topicName">Name of the topic.</param>
        /// <param name="subscriptionName">Name of the subscription.</param>
        /// <param name="filter">The filter.</param>
        /// <param name="deadletterName">Name of the dead-letter topic.</param>
        /// <returns>
        ///   <c>true</c> if created, <c>false</c> if already exists.</returns>
        public async Task <bool> CreateSubscription(string topicName, string subscriptionName, string filter = null, string deadletterName = null)
        {
            var createSubscription = new Subscription
            {
                SubscriptionName = new SubscriptionName(_projectId, subscriptionName),
                Topic            = new TopicName(_projectId, topicName).ToString(),
            };

            if (!filter.IsNullOrEmpty())
            {
                createSubscription.Filter = filter;
            }

            if (!deadletterName.IsNullOrEmpty())
            {
                createSubscription.DeadLetterPolicy = new DeadLetterPolicy
                {
                    MaxDeliveryAttempts = 12,
                    DeadLetterTopic     = new TopicName(_projectId, deadletterName).ToString()
                };
            }

            try
            {
                // Create topic subscription.
                await _receiverSubscriptionClient.CreateSubscriptionAsync(createSubscription);

                return(true);
            }
            catch (RpcException ex) when(ex.StatusCode == StatusCode.AlreadyExists)
            {
                return(false);
            }
        }
Example #2
0
        private static async Task <Subscription> EnsureSubscriptionExists(string projectId, string subscriptionId, TopicName topicName)
        {
            // Subscribe to the topic.
            SubscriberServiceApiClient subscriberService = await SubscriberServiceApiClient.CreateAsync();

            SubscriptionName subscriptionName = new SubscriptionName(projectId, subscriptionId);
            Subscription     subscription;

            try
            {
                subscription = await subscriberService.CreateSubscriptionAsync(subscriptionName, topicName, pushConfig : null, ackDeadlineSeconds : 60);
            }
            catch (RpcException e) when(e.Status.StatusCode == StatusCode.AlreadyExists)
            {
                subscription = await subscriberService.GetSubscriptionAsync(subscriptionName);
            }

            return(subscription);
        }
Example #3
0
        public async Task CreateSubscriptionAsync()
        {
            string projectId      = _fixture.ProjectId;
            string topicId        = _fixture.CreateTopicId();
            string subscriptionId = _fixture.CreateSubscriptionId();

            PublisherServiceApiClient.Create().CreateTopic(new TopicName(projectId, topicId));

            // Snippet: CreateSubscriptionAsync(SubscriptionName,TopicName,*,*,CallSettings)
            // Additional: CreateSubscriptionAsync(SubscriptionName,TopicName,*,*,CancellationToken)
            SubscriberServiceApiClient client = SubscriberServiceApiClient.Create();

            SubscriptionName subscriptionName = new SubscriptionName(projectId, subscriptionId);
            TopicName        topicName        = new TopicName(projectId, topicId);
            Subscription     subscription     = await client.CreateSubscriptionAsync(
                subscriptionName, topicName, pushConfig : null, ackDeadlineSeconds : 30);

            Console.WriteLine($"Created {subscription.Name} subscribed to {subscription.Topic}");
            // End snippet
        }
Example #4
0
        private async Task CreateSubscriptionAsync()
        {
            _subscriptionName = SubscriptionName.FromProjectSubscription(_projectId, _inputQueueName);

            _subscriberClient = await GetSubscriberServiceApiClientAsync();

            try
            {
                await _subscriberClient.GetSubscriptionAsync(_subscriptionName);
            }
            catch (RpcException ex) when(ex.StatusCode == StatusCode.NotFound)
            {
                int retries    = 0;
                var maxRetries = 10;

                while (retries < maxRetries)
                {
                    try
                    {
                        await _subscriberClient.CreateSubscriptionAsync(_subscriptionName.ToString(), _inputTopic.ToString(), null, 30, null);

                        //wait after subscription is created - because some delay on google's side
                        await Task.Delay(5000);

                        Log.Info("Created subscription {sub} for topic {topic}", _subscriptionName.ToString(), _inputTopic.ToString());
                        break;
                    }
                    catch (RpcException ex1) when(ex1.StatusCode == StatusCode.NotFound)
                    {
                        Log.Warn("Failed creating subscription {sub} for topic {topic} {times}", _subscriptionName.ToString(), _inputTopic.ToString(), retries + 1);
                        retries++;
                        if (retries == maxRetries)
                        {
                            throw new RebusApplicationException($"Could not create subscription topic {_inputTopic}");
                        }
                        await Task.Delay(1000 *retries);
                    }
                }
            }
        }
Example #5
0
        public async Task InitalizeAsync(CancellationToken cancellationToken)
        {
            PublisherServiceApiClient publisherService = await PublisherServiceApiClient.CreateAsync(cancellationToken);

            SubscriberServiceApiClient subscriberService = await SubscriberServiceApiClient.CreateAsync(cancellationToken);

            // ensure each topic exists
            foreach (string topicId in Topics.AllTopics)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }

                TopicName topicName = new TopicName(m_projectId, topicId);
                try
                {
                    await publisherService.GetTopicAsync(topicName, cancellationToken);
                }
                catch (RpcException)
                {
                    Topic topic = await publisherService.CreateTopicAsync(topicName, cancellationToken);

                    m_logger.Info($"Created topic {topic.Name}");
                }

                m_publisherClients.Add(topicName, await PublisherClient.CreateAsync(topicName));
            }

            // ensure each subscription exists
            foreach (var(topicId, subscriptionId) in Subscriptions.AllSubscriptions)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }

                SubscriptionName subscriptionName = new SubscriptionName(m_projectId, subscriptionId);
                try
                {
                    await subscriberService.GetSubscriptionAsync(subscriptionName, cancellationToken);
                }
                catch (RpcException)
                {
                    Subscription subscription = await subscriberService.CreateSubscriptionAsync(
                        new Subscription
                    {
                        TopicAsTopicName   = new TopicName(m_projectId, topicId),
                        SubscriptionName   = subscriptionName,
                        AckDeadlineSeconds = 30,
                        ExpirationPolicy   = new ExpirationPolicy
                        {
                            Ttl = Duration.FromTimeSpan(TimeSpan.FromDays(365)),
                        },
                    },
                        cancellationToken);

                    m_logger.Info($"Created subscription {subscription.Name}");
                }

                m_subscriberClients.Add(subscriptionName, await SubscriberClient.CreateAsync(subscriptionName));
            }
        }