未加星标

Azure Storage Queues New Feature: Pop-Receipt on Add Message

字体大小 | |
[系统(windows) 所属分类 系统(windows) | 发布者 店小二04 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

As part of the “2016-05-31” REST API version, we have introduced the pop receipt on add message functionality, which has been a commonly requested feature by our users.

Pop receipt functionality for the Queue service is a great tool for developers to easily identify an enqueued message for further processing. Prior to the “2016-05-31” version, pop receipt value could only be retrieved when auser gets a message from the queue. To simplify this, we now make pop receipt value available in the Put Message (aka Add Message) response which allows users to update/delete a message without the need to retrieve the message first.

Below is a short code snippet that make use of this new feature using Azure Storage Client Library 8.0 for .NET.

// create initial message
CloudQueueMessage message = new CloudQueueMessage(" ");
// add the message to the queue, but keep it hidden for 3 min
queue.AddMessage(message, null, TimeSpan.FromSeconds(180));
//message.PopReceipt is now populated, and only this client can operate on the message until visibility timeout expires
.
.
.
// update the message (now no need to receive the message first, since we already have a PopReceipt for the message)
message.SetMessageContent(" ");
queue.UpdateMessage(message, TimeSpan.FromSeconds(180), MessageUpdateFields.Content | MessageUpdateFields.Visibility);
// remove the message using the PopReceipt before any other process sees it
await queue.DeleteMessageAsync(message.Id, message.PopReceipt);

A common problem in cloud applications is to help coordinate updates across non-transactional resources. As an example, an application that processes images or videos may:

1. Process an image

2. Upload it to a blob

3. Save metadata in a table entity

These steps can be tracked using the Queue service as the processes complete successfully using the following flow:

1. Add a state as a message to the Queue service

2. Process an image

3. Upload it to a blob

4. Save metadata in a table entity

5. Delete the message if all were successful

Remaining messages in the queue are simply images that failed to be processed, and can be consumed by a worker for cleanup. The scenario above is now made simpler with the popreceipt on add message feature, since in the 5th step the message can be deleted with the popreceipt value retrieved in the 1st step.

Quick Sample using the Face API from Azure Cognitive Services

In the following sample, we are going to be uploading photos from a local folder to the Blob service and we will also make use of the Face API to estimate each person’s age in the photos, storing as an entity in a table. This process will be tracked in a queue and once completed, the message will be deleted with the pop receipt value. The workflow for the sample is:

1. Find JPG files in ‘testfolder’

2. For each photo, repeat steps 2-7:

3. Upload a queue message representing the processing of this photo.

4. Call the Face API to estimate the age of each person in the photo.

5. Store the age information as an entity in the table.

6. Upload the image to a blob if at least one face is detected.

7. If both the blob and the table entity operation succeeded, delete the message from queue using the pop receipt.

// Iterate over photos in 'testfolder'
var images = Directory.EnumerateFiles("testfolder", "*.jpg");
foreach (string currentFile in images)
{
string fileName = currentFile.Replace("testfolder\\", "");
Console.WriteLine("Processing image {0}", fileName);
// Add a message to the queue for each photo. Note the visibility timeout
// as blob and table operations in the following process may take up to 180 seconds.
// After the 180 seconds, the message will be visible and a worker role can pick up
// the message from queue for cleanup. Default time to live for the message is 7 days.
CloudQueueMessage message = new CloudQueueMessage(fileName);
queue.AddMessage(message, null, TimeSpan.FromSeconds(180));
// read the file
using (var fileStream = File.OpenRead(currentFile))
{
// detect face and estimate the age
var faces = await faceClient.DetectAsync(fileStream, false, true, new FaceAttributeType[] { FaceAttributeType.Age });
Console.WriteLine(" > " + faces.Length + " face(s) detected.");
CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
var tableEntity = new DynamicTableEntity(DateTime.Now.ToString("yyMMdd"), fileName);
// iterate over detected faces
int i = 1;
foreach (var face in faces)
{// append the age info as property in the table entitytableEntity.Properties.Add("person" + i.ToString(), new EntityProperty(face.FaceAttributes.Age.ToString()));i++;
}
// upload the blob if a face was detected
if (faces.Length > 0)await blob.UploadFromFileAsync(currentFile);
// store the age info in the table
table.Execute(TableOperation.InsertOrReplace(tableEntity));
// delete the queue message with the pop receipt since previous operations completed successfully
await queue.DeleteMessageAsync(message.Id, message.PopReceipt);
}
}

Check out the full sample in our Github sample repository .

As always, if you have any feature requests please let us know by submitting your ideas to Azure Storage Feedback .

本文系统(windows)相关术语:三级网络技术 计算机三级网络技术 网络技术基础 计算机网络技术

主题: RESTGit
分页:12
转载请注明
本文标题:Azure Storage Queues New Feature: Pop-Receipt on Add Message
本站链接:http://www.codesec.net/view/523055.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(windows) | 评论(0) | 阅读(29)