private async Task Initialize()
 {
     // Create the queues if they don't exist
     _queue = _cloudQueueClient.GetQueueReference(UploadConfig.NotificationQueueName);
     _poisonQueue = _cloudQueueClient.GetQueueReference(PoisionQueueName);
     await Task.WhenAll(_queue.CreateIfNotExistsAsync(), _poisonQueue.CreateIfNotExistsAsync()).ConfigureAwait(false);
     _initialized = true;
 }
Exemplo n.º 2
0
        public AzureQueueExt(string conectionString, string queueName, IQueueMessageSerializer serializer = null)
        {
            queueName = queueName.ToLower();
            var storageAccount = CloudStorageAccount.Parse(conectionString);
            var queueClient = storageAccount.CreateCloudQueueClient();

            _queue = queueClient.GetQueueReference(queueName);
            _queue.CreateIfNotExistsAsync().Wait();
            _serializer = serializer ?? new DefaultQueueSerializer();
        }
        /// <summary>
        /// Initialize the connection to Azure Storage.
        /// If initReader is set to true, a timer will start that polls the queue every readerPollInterval, or by default 250ms.
        /// </summary>
        public async Task InitAsync(bool initReader = false, TimeSpan? readerPollInterval = null) {
            CloudStorageAccount sta = CloudStorageAccount.Parse(connectionString);
            var queueClient = sta.CreateCloudQueueClient();
            queue = queueClient.GetQueueReference(queueName);

            await queue.CreateIfNotExistsAsync();

            if (initReader) {
                await StartReaderAsync(readerPollInterval);
            }
        }
Exemplo n.º 4
0
 public AzureQueueProvider(string indexName)
 {
     var account = AzureConnectionHelper.CloudStorageAccount;
     if (account != null)
     {
         var queueClient = account.CreateCloudQueueClient();
         Queue = queueClient.GetQueueReference(indexName);
         var t = Queue.CreateIfNotExistsAsync();
         t.Wait();
     }
 }
        public async Task CloudQueueAddMessageVerifyContent()
        {
            CloudQueueClient client = GenerateCloudQueueClient();
            string           name   = GenerateNewQueueName();
            CloudQueue       queue  = client.GetQueueReference(name);

            try
            {
                await queue.CreateIfNotExistsAsync();

                string            msgContent = Guid.NewGuid().ToString("N");
                CloudQueueMessage message    = new CloudQueueMessage(msgContent);
                message.NextVisibleTime = null;

                await queue.AddMessageAsync(message);

                VerifyAddMessageResult(message);
                Assert.IsTrue(message.AsString == msgContent);
            }
            finally
            {
                queue.DeleteIfExistsAsync().Wait();
            }
        }
        public async Task CloudQueueAddMessageFullParameterAsync()
        {
            CloudQueueMessage futureMessage  = new CloudQueueMessage("This message is for the future.");
            CloudQueueMessage presentMessage = new CloudQueueMessage("This message is for the present.");

            CloudQueueClient client = GenerateCloudQueueClient();
            CloudQueue       queue  = client.GetQueueReference(GenerateNewQueueName());

            try
            {
                await queue.CreateIfNotExistsAsync();

                await queue.AddMessageAsync(futureMessage, null, TimeSpan.FromDays(2), null, null);

                VerifyAddMessageResult(futureMessage);

                // We should not be able to see the future message yet.
                CloudQueueMessage retrievedMessage = await queue.GetMessageAsync();

                Assert.IsNull(retrievedMessage);

                await queue.AddMessageAsync(presentMessage, null, TimeSpan.Zero, null, null);

                VerifyAddMessageResult(presentMessage);
                await queue.AddMessageAsync(presentMessage, TimeSpan.FromDays(1), null, null, null);

                VerifyAddMessageResult(presentMessage);

                // We should be able to see the present message.
                retrievedMessage = await queue.GetMessageAsync();

                Assert.IsNotNull(retrievedMessage);
                Assert.AreEqual <string>(presentMessage.AsString, retrievedMessage.AsString);

                await queue.AddMessageAsync(futureMessage, TimeSpan.FromDays(2), TimeSpan.FromDays(1), null, null);

                VerifyAddMessageResult(futureMessage);

                await queue.ClearAsync();

                // -1 seconds should set an infinite ttl
                await queue.AddMessageAsync(presentMessage, TimeSpan.FromSeconds(-1), null, null, null);

                retrievedMessage = await queue.PeekMessageAsync();

                Assert.AreEqual(DateTime.MaxValue.Year, retrievedMessage.ExpirationTime.Value.Year);

                // There should be no upper bound on ttl
                await queue.AddMessageAsync(presentMessage, TimeSpan.MaxValue, null, null, null);

                // Check other edge cases
                await queue.AddMessageAsync(presentMessage, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(0), null, null);

                await queue.AddMessageAsync(presentMessage, TimeSpan.FromSeconds(7 * 24 * 60 * 60), TimeSpan.FromSeconds(7 * 24 * 60 * 60 - 1), null, null);

                await queue.AddMessageAsync(presentMessage, TimeSpan.FromSeconds(-1), TimeSpan.FromSeconds(1), null, null);

                await TestHelper.ExpectedExceptionAsync <ArgumentException>(
                    async() => await queue.AddMessageAsync(futureMessage, TimeSpan.FromDays(1), TimeSpan.FromDays(2), null, null),
                    "Using a visibility timeout longer than the time to live should fail");

                await TestHelper.ExpectedExceptionAsync <ArgumentException>(
                    async() => await queue.AddMessageAsync(futureMessage, null, TimeSpan.FromDays(8), null, null),
                    "Using a visibility longer than the maximum visibility timeout should fail");

                await TestHelper.ExpectedExceptionAsync <ArgumentException>(
                    async() => await queue.AddMessageAsync(futureMessage, null, TimeSpan.FromMinutes(-1), null, null),
                    "Using a negative visibility should fail");

                await TestHelper.ExpectedExceptionAsync <ArgumentException>(
                    async() => await queue.AddMessageAsync(futureMessage, TimeSpan.FromMinutes(-1), null, null, null),
                    "Using a negative TTL other than -1 seconds (infinite) should fail");

                await TestHelper.ExpectedExceptionAsync <ArgumentException>(
                    () => queue.AddMessageAsync(futureMessage, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1), null, null),
                    "Visibility timeout must be strictly less than the TTL");

                await TestHelper.ExpectedExceptionAsync <ArgumentException>(
                    () => queue.AddMessageAsync(presentMessage, null, CloudQueueMessage.MaxVisibilityTimeout, null, null),
                    "Null TTL will default to 7 days, which is the max visibility timeout. They cannot be equal.");
            }
            finally
            {
                queue.DeleteIfExistsAsync().Wait();
            }
        }
        private static async Task AddMessageAndCreateIfNotExistsAsync(CloudQueue queue, CloudQueueMessage message, CancellationToken cancellationToken)
        {
            if (queue == null)
            {
                throw new ArgumentNullException("queue");
            }

            bool isQueueNotFoundException = false;
            try
            {
                await queue.AddMessageAsync(message, cancellationToken);
                return;
            }
            catch (StorageException exception)
            {
                if (!exception.IsNotFoundQueueNotFound())
                {
                    throw;
                }

                isQueueNotFoundException = true;
            }

            Debug.Assert(isQueueNotFoundException);
            await queue.CreateIfNotExistsAsync(cancellationToken);
            await queue.AddMessageAsync(message, cancellationToken);
        }