public static void Update(CloudQueue queue, string msg) { // Get the message from the queue and update the message contents. CloudQueueMessage message = queue.GetMessage(); message.SetMessageContent2(msg, false); queue.UpdateMessage(message, TimeSpan.FromSeconds(60.0), // Make it visible for another 60 seconds. MessageUpdateFields.Content | MessageUpdateFields.Visibility); }
public async Task QueueMessageBatchAsync <T>(Guid streamGuid, String streamNamespace, IEnumerable <T> events, StreamSequenceToken token, Dictionary <string, object> requestContext) { if (events == null) { throw new ArgumentNullException("events", "Trying to QueueMessageBatchAsync null data."); } //int count = events.Count(); //if (count != 1) //{ // throw new OrleansException("Trying to QueueMessageBatchAsync a batch of more than one event. " + // "SimpleAzureQueueAdapter does not support batching. Instead, you can batch in your application code."); //} object data = events.First(); bool isBytes = data is byte[]; bool isString = data is string; if (data != null && !isBytes && !isString) { throw new OrleansException( string.Format( "Trying to QueueMessageBatchAsync a type {0} which is not a byte[] and not string. " + "SimpleAzureQueueAdapter only supports byte[] or string.", data.GetType())); } if (Queue == null) { var tmpQueue = new AzureQueueDataManager(this.loggerFactory, QueueName, DataConnectionString); await tmpQueue.InitQueueAsync(); if (Queue == null) { Queue = tmpQueue; } } CloudQueueMessage cloudMsg = null; if (isBytes) { //new CloudQueueMessage(byte[]) not supported in netstandard cloudMsg = new CloudQueueMessage(null as string); cloudMsg.SetMessageContent2(data as byte[]); } else if (isString) { cloudMsg = new CloudQueueMessage(data as string); } else if (data == null) { // It's OK to pass null data. why should I care? cloudMsg = new CloudQueueMessage(null as string); } await Queue.AddQueueMessage(cloudMsg); }
private CloudQueueMessage ConvertToMessage(Message message) { if (!string.IsNullOrEmpty(message.Id)) { var nativeMessage = new CloudQueueMessage(message.Id, Guid.NewGuid().ToString()); nativeMessage.SetMessageContent2(message.Content); return(nativeMessage); } else { return(new CloudQueueMessage(message.Content)); } }
public static async Task UpdateQueueMessage() { CloudQueue queue = await QueueHelper.GetCloudQueue(); CloudQueueMessage cloudQueueMessage = await queue.GetMessageAsync(); if (cloudQueueMessage != null) { cloudQueueMessage.SetMessageContent2(cloudQueueMessage.AsString + "\t" + "Update Message", false); await queue.UpdateMessageAsync(cloudQueueMessage, TimeSpan.Zero, MessageUpdateFields.Content | MessageUpdateFields.Visibility); } }
protected CloudQueueMessage CreateMessage <T>(T someObject) { const string id = "aaa"; const string popReceipt = "bbb"; var content = Helpers.IsASimpleType(typeof(T)) ? someObject.ToString() : JsonConvert.SerializeObject(someObject); var message = new CloudQueueMessage(id, popReceipt); message.SetMessageContent2(System.Text.Encoding.UTF8.GetBytes(content)); return(message); }
/// <summary> /// Update an enqueued message and its visibility time. For workflow scenarios this could enable you to update /// the status of a task as well as extend the visibility timeout in order to provide more time for a client /// continue working on the message before another client can see the message. /// </summary> /// <param name="queue">The sample queue</param> public async Task UpdateEnqueuedMessageAsync(CloudQueue queue) { // Insert another test message into the queue Console.WriteLine("5. Insert another test message "); await queue.AddMessageAsync(new CloudQueueMessage("Hello World Again!")); Console.WriteLine("6. Change the contents of a queued message"); CloudQueueMessage message = await queue.GetMessageAsync(); message.SetMessageContent2($"Updated contents {DateTime.Now.Ticks}.", false); await queue.UpdateMessageAsync( message, TimeSpan.Zero, // For the purpose of the sample make the update visible immediately MessageUpdateFields.Content | MessageUpdateFields.Visibility); }
public async Task UpdateMessageAsync(string queueName, string messageId, string popReceipt, string newMessage) { //Queue CloudQueue queue = await GetQueueAsync(queueName); // Message CloudQueueMessage message = await queue.GetMessageAsync(); // Update message message.SetMessageContent2(newMessage, false); await queue.UpdateMessageAsync( message, TimeSpan.Zero, // Make the update visible immediately MessageUpdateFields.Content | MessageUpdateFields.Visibility); }
public static CloudQueueMessage ToCloudQueueMessage(QueueMessage message) { if (message == null) { throw new ArgumentNullException(nameof(message)); } //when there are no properties pack the data as binary in raw form if (message.Properties == null || message.Properties.Count == 0) { var r = new CloudQueueMessage((string)null); r.SetMessageContent2(message.Content); return(r); } //note that Azure Storage doesn't have properties on message, therefore I can do a simulation instead var clazz = new JsonProps { Properties = message.Properties.Select(p => new JsonProp { Name = p.Key, Value = p.Value }).ToArray() }; byte[] propBytes = Encoding.UTF8.GetBytes(clazz.ToJsonString()); CloudQueueMessage result; using (var ms = new MemoryStream()) { using (var writer = new BinaryWriter(ms, Encoding.UTF8)) { writer.Write(CustomFlagBytes); writer.Write(propBytes.Length); writer.Write(propBytes); if (message.Content != null) { writer.Write(message.Content); } } result = new CloudQueueMessage((string)null); result.SetMessageContent2(ms.ToArray()); } return(result); }
/// <summary> /// Get a message from the MQ /// </summary> /// <returns></returns> private async Task GetMessageFromQueueAsync() { // Retrieve storage account from connection string. CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString); // Create the queue client. CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient(); // Retrieve a reference to a queue. CloudQueue queue = queueClient.GetQueueReference("myqueue"); // Get the message from the queue and update the message contents. CloudQueueMessage message = await queue.GetMessageAsync(); message.SetMessageContent2("Updated contents.", false); queue.UpdateMessage(message, TimeSpan.FromSeconds(60.0), // Make it invisible for another 60 seconds. MessageUpdateFields.Content | MessageUpdateFields.Visibility); }
public void UpdateContent() { // Retrieve storage account from connection string. CloudStorageAccount storageAccount = CloudStorageAccount.Parse( CloudConfigurationManager.GetSetting("StorageConnectionString")); // Create the queue client. CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient(); // Retrieve a reference to a queue. CloudQueue queue = queueClient.GetQueueReference("msgqueue"); // Get the message from the queue and update the message contents. CloudQueueMessage message = queue.GetMessage(); message.SetMessageContent2("food Ready.", false); queue.UpdateMessage(message, TimeSpan.FromSeconds(30.0), // Make it invisible for another 60 seconds. MessageUpdateFields.Content | MessageUpdateFields.Visibility); }
private static async Task ProcessAsync() { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); IConfigurationRoot configuration = builder.Build(); var storageAccount = CloudStorageAccount.Parse(configuration.GetConnectionString("StorageConnectionString")); var cloudQueueClient = storageAccount.CreateCloudQueueClient(); var queue = cloudQueueClient.GetQueueReference("examplequeue"); if (queue == null) { Console.WriteLine("Please run 'Queue Message Creator' firstly"); return; } await queue.FetchAttributesAsync(); // this method asks the Queue service to retrieve the queue attributes, including the message count. if (queue.ApproximateMessageCount != null) // ApproximateMessageCount property returns the last value retrieved by the FetchAttributes method, without calling the Queue service. { Console.WriteLine($"Approximate count for the queue is {queue.ApproximateMessageCount}"); } var peekedMessage = await queue.PeekMessageAsync(); while (peekedMessage != null) { Console.WriteLine(); Console.WriteLine($"Contents of message: {peekedMessage.AsString}"); Console.WriteLine("What would you like to do?"); Console.WriteLine("1 - Process / Delete message"); Console.WriteLine("2 - Modify / Re-queue message"); Console.WriteLine("3 - Delay / Re-queue message"); Console.WriteLine("4 - Exit"); var result = Console.ReadLine(); CloudQueueMessage message = null; switch (result) { case "1": // delete message message = await queue.GetMessageAsync(); Console.WriteLine($"Retrieved message with content '{message.AsString}'"); await queue.DeleteMessageAsync(message); Console.WriteLine("Deleted message"); break; case "2": // update message message = await queue.GetMessageAsync(); Console.WriteLine($"Retrieved message with content '{message.AsString}'"); message.SetMessageContent2($"Updated contents: {message.AsString}", false); await queue.UpdateMessageAsync(message, TimeSpan.FromSeconds(0.0), MessageUpdateFields.Visibility | MessageUpdateFields.Content); Console.WriteLine("Modified / re-queued message"); break; case "3": // ignore message and make it invisible for 10 seconds message = await queue.GetMessageAsync(TimeSpan.FromSeconds(10.0), null, null); Console.WriteLine("Ignored / made invisible for 10 seconds message"); break; default: Environment.Exit(0); break; } peekedMessage = await queue.PeekMessageAsync(); } }