public async Task RequeueDeadLettersAsync <T>(int count, Func <T, bool> shouldRequeue) where T : IStorageQueueCommand
        {
            for (var i = 0; i < count; i++)
            {
                var messages = await GetMessagesAsync <T>(1, TimeSpan.FromMinutes(2), _dlQueue)
                               .ConfigureAwait(false);

                if (!messages.Any())
                {
                    continue;
                }
                var deadletter = messages.Single();

                // Delete the old message
                await _dlQueue.DeleteMessageAsync(deadletter.QueueMessageId, deadletter.PopReceipt).ConfigureAwait(false);

                // The dead letter will be deleted even though shouldRequeue is false
                if (shouldRequeue != null && !shouldRequeue((T)deadletter.Message))
                {
                    continue;
                }

                // enqueue the new message again
                await _queue.SendMessageAsync(new BinaryData(_serializer.Serialize(deadletter.Properties)), TimeSpan.Zero, TimeSpan.FromSeconds(-1)).ConfigureAwait(false);
            }
        }
Beispiel #2
0
        private async Task ReadMessages()
        {
            // will need to use a loop or timer to continually get new messages

            // Get the connection string from app settings
            var connectionString = _config["Azure:ConnectionString"];

            // Instantiate a QueueClient which will be used to create and manipulate the queue
            var queueClient = new QueueClient(connectionString, "video-queue");

            var newMessagesResponse = await queueClient.ReceiveMessagesAsync();

            var newMessages = newMessagesResponse.Value;

            if (newMessages.Length == 0)
            {
                Console.WriteLine("No messages...");
                return;
            }

            // DO SOMETHING W/ MESSAGE HERE....
            foreach (var message in newMessages)
            {
                Console.WriteLine("Message Id - Text: " + message.MessageId + " - " + message.MessageText);
                Console.ReadLine();
                await queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt);
            }
        }
        public async Task <Response> DeleteMessageAsync <TData>(DeleteMessageModel message)
            where TData : class
        {
            await GetQueueClient(message.QueueName);

            return(await _queueClient.DeleteMessageAsync(message.Message.MessageId, message.Message.PopReceipt));
        }
Beispiel #4
0
        public static async Task Main(string[] args)
        {
            Console.WriteLine("Hello MessageProcessor");

            QueueClient client = new QueueClient(storageConnectionString, queueName);
            await client.CreateAsync();

            Console.WriteLine($"---Account Metadata---");
            Console.WriteLine($"Account Uri:\t{client.Uri}");
            //Console.WriteLine($"---Existing Messages---");
            int      batchSize         = 10;
            TimeSpan visibilityTimeout = TimeSpan.FromSeconds(2.5d);

            Response <QueueMessage[]> messages = await client.ReceiveMessagesAsync(batchSize, visibilityTimeout);

            Console.WriteLine($"type of <messages> is {messages.GetType()}");
            //**************************************************************************
            //if(messages?.MessageId? == null)	Console.WriteLine("\nno messages\n");
            if (messages.Value.ToString() == "Azure.Storage.Queues.Models.QueueMessage[]")
            {
                Console.WriteLine("\npredicate is <<messages.Value.ToString()>>\n");
            }
            if (messages.Value.Length == 0)
            {
                Console.WriteLine("\nno messages\n");
            }
            //**************************************************************************
            //foreach(QueueMessage message in messages?.Value)
            //Console.WriteLine($"[{message.MessageId}]\t{message.MessageText}");
            foreach (QueueMessage message in messages?.Value)
            {
                Console.WriteLine($"[{message.MessageId}]\t{message.MessageText}");
                await client.DeleteMessageAsync(message.MessageId, message.PopReceipt);
            }
        }
        public async Task ProcessMessages(CancellationToken token)
        {
            foreach (QueueMessage message in (await _queueClient.ReceiveMessagesAsync(maxMessages: 10)).Value)
            {
                var    jsonMessage  = JObject.Parse(message.MessageText);
                Uri    uploadedUri  = new Uri(jsonMessage["data"]["url"].ToString());
                string uploadedFile = Path.GetFileName(jsonMessage["data"]["url"].ToString());
                Console.WriteLine("Blob available at: {0}", jsonMessage["data"]["url"]);

                BlockBlobClient blockBlob = new BlockBlobClient(uploadedUri);

                string destinationFile = Path.Combine(_downloadDestination, Path.GetFileName(uploadedFile));
                Console.WriteLine("Downloading to {0}...", destinationFile);
                await blockBlob.DownloadToAsync(destinationFile);

                Console.WriteLine("Done.");
                await _queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt);

                Console.WriteLine();
            }

            if (!token.IsCancellationRequested)
            {
                await Task.Delay(1000);
            }
        }
