使用Azure Functions中的Timer触发器进行队列处理

时间:2017-05-10 13:01:04

标签: azure azure-functions

我有一个Azure函数,它处理一个队列,其中包含一些需要插入数据库的记录。它的工作很好。但我想使用Timer触发器而不是queueTrigger,这样我就可以每隔30秒检查一次记录。有人可以帮我这个。 我已经尝试了https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer中描述的Timer的基本实现,但我不知道如何处理记录。

3 个答案:

答案 0 :(得分:1)

您可以通过队列输入绑定绑定到CloudQueue。

以下是TimerTrigger的示例代码,用于从队列中提取消息。

<强> Function.json

{
  "bindings": [
    {
      "type": "timerTrigger",
      "name": "myTimer",
      "schedule": "0 * * * * *",
      "direction": "in"
    },
    {
      "type": "queue",
      "name": "clQueue",
      "queueName": "myqueue",
      "connection": "",
      "direction": "in"
    }
  ],
  "disabled": false
}

Run.csx

#r "Microsoft.WindowsAzure.Storage"

using Microsoft.WindowsAzure.Storage.Queue;

public static async Task Run(TimerInfo myTimer, CloudQueue clQueue, TraceWriter log)
{
    var message = await clQueue.GetMessageAsync();
    log.Info($"{message.As​String}");        

    clQueue.DeleteMessage(message);
}

答案 1 :(得分:0)

根据您的上一条评论,如果您肯定不需要使用队列触发器(请记住,如果您有太多消息,则无关紧要,Azure将扩展并设置处理所有消息所需的资源,但运行一段时间可能会很昂贵)您只需上传相同的代码,例如在控制台应用程序中使用所有需要的库的Azure函数。

以下是如何在C#中使用队列数据的示例:https://docs.microsoft.com/en-us/azure/storage/storage-dotnet-how-to-use-queues

您只需要调整代码以作为.csx脚本运行(您在控制台应用程序的Main方法中放入的代码将与您在.csx脚本的Run方法中放置的代码相同)。

答案 2 :(得分:0)

这是对我有用的答案: 使用计时器触发器时,没有直接绑定到Azure队列或ServiceBus队列。您必须自己编写Queue连接和读取逻辑,类似于未使用Function或Console App时的操作。作为示例,从ServiceBus队列中读取单个消息的内容类似于以下内容:

var connectionString =“Endpoint = sb://.servicebus.windows.net/; SharedAccessKeyName = RootManageSharedAccessKey; SharedAccessKey =”“; var queueName =“”;

var client = QueueClient.CreateFromConnectionString(connectionString,queueName); var message = client.Receive();