如何在 Azure Web 作业中使用 QueueTrigger 避免竞争条件?
How to avoid race conditions using QueueTrigger in Azure web job?
我有一个 WCF 服务,它将传入的 SOAP 消息从 第三方 推送到 Azure 存储队列。
第三方系统是一个自动系统,它以 BULK 的形式触发 SOAP 请求消息。
一旦在 WCF 中收到消息,WCF 服务就会读取消息并将其推送到 Azure 存储队列,然后由使用 [QueueTrigger] 属性配置的 Azure Web 作业读取。
public async Task UpdateRatesAsync([QueueTrigger("queue-name")] string message, TextWriter log)
The role of this function is update the Rates of the Product in the system. The web job is triggered as soon as there's a message in the queue and it starts processing the request. When multiple rate messages for the same product are pushed, the QueueTrigger function executes which tries to update the Rates for the same Product at the same time resulting in a race condition.
- How can I resolve this race condition? Is it by reading the message from the queue one by one?
- Do I need to try a different operation to read the message from the queue by removing the QueueTrigger?
如果我没记错的话,QueueTrigger 的批量大小默认为一次 1 条消息,当然你可以手动编辑它的配置,但你不应该 运行 一开始就陷入这个问题除非您已经编辑了批量大小。
static void Main()
{
JobHostConfiguration config = new JobHostConfiguration();
config.Queues.BatchSize = 1;
var host = new JobHost(config);
host.RunAndBlock();
[...]
}
是的,如果队列中的消息需要按特定顺序执行,您应该一次执行一个,因为这些消息的执行是异步的。
我有一个 WCF 服务,它将传入的 SOAP 消息从 第三方 推送到 Azure 存储队列。
第三方系统是一个自动系统,它以 BULK 的形式触发 SOAP 请求消息。 一旦在 WCF 中收到消息,WCF 服务就会读取消息并将其推送到 Azure 存储队列,然后由使用 [QueueTrigger] 属性配置的 Azure Web 作业读取。
public async Task UpdateRatesAsync([QueueTrigger("queue-name")] string message, TextWriter log)
The role of this function is update the Rates of the Product in the system. The web job is triggered as soon as there's a message in the queue and it starts processing the request. When multiple rate messages for the same product are pushed, the QueueTrigger function executes which tries to update the Rates for the same Product at the same time resulting in a race condition.
- How can I resolve this race condition? Is it by reading the message from the queue one by one?
- Do I need to try a different operation to read the message from the queue by removing the QueueTrigger?
如果我没记错的话,QueueTrigger 的批量大小默认为一次 1 条消息,当然你可以手动编辑它的配置,但你不应该 运行 一开始就陷入这个问题除非您已经编辑了批量大小。
static void Main()
{
JobHostConfiguration config = new JobHostConfiguration();
config.Queues.BatchSize = 1;
var host = new JobHost(config);
host.RunAndBlock();
[...]
}
是的,如果队列中的消息需要按特定顺序执行,您应该一次执行一个,因为这些消息的执行是异步的。