Beispiel #6
0
        public async Task DeleteEventAsync(Event @event)
        {
            if (@event == null)
            {
                throw new ArgumentNullException(nameof(@event));
            }
            if (@event.PersistenceId == null)
            {
                throw new ArgumentException(nameof(@event.PersistenceId));
            }

            logger.LogTrace($"Starting {nameof(DeleteEventAsync)}");

            try
            {
                var messageId = @event.PersistenceId as MessageIdentifier;

                using (var metricLogger = new DurationMetricLogger(MetricNames.DeleteMessageDuration, logger))
                {
                    await queueClient.DeleteMessageAsync(messageId.MessageId, messageId.PopReceipt);
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex, $"Error during deleting message from queue");
                throw;
            }

            logger.LogTrace($"Finish {nameof(DeleteEventAsync)}");
        }
        static void Main(string[] args)
        {
            var queueClient = new QueueClient(_storageConnectionString, _storageQueueName);

            queueClient.CreateIfNotExists();

            var count     = 0;
            var stopWatch = new Stopwatch();

            stopWatch.Start();

            do
            {
                QueueMessage[] retrievedMessage = queueClient.ReceiveMessages();

                count += retrievedMessage.Length;

                Task.WhenAll(
                    retrievedMessage.Select(msg => queueClient.DeleteMessageAsync(msg.MessageId, msg.PopReceipt)));

                if (count % 100 == 0)
                {
                    var totalEvents     = count * _batchSize;
                    var timeElapsed     = stopWatch.Elapsed.TotalSeconds;
                    var eventsPerSecond = totalEvents / timeElapsed;
                    Console.WriteLine($"Received {count} messages, {count * _batchSize} events total in {timeElapsed} seconds, {eventsPerSecond} events total/second");
                }
            } while (true);
        }
Beispiel #8
0
        static async Task Main(string[] args)
        {
            QueueClient client = new QueueClient("UseDevelopmentStorage=true", "vsqueue");

            if (await client.ExistsAsync())
            {
                await client.SendMessageAsync("vstext");

                while (true)
                {
                    QueueMessage[] messages = client.ReceiveMessages();
                    if (messages.Count() > 0)
                    {
                        foreach (var message in messages)
                        {
                            var result = await ProcessMessage(message);

                            if (result)
                            {
                                await client.DeleteMessageAsync(message.MessageId, message.PopReceipt);
                            }
                            else
                            {
                                await client.SendMessageAsync(message.MessageText);
                            }
                        }
                    }
                }
            }
        }
        private async Task ReceiveStringAsync(Action <string> action)
        {
            var queueClient = new QueueClient(_connectionString, _queueName, new QueueClientOptions
            {
                MessageEncoding = _messageEncoding,
            });

            await queueClient.CreateIfNotExistsAsync();

            while (true)
            {
                try
                {
                    var retrievedMessages = (await queueClient.ReceiveMessagesAsync()).Value;

                    if (retrievedMessages.Length > 0)
                    {
                        foreach (var retrievedMessage in retrievedMessages)
                        {
                            action(retrievedMessage.Body.ToString());
                            await queueClient.DeleteMessageAsync(retrievedMessage.MessageId, retrievedMessage.PopReceipt);
                        }
                    }
                    else
                    {
                        await Task.Delay(1000);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    await Task.Delay(1000);
                }
            }
        }
Beispiel #10
0
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);

            var retrievedMessage = await _queueClient.ReceiveMessageAsync(cancellationToken : stoppingToken);

            if (retrievedMessage.Value != null)
            {
                using var scope = _serviceProvider.CreateScope();
                var handler = scope.ServiceProvider.GetRequiredService <IPodcastIngestionHandler>();

                try
                {
                    var(title, url, categories) = retrievedMessage.Value.Body.ToObjectFromJson <NewFeedRequested>();
                    await handler.HandleIngestionAsync(title, url, categories, stoppingToken);
                }
                catch (Exception ex)
                {
                    _logger.LogError("Worker failed with exception: {exception} at: {time}", ex.Message,
                                     DateTimeOffset.Now);
                }

                await _queueClient.DeleteMessageAsync(retrievedMessage.Value.MessageId,
                                                      retrievedMessage.Value.PopReceipt, stoppingToken);
            }

            await Task.Delay(1000, stoppingToken);
        }
    }
Beispiel #11
0
    public static async Task Main(string[] args)
    {
        QueueClient client = new QueueClient(storageConnectionString, queueName);
        await client.CreateAsync();

        Console.WriteLine($"---Account Metdata---");
        Console.WriteLine($"Account Uri:\t{client.Uri}");

        Console.WriteLine($"---Existing Messages---");
        int      batchSize         = 10;
        TimeSpan visibilityTimeout = TimeSpan.FromSeconds(2.5d);

        Response <QueueMessage[]> messages = await client.ReceiveMessagesAsync(batchSize, visibilityTimeout);

        foreach (QueueMessage message in messages?.Value)
        {
            Console.WriteLine($"[{message.MessageId}]\t{message.MessageText}");
            await client.DeleteMessageAsync(message.MessageId, message.PopReceipt);
        }

        Console.WriteLine($"---New Messages---");
        string greeting = "Hi, Developer!";
        await client.SendMessageAsync(greeting);

        Console.WriteLine($"Sent Message:\t{greeting}");
    }
        public async Task MessageSample()
        {
            // Instantiate a new QueueServiceClient using a connection string.
            QueueServiceClient queueServiceClient = new QueueServiceClient(TestConfigurations.DefaultTargetTenant.ConnectionString);

            // Instantiate a new QueueClient
            QueueClient queueClient = queueServiceClient.GetQueueClient($"myqueue2-{Guid.NewGuid()}");

            try
            {
                // Create your new Queue in the service
                await queueClient.CreateAsync();

                // Instantiate a new MessagesClient
                // Enqueue a message to the queue
                Response <EnqueuedMessage> enqueueResponse = await queueClient.EnqueueMessageAsync("my message");

                // Peek message
                Response <IEnumerable <PeekedMessage> > peekResponse = await queueClient.PeekMessagesAsync();

                // Update message
                await queueClient.UpdateMessageAsync("new message", enqueueResponse.Value.MessageId, enqueueResponse.Value.PopReceipt);

                // Dequeue message
                Response <IEnumerable <DequeuedMessage> > dequeueResponse = await queueClient.DequeueMessagesAsync();

                // Delete Message
                await queueClient.DeleteMessageAsync(enqueueResponse.Value.MessageId, dequeueResponse.Value.First().PopReceipt);
            }
            finally
            {
                // Delete your Queue in the service
                await queueClient.DeleteAsync();
            }
        }
Beispiel #13
0
        /// <inheritdoc />
        public async Task DeleteMessagesAsync(QueueClient queue, IEnumerable <QueueMessage> messages)
        {
            if (queue == null)
            {
                throw new ArgumentNullException(nameof(queue));
            }

            try
            {
                var deleteTasks = new List <Task>();
                foreach (var message in messages)
                {
                    var deleteTask = queue.DeleteMessageAsync(message.MessageId, message.PopReceipt);
                    deleteTasks.Add(deleteTask);
                }

                await Task.WhenAll(deleteTasks);
            }
            catch (Exception ex)
            {
                var errorMessage = GetStorageErrorMessage(ex);
                var statusCode   = GetStorageStatusCode(ex);
                var message      = string.Format(CultureInfo.CurrentCulture, AzureStorageResources.StorageManager_OperationFailed, statusCode, errorMessage);
                _logger.Error(message, ex);
            }
        }
Beispiel #14
0
        /// <inheritdoc/>
        public async Task DeleteMessagesAsync(string queueName, QueueMessage message)
        {
            ThrowIfNotSpecified(queueName);

            QueueClient queueClient = CreateQueueClient(queueName);
            await queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt);
        }
Beispiel #15
0
        public static async Task DequeueMessage(string account, string key, string queueName)
        {
            QueueClient queueClient = new QueueClient(Client.GetConnectionString(account, key), queueName);

            QueueMessage msg = await queueClient.ReceiveMessageAsync();

            await queueClient.DeleteMessageAsync(msg.MessageId, msg.PopReceipt);
        }
Beispiel #16
0
        static async Task Main(string[] args)
        {
            ISerializer serializer    = new Serializer();
            var         loggerFactory = LoggerFactory.Create(builder =>
            {
                builder
                .AddFilter("Microsoft", LogLevel.Warning)
                .AddFilter("System", LogLevel.Warning)
                .AddFilter("LoggingConsoleApp.Program", LogLevel.Debug)
                .AddConsole()
                .AddEventLog();
            });
            ILogger logger = loggerFactory.CreateLogger <Program>();

            var connectionStringKeyVaultFetchStore = new SimpleStringKeyVaultFetchStore(
                new KeyVaultFetchStoreOptions <string>()
            {
                ExpirationSeconds = 3600,
                KeyVaultName      = "kv-queueflow",
                SecretName        = "stazfuncqueueflow-primary-connection-string"
            }, logger);

            var connectionString = await connectionStringKeyVaultFetchStore.GetStringValueAsync();



            var accountName = "stazfuncqueueflow";
            var queueName   = "queue-main";

            string queueUri = $"https://{accountName}.queue.core.windows.net/{queueName}";

            // Get a credential and create a client object for the blob container.
            //           QueueClient queueClient = new QueueClient(new Uri(queueUri),new DefaultAzureCredential());
            QueueClient queueClient = new QueueClient(connectionString, queueName);
            // Create the queue
            await queueClient.CreateAsync();


            while (true)
            {
                QueueMessage[] messages = await queueClient.ReceiveMessagesAsync(maxMessages : 10);

                // Process and delete messages from the queue
                foreach (QueueMessage message in messages)
                {
                    var decoded = message.MessageText.Base64Decode();
                    // "Process" the message
                    Console.WriteLine($"Message: {message.MessageText} - {decoded}");
                    var job = serializer.Deserialize <Job>(decoded);

                    // Let the service know we're finished with
                    // the message and it can be safely deleted.
                    await queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt);
                }

                Thread.Sleep(1000);
            }
        }
        static async Task Main(string[] args)
        {
            Environment.SetEnvironmentVariable(
                "AZURE_STORAGE_CONNECTION_STRING",
                "<placeholder>"
                );

            var connectionString = Environment.GetEnvironmentVariable("AZURE_STORAGE_CONNECTION_STRING");

            Console.WriteLine("Queue connection string: " + connectionString ?? "null");

            var queueName = "colins-sample-queue";

            Console.WriteLine("Queue name: " + queueName + "\n");

            //QueueServiceClient queueServiceClient;

            QueueClient queueClient = new QueueClient(connectionString, queueName);

            await peekTheQueue(queueClient);


            // programmatically add messages to the queue
            Console.WriteLine("Adding 3 messages to the queue...");
            await queueClient.SendMessageAsync("A programmatically inserted queue message.");

            await queueClient.SendMessageAsync("A second programmatically inserted queue message.");

            await queueClient.SendMessageAsync("A third programmatically inserted queue message.");

            Console.WriteLine("Messages added.\n");

            await peekTheQueue(queueClient);

            // receive one message
            QueueMessage[] messagesReceived = await queueClient.ReceiveMessagesAsync(maxMessages : 1);

            await peekTheQueue(queueClient);

            /*
             *      DO PROCESS LOGIC FOR MESSAGES RECEIVED
             */
            foreach (var msg in messagesReceived)
            {
                Console.WriteLine($"Processed message {msg.MessageId}.");
            }

            await peekTheQueue(queueClient);

            foreach (var msg in messagesReceived)
            {
                await queueClient.DeleteMessageAsync(msg.MessageId, msg.PopReceipt);
            }

            await peekTheQueue(queueClient);

            Console.WriteLine("End of demo.  Thanks!\n");
        }
        async Task <Context> SendMessage <TReceiver>(string destination, string destinationConnectionString, CancellationToken cancellationToken = default)
            where TReceiver : EndpointConfigurationBuilder
        {
            var ctx = await Scenario.Define <Context>()
                      .WithEndpoint <Sender>(b => b.When(s =>
            {
                var options = new SendOptions();
                options.SetDestination(destination);
                return(s.Send(new MyCommand(), options));
            }))
                      .WithEndpoint <TReceiver>()
                      .Done(c => c.Received)
                      .Run().ConfigureAwait(false);

            Assert.IsTrue(ctx.Received);

            Dictionary <string, string> propertiesFlattened;

            do
            {
                cancellationToken.ThrowIfCancellationRequested();

                var receiverAuditQueue = new QueueClient(destinationConnectionString, AuditName);

                QueueMessage[] rawMessages = await receiverAuditQueue.ReceiveMessagesAsync(1, cancellationToken : cancellationToken).ConfigureAwait(false);

                if (rawMessages.Length == 0)
                {
                    Assert.Fail("No message in the audit queue to pick up.");
                }
                var rawMessage = rawMessages[0];
                await receiverAuditQueue.DeleteMessageAsync(rawMessage.MessageId, rawMessage.PopReceipt, cancellationToken).ConfigureAwait(false);

                JToken message;
                var    bytes = Convert.FromBase64String(rawMessage.MessageText);
                using (var reader = new JsonTextReader(new StreamReader(new MemoryStream(bytes))))
                {
                    message = JToken.ReadFrom(reader);
                }

                propertiesFlattened = message.FindProperties(IsSimpleProperty)
                                      .ToDictionary(jp => jp.Name, jp => ((JValue)jp.Value).Value <string>());

                if (propertiesFlattened.ContainsValue(ctx.TestRunId.ToString()))
                {
                    break;
                }
            }while (true);


            ctx.AllPropertiesFlattened = propertiesFlattened;

            ctx.ContainingRawConnectionString = ctx.AllPropertiesFlattened.Where(kvp => kvp.Value.Contains(Utilities.GetEnvConfiguredConnectionString()))
                                                .Select(kvp => kvp.Key).ToArray();

            return(ctx);
        }
        private async Task ExecuteAsync(CancellationToken cancellationToken)
        {
            _queueClient = new QueueClient(_globalSettings.Notifications.ConnectionString, "notifications");
            while (!cancellationToken.IsCancellationRequested)
            {
                try
                {
                    var messages = await _queueClient.ReceiveMessagesAsync(32);

                    if (messages.Value?.Any() ?? false)
                    {
                        foreach (var message in messages.Value)
                        {
                            try
                            {
                                await HubHelpers.SendNotificationToHubAsync(
                                    message.DecodeMessageText(), _hubContext, cancellationToken);

                                await _queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt);
                            }
                            catch (Exception e)
                            {
                                _logger.LogError("Error processing dequeued message: " +
                                                 $"{message.MessageId} x{message.DequeueCount}. {e.Message}", e);
                                if (message.DequeueCount > 2)
                                {
                                    await _queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt);
                                }
                            }
                        }
                    }
                    else
                    {
                        await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
                    }
                }
                catch (Exception e)
                {
                    _logger.LogError("Error processing messages.", e);
                }
            }

            _logger.LogWarning("Done processing.");
        }
