简体   繁体   English

Azure 服务总线 Azure 函数触发并获取所有消息

[英]Azure service bus Azure Function trigger and get all messages

I have a queue that a lot of high frequency data is put in. I have an azure function that will trigger when new messages are written to it and write the message to a Azure sql database.我有一个队列,其中放置了大量高频数据。我有一个 azure 函数,该函数将在向其中写入新消息并将消息写入 Azure sql 数据库时触发。 My problem is that there are too many messages to write so what I want to do is kind of "accumulate" messages and then write them all at once to the database.我的问题是有太多的消息要写,所以我想做的是“积累”消息,然后一次将它们全部写入数据库。 I don't see any kind of functionality in Azure function service bus integration to get all(or x) messages and handle them.我在 Azure 函数服务总线集成中没有看到任何类型的功能来获取所有(或 x)消息并处理它们。

Is something like this at all possible?这样的事情可能吗?

Any help is appreciated.任何帮助表示赞赏。

My way of solving this so far is to avoid service bus trigger because it involves that the function executes one instance per message.到目前为止,我解决这个问题的方法是避免服务总线触发器,因为它涉及该函数对每个消息执行一个实例。 I do a time trigger every 10 seconds and I use the method Receive from the subscription client.我每 10 秒执行一次时间触发器,并使用来自订阅客户端的方法接收。

Indeed in this case you would need to manually bring the Azure Service Bus library, create the needed classes and call the Receive method which you can tell how many items you want.实际上,在这种情况下,您需要手动引入 Azure 服务总线库,创建所需的类并调用 Receive 方法,您可以通过该方法判断所需的项目数量。 Do follow best practice that ASB classes (TopicClient and SubscriptionClient) should be set as singletons because they are expensive objects.请遵循 ASB 类(TopicClient 和 SubscriptionClient)应设置为单例的最佳实践,因为它们是昂贵的对象。

I also have to accumulate messages to sort them and deduplicate them so this is how I solved it.我还必须积累消息来对它们进行排序和重复数据删除,所以这就是我解决它的方法。 However, it would be very cool to have that kind of functionality out of the box from Azure Functions extension.但是,从 Azure Functions 扩展中获得这种开箱即用的功能会非常酷。

I'm just wondering why you need that batch insert.我只是想知道你为什么需要批量插入。 for every single message that triggers your function a new instance of you function is being created by Azure;对于触发您的函数的每条消息,Azure 都会创建一个新的函数实例; so performance won't be an issue here.所以性能在这里不会成为问题。 If you still wants to do batching, you may store incoming messages temporarily in a proxy Azure database/data store and query that proxy in specified periods and run your batch insert command.如果您仍然想要进行批处理,您可以将传入的消息临时存储在代理 Azure 数据库/数据存储中,并在指定的时间段内查询该代理并运行您的批量插入命令。 Please keep in mind that you must delete queried records after successful batch insert.请记住,您必须在批量插入成功后删除查询记录。

Azure Functions runtime retrieves and processes queue messages in batches. Azure Functions 运行时分批检索和处理队列消息。 And the default batchSize is 16 and the maximum batchSize is 32..This can be configured for 'queue' triggers by mentioning batchSize in host.json file of the Function App.默认batchSize为16,最大batchSize为32..这可以通过在Function App的host.json文件中提及batchSize来配置“队列”触发器。 Refer to know more on host.json 请参阅有关 host.json 的更多信息

Configuration settings for 'queue' triggers “队列”触发器的配置设置

"queues": {
  "maxPollingInterval": 2000,
  "visibilityTimeout" : "00:00:10",
  "batchSize": 16,
  "maxDequeueCount": 5,
  "newBatchThreshold": 8
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM