public string AwakeThread()
        {
            ReceiptThread.AwakenThread();
            int cacheQueue = QueueCache.GetCacheLength();

            return(JsonConvert.SerializeObject(new
            {
                cacheQueue = cacheQueue
            }));
        }
Exemplo n.º 2
0
        public SqsTransportInfrastructure(ReadOnlySettings settings)
        {
            this.settings           = settings;
            messageMetadataRegistry = this.settings.Get <MessageMetadataRegistry>();
            configuration           = new TransportConfiguration(settings);

            if (settings.HasSetting(SettingsKeys.DisableNativePubSub))
            {
                OutboundRoutingPolicy = new OutboundRoutingPolicy(OutboundRoutingType.Unicast, OutboundRoutingType.Unicast, OutboundRoutingType.Unicast);
            }
            else
            {
                OutboundRoutingPolicy = new OutboundRoutingPolicy(OutboundRoutingType.Unicast, OutboundRoutingType.Multicast, OutboundRoutingType.Unicast);
            }

            try
            {
                sqsClient = configuration.SqsClientFactory();
            }
            catch (AmazonClientException e)
            {
                var message = "Unable to configure the SQS client. Make sure the environment variables for AWS_REGION, AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are set or the client factory configures the created client accordingly";
                Logger.Error(message, e);
                throw new Exception(message, e);
            }

            try
            {
                snsClient = configuration.SnsClientFactory();
            }
            catch (AmazonClientException e)
            {
                var message = "Unable to configure the SNS client. Make sure the environment variables for AWS_REGION, AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are set or the client factory configures the created client accordingly";
                Logger.Error(message, e);
                throw new Exception(message, e);
            }

            try
            {
                if (!string.IsNullOrEmpty(settings.GetOrDefault <string>(SettingsKeys.S3BucketForLargeMessages)))
                {
                    s3Client = configuration.S3ClientFactory();
                }
            }
            catch (AmazonClientException e)
            {
                var message = "Unable to configure the S3 client. Make sure the environment variables for AWS_REGION, AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are set or the client factory configures the created client accordingly";
                Logger.Error(message, e);
                throw new Exception(message, e);
            }

            queueCache = new QueueCache(sqsClient, configuration);
            topicCache = new TopicCache(snsClient, messageMetadataRegistry, configuration);
        }
        public string GetCacheCount()
        {
            Dictionary <string, CacheInfo> dic = MessageCache.GetAllMessageCaches();
            int cache501   = dic.Where(e => e.Key.IndexOf("501") > -1).Count();
            int cache503R  = dic.Where(e => e.Key.IndexOf("503R") > -1).Count();
            int cache601   = dic.Where(e => e.Key.IndexOf("601") > -1).Count();
            int cacheQueue = QueueCache.GetCacheLength();

            //int cacheFile = Fil
            return(JsonConvert.SerializeObject(new { cache501 = cache501, cache503R = cache503R, cache601 = cache601, cacheQueue = cacheQueue }));
        }
        public void Preserves_FifoQueue(string destination, string expected)
        {
            var configuration = new TransportConfiguration(new SettingsHolder());

            var cache = new QueueCache(null, configuration);

            var result           = cache.GetPhysicalQueueName(destination);
            var resultIdempotent = cache.GetPhysicalQueueName(result);

            Assert.AreEqual(expected, result);
            Assert.AreEqual(expected, resultIdempotent);
        }
        public void ThrowsWhenLongerThanEightyChars(string destination, string queueNamePrefix)
        {
            var settings = new SettingsHolder();

            settings.Set(SettingsKeys.QueueNamePrefix, queueNamePrefix);

            var configuration = new TransportConfiguration(settings);

            var cache = new QueueCache(null, configuration);

            var exception = Assert.Throws <Exception>(() => cache.GetPhysicalQueueName(destination));

            Assert.That(exception.Message, Contains.Substring("is longer than 80 characters"));
        }
        public void ReplacesNonDigitsWithDash(string destination, string expected)
        {
            var settings = new SettingsHolder();

            var configuration = new TransportConfiguration(settings);

            var cache = new QueueCache(null, configuration);

            var result           = cache.GetPhysicalQueueName(destination);
            var resultIdempotent = cache.GetPhysicalQueueName(result);

            Assert.AreEqual(expected, result);
            Assert.AreEqual(expected, resultIdempotent);
        }
        public async Task SetQueueUrl_caches()
        {
            var settings = new SettingsHolder();

            var configuration = new TransportConfiguration(settings);
            var sqsClient     = new MockSqsClient();

            var cache = new QueueCache(sqsClient, configuration);

            cache.SetQueueUrl("fakeQueueName", "http://fakeQueueName");

            await cache.GetQueueUrl("fakeQueueName");

            Assert.IsEmpty(sqsClient.QueueUrlRequestsSent);
        }
        public void Preserves_FifoQueue_WithPreTruncate(string destination, string expected)
        {
            var settings = new SettingsHolder();

            settings.Set(SettingsKeys.PreTruncateQueueNames, true);
            settings.Set(SettingsKeys.QueueNamePrefix, string.Empty);

            var configuration = new TransportConfiguration(settings);

            var cache = new QueueCache(null, configuration);

            var result           = cache.GetPhysicalQueueName(destination);
            var resultIdempotent = cache.GetPhysicalQueueName(result);

            Assert.AreEqual(expected, result);
            Assert.AreEqual(expected, resultIdempotent);
        }
    async Task SendPoisonMessage(string inputQueueName)
    {
        var transport = new TransportExtensions <SqsTransport>(new SettingsHolder());

        transport = transport.ConfigureSqsTransport(SetupFixture.NamePrefix);
        var transportConfiguration = new TransportConfiguration(transport.GetSettings());

        using (var sqsClient = SqsTransportExtensions.CreateSQSClient())
        {
            var getQueueUrlResponse = await sqsClient.GetQueueUrlAsync(new GetQueueUrlRequest
            {
                QueueName = QueueCache.GetSqsQueueName(inputQueueName, transportConfiguration)
            }).ConfigureAwait(false);

            await sqsClient.SendMessageAsync(new SendMessageRequest
            {
                QueueUrl    = getQueueUrlResponse.QueueUrl,
                MessageBody = PoisonMessageBody
            }).ConfigureAwait(false);
        }
    }
        public async Task GetQueueArn_caches()
        {
            var settings = new SettingsHolder();

            var configuration = new TransportConfiguration(settings);
            var sqsClient     = new MockSqsClient();

            var cache = new QueueCache(sqsClient, configuration);

            await cache.GetQueueArn("fakeQueueUrl");

            var requestsSent = new List <string>(sqsClient.GetAttributeRequestsSent);

            sqsClient.GetAttributeRequestsSent.Clear();

            await cache.GetQueueArn("fakeQueueUrl");

            Assert.IsEmpty(sqsClient.GetAttributeRequestsSent);
            CollectionAssert.AreEqual(new List <string> {
                "fakeQueueUrl"
            }, requestsSent);
        }
    async Task CheckErrorQueue(string errorQueueName, CancellationToken cancellationToken)
    {
        var transport = new TransportExtensions <SqsTransport>(new SettingsHolder());

        transport = transport.ConfigureSqsTransport(SetupFixture.NamePrefix);
        var transportConfiguration = new TransportConfiguration(transport.GetSettings());

        using (var sqsClient = SqsTransportExtensions.CreateSQSClient())
        {
            var getQueueUrlResponse = await sqsClient.GetQueueUrlAsync(new GetQueueUrlRequest
            {
                QueueName = QueueCache.GetSqsQueueName(errorQueueName, transportConfiguration)
            }, cancellationToken).ConfigureAwait(false);

            var messageReceived = false;
            ReceiveMessageResponse receiveMessageResponse = null;

            while (messageReceived == false && !cancellationToken.IsCancellationRequested)
            {
                receiveMessageResponse = await sqsClient.ReceiveMessageAsync(new ReceiveMessageRequest
                {
                    QueueUrl        = getQueueUrlResponse.QueueUrl,
                    WaitTimeSeconds = 20
                }, cancellationToken).ConfigureAwait(false);

                if (receiveMessageResponse.Messages.Any())
                {
                    messageReceived = true;
                }
            }

            Assert.NotNull(receiveMessageResponse);
            Assert.AreEqual(1, receiveMessageResponse.Messages.Count);
            Assert.AreEqual(PoisonMessageBody, receiveMessageResponse.Messages.Single().Body);
        }
    }
        public async Task GetQueueUrl_caches()
        {
            var settings = new SettingsHolder();

            settings.Set(SettingsKeys.QueueNamePrefix, "PREFIX");

            var configuration = new TransportConfiguration(settings);
            var sqsClient     = new MockSqsClient();

            var cache = new QueueCache(sqsClient, configuration);

            await cache.GetQueueUrl("fakeQueueName");

            var requestsSent = new List <string>(sqsClient.QueueUrlRequestsSent);

            sqsClient.QueueUrlRequestsSent.Clear();

            await cache.GetQueueUrl("fakeQueueName");

            Assert.IsEmpty(sqsClient.QueueUrlRequestsSent);
            CollectionAssert.AreEqual(new List <string> {
                "PREFIXfakeQueueName"
            }, requestsSent);
        }
        public static async Task <string> FindMatchingSubscription(this IAmazonSimpleNotificationService snsClient, QueueCache queueCache, Topic topic, string queueName)
        {
            var physicalQueueName = queueCache.GetPhysicalQueueName(queueName);

            ListSubscriptionsByTopicResponse upToAHundredSubscriptions = null;

            do
            {
                upToAHundredSubscriptions = await snsClient.ListSubscriptionsByTopicAsync(topic.TopicArn, upToAHundredSubscriptions?.NextToken)
                                            .ConfigureAwait(false);

                // ReSharper disable once ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator
                foreach (var upToAHundredSubscription in upToAHundredSubscriptions.Subscriptions)
                {
                    if (upToAHundredSubscription.Endpoint.EndsWith($":{physicalQueueName}", StringComparison.Ordinal))
                    {
                        return(upToAHundredSubscription.SubscriptionArn);
                    }
                }
            } while (upToAHundredSubscriptions.NextToken != null && upToAHundredSubscriptions.Subscriptions.Count > 0);

            return(null);
        }
        public static async Task <string> FindMatchingSubscription(this IAmazonSimpleNotificationService snsClient, QueueCache queueCache, string topicName, string queueName)
        {
            var existingTopic = await snsClient.FindTopicAsync(topicName).ConfigureAwait(false);

            if (existingTopic == null)
            {
                return(null);
            }

            return(await snsClient.FindMatchingSubscription(queueCache, existingTopic, queueName)
                   .ConfigureAwait(false));
        }
 public TestableSubscriptionManager(IAmazonSQS sqsClient, IAmazonSimpleNotificationService snsClient, string queueName, QueueCache queueCache, MessageMetadataRegistry messageMetadataRegistry, TopicCache topicCache, bool disableSubscribeBatchingOnStart) : base(sqsClient, snsClient, queueName, queueCache, messageMetadataRegistry, topicCache, disableSubscribeBatchingOnStart)
 {
 }
        public static Task <string> FindMatchingSubscription(this IAmazonSimpleNotificationService snsClient, QueueCache queueCache, TopicCache topicCache, MessageMetadata metadata, string queueName)
        {
            var topicName = topicCache.GetTopicName(metadata);

            return(snsClient.FindMatchingSubscription(queueCache, topicName, queueName));
        }
 public int ClearFileCache()
 {
     ReceiptThread.AwakenThread();
     return(QueueCache.GetCacheLength());
 }