Beispiel #20
0
        //-------------------------------------------------
        // Process and remove one message from the queue
        //-------------------------------------------------
        public static async Task <QueueMessage[]> DequeueMessage(QueueClient queueClient)
        {
            // Get the next message
            QueueMessage[] retrievedMessage = await queueClient.ReceiveMessagesAsync();


            // Delete the message
            await queueClient.DeleteMessageAsync(retrievedMessage[0].MessageId, retrievedMessage[0].PopReceipt);

            return(retrievedMessage);
        }
        /****************************************************************************/
        /// <summary>
        /// Send a message
        /// </summary>
        /// <param name="message"></param>
        /// <param name="sendOn"></param>
        public Task Delete(IMessage msg)
        {
            var amsg = msg as AzureStorageQueueMessage;

            if (amsg == null)
            {
                throw new ArgumentException("Message is not a valid type for this queue");
            }

            return(_queueClient.DeleteMessageAsync(amsg.Message.MessageId, amsg.Message.PopReceipt));
        }
Beispiel #22
0
        static async Task ReceiveArticleAsync()
        {
            var resp = await client.ReceiveMessagesAsync(maxMessages : 3);

            foreach (var msg in resp.Value)
            {
                Console.WriteLine("-- receive " + msg.MessageText);

                await client.DeleteMessageAsync(msg.MessageId, msg.PopReceipt);
            }
        }
Beispiel #23
0
        /// <summary>
        /// backup application configuration store as an asynchronous operation.
        /// </summary>
        /// <param name="storageQueueUri">The storage queue URI.</param>
        /// <param name="primaryStoreEndpoint">The primary store endpoint.</param>
        /// <param name="secondaryStoreEndpoint">The secondary store endpoint.</param>
        /// <param name="log">The log.</param>
        private static async Task BackupAppConfigurationStoreAsync(string storageQueueUri,
                                                                   string primaryStoreEndpoint,
                                                                   string secondaryStoreEndpoint,
                                                                   ILogger log)
        {
            QueueClient queueClient = new QueueClient(new Uri(storageQueueUri), new ManagedIdentityCredential());

            // Peek to see if there are events in the queue.
            if ((await queueClient.PeekMessagesAsync()).Value.Length > 0)
            {
                ConfigurationClient primaryAppConfigClient   = new ConfigurationClient(new Uri(primaryStoreEndpoint), new ManagedIdentityCredential());
                ConfigurationClient secondaryAppConfigClient = new ConfigurationClient(new Uri(secondaryStoreEndpoint), new ManagedIdentityCredential());
                do
                {
                    Response <QueueMessage[]> retrievedMessages = await queueClient.ReceiveMessagesAsync(MaxMessagesToRead);

                    // Extract list of key+labels from event data.
                    HashSet <KeyLabel> updatedKeyLabels = ExtractKeyLabelsFromEvents(retrievedMessages.Value, log);

                    // If there are any valid App Configuration events, update secondary store.
                    if (updatedKeyLabels.Count > 0)
                    {
                        bool isBackupSuccessful = await BackupKeyValuesAsync(updatedKeyLabels, primaryAppConfigClient, secondaryAppConfigClient, log);

                        if (!isBackupSuccessful)
                        {
                            // Abort this function without deleting retrievedMessages from storage queue.
                            log.LogWarning($"Aborting App Configuration store backup. It will be attempted next time the function is triggered.");
                            break;
                        }
                    }

                    // Delete this batch of events from storage queue.
                    foreach (QueueMessage message in retrievedMessages.Value)
                    {
                        try
                        {
                            await queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt);

                            log.LogInformation($"Successfully deleted message from queue. Message ID: {message.MessageId}");
                        }
                        catch (RequestFailedException ex) when(
                            ex.ErrorCode == QueueErrorCode.PopReceiptMismatch ||
                            ex.ErrorCode == QueueErrorCode.MessageNotFound)
                        {
                            // We can continue processing the rest of the queue and safely ignore these exceptions.
                            log.LogWarning($"Failed to delete message from queue. Message ID: {message.MessageId}\nException: {ex}");
                        }
                    }
                } while ((await queueClient.PeekMessagesAsync()).Value.Length > 0);
            }
        }
Beispiel #24
0
        //-----------------------------------------------------
        // Process and remove multiple messages from the queue
        //-----------------------------------------------------
        public static async Task <QueueMessage[]> DequeueMessages(QueueClient queueClient)
        {
            // Receive and process 20 messages
            QueueMessage[] receivedMessages = await queueClient.ReceiveMessagesAsync(20, TimeSpan.FromMinutes(5));

            foreach (QueueMessage message in receivedMessages)
            {
                // Delete the message
                await queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt);
            }

            return(receivedMessages);
        }
        public async Task DequeueAndUpdateAsync()
        {
            // Get a connection string to our Azure Storage account.
            string connectionString = ConnectionString;

            // Get a reference to a queue named "sample-queue" and then create it
            QueueClient queue = new QueueClient(connectionString, Randomize("sample-queue"));
            await queue.CreateAsync();

            try
            {
                // Add several messages to the queue
                await queue.SendMessageAsync("first");

                await queue.SendMessageAsync("second");

                await queue.SendMessageAsync("third");

                // Get the messages from the queue with a short visibility timeout
                List <QueueMessage> messages = new List <QueueMessage>();
                foreach (QueueMessage message in (await queue.ReceiveMessagesAsync(10, TimeSpan.FromSeconds(1))).Value)
                {
                    // Tell the service we need a little more time to process the message
                    UpdateReceipt changedMessage = await queue.UpdateMessageAsync(
                        message.MessageId,
                        message.PopReceipt,
                        message.MessageText,
                        TimeSpan.FromSeconds(5));

                    messages.Add(message.Update(changedMessage));
                }

                // Wait until the visibility window times out
                await Task.Delay(TimeSpan.FromSeconds(1.5));

                // Ensure the messages aren't visible yet
                Assert.AreEqual(0, (await queue.ReceiveMessagesAsync(10)).Value.Count());

                // Finish processing the messages
                foreach (QueueMessage message in messages)
                {
                    // Tell the service we need a little more time to process the message
                    await queue.DeleteMessageAsync(message.MessageId, message.PopReceipt);
                }
            }
            finally
            {
                // Clean up after the test when we're finished
                await queue.DeleteAsync();
            }
        }
        public async Task <Response <int> > Delete(string queueName)
        {
            QueueClient queueClient = await QueueClient(queueName);

            QueueMessage queueMessage = await queueClient.ReceiveMessageAsync();

            if (queueMessage == null)
            {
                return(new Response <int>("Not found message"));
            }
            var delete = await queueClient.DeleteMessageAsync(queueMessage.MessageId, queueMessage.PopReceipt);

            return(new Response <int>(delete.Status));
        }
        private static async Task ReadExistingMessages(QueueClient client)
        {
            await Console.Out.WriteLineAsync($"---Existing Messages---");

            int      batchSize         = 10;
            TimeSpan visibilityTimeout = TimeSpan.FromSeconds(2.5d);
            var      messages          = await client.ReceiveMessagesAsync(batchSize, visibilityTimeout);

            foreach (QueueMessage message in messages?.Value)
            {
                Console.WriteLine($"[{message.MessageId}]\t{message.MessageText}");
                await client.DeleteMessageAsync(message.MessageId, message.PopReceipt);
            }
        }
Beispiel #28
0
        static async Task <bool> ConsumeMessages(QueueClient queue)
        {
            try
            {
                QueueProperties properties = await queue.GetPropertiesAsync();

                if (properties != null)
                {
                    while (properties.ApproximateMessagesCount > 0)
                    {
                        int duration = 0;
                        // Setting to 40 seconds since default time it sleeps is 30 seconds
                        QueueMessage[] retrievedMessage = await queue.ReceiveMessagesAsync(1, TimeSpan.FromSeconds(40));

                        if (retrievedMessage.Length > 0)
                        {
                            if (!String.IsNullOrEmpty(retrievedMessage[0].MessageText))
                            {
                                duration = Convert.ToInt32(retrievedMessage[0].MessageText);

                                Console.WriteLine($"{DateTime.Now} Sleeping for {retrievedMessage[0].MessageText} seconds");
                            }

                            Thread.Sleep(duration * 1000);

                            var tId = System.Threading.Thread.CurrentThread.ManagedThreadId;
                            Console.WriteLine($"{DateTime.Now} Processed message {retrievedMessage[0].MessageId} in thread {tId.ToString()}");

                            await queue.DeleteMessageAsync(retrievedMessage[0].MessageId, retrievedMessage[0].PopReceipt);

                            properties = await queue.GetPropertiesAsync();
                        }
                        else
                        {
                            Console.WriteLine($"{DateTime.Now} Queue drained. (retrievedMessage.Length = 0)");

                            return(false);
                        }
                    }
                }
                Console.WriteLine($"{DateTime.Now} Queue drained. (properties = null)");
                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                return(false);
            }
        }
Beispiel #29
0
        public async Task <TReadType> Read <TReadType>() where TReadType : class
        {
            Response <QueueMessage> response = await _queueClient.ReceiveMessageAsync();

            if (response.Value?.MessageId == null)
            {
                return(null);
            }
            TReadType result = JsonSerializer.Deserialize <TReadType>(response.Value.Body);

            await _queueClient.DeleteMessageAsync(response.Value.MessageId, response.Value.PopReceipt);

            return(result);
        }
        private async Task HandleMessageAsync(QueueMessage message)
        {
            Replicatable replicatable = null;

            try
            {
                replicatable = MessageSerializer.Deserialize(message.MessageText);

                using (var executionTimer = new ExecutionTimer())
                {
                    await _messageHandler.HandleAsync(replicatable, _cancellationToken);

                    await _queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt, _cancellationToken);

                    await Task.WhenAll(_replicationStatusLoggers.Select(r => r.SuccessAsync(replicatable, executionTimer.CalculateElapsedAndStopMeasuring())));
                }
            }
            catch (UnsupportedMessageFormatException e)
            {
                await _queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt, _cancellationToken);

                await Task.WhenAll(_replicationStatusLoggers.Select(r => r.UnprocessableAsync(message.MessageId, message.PopReceipt, message.MessageText, e)));
            }
            catch (Exception e)
            {
                if (message.DequeueCount > _maximumNumberOfRetries)
                {
                    await _queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt, _cancellationToken);

                    await Task.WhenAll(_replicationStatusLoggers.Select(r => r.FailureAsync(replicatable, e)));
                }
            }
            finally
            {
                _concurrencyLimiter.Release();
            }
        }