有没有一种方法可以动态确定启动期间在Azure webjob中触发的队列的数量和名称? [英] Is there a way to dynamically determine the amount and names of queues triggered in Azure webjob during startup time?
问题描述
我正在使用带有队列触发功能的Azure webjob来监听多个Azure队列。每个队列的处理方法是相同的(但是队列仍然需要分开)。我想知道是否有办法在配置中存储队列名称列表并动态创建在启动时在这些队列上触发的函数?
I am using an Azure webjob with queue triggered functions to listen on several Azure queues. The processing method for each queue is identical (but the queues still need to be separate). I was wondering if there is a way to store the list of queue names in configuration and dynamically create functions that are triggered on those queues during startup time?
我知道可能使用INameResolver为单个队列使用INameResolver ,但找不到多个队列的解决方案。
I know it is possible to do this for a single queue using INameResolver, but I couldn't find a solution for multiple queues.
推荐答案
实际上,您可以在Function中编写服务器处理方法。当webjob运行时,它将遍历函数中的所有方法。
Actually, you could write server processing methods in a Function. When a webjob running, it will traverses all the methods in your function.
您可以参考以下代码来动态触发多个队列。
You could refer to the following code to dynamic trigger multiple queues.
在程序中:
static void Main()
{
var host = new JobHost(new JobHostConfiguration
{
NameResolver = new QueueNameResolver(),
});
host.RunAndBlock();
}
功能:
public class Functions
{
public static void ProcessQueueMessage([QueueTrigger("%queuename1%")] string message, TextWriter log)
{
log.WriteLine(message);
Console.WriteLine("success");
}
public static void ProcessQueueMessage1([QueueTrigger("%queuename2%")] string message, TextWriter log)
{
log.WriteLine(message);
Console.WriteLine("success2");
}
}
在QueueNameResolver中:
In QueueNameResolver:
public class QueueNameResolver : INameResolver
{
public string Resolve(string name)
{
return ConfigurationManager.AppSettings[name].ToString();
}
}
在App.config中:
In App.config:
<appSettings>
<add key="queuename1" value="queue"/>
<add key="queuename2" value="myqueue"/>
</appSettings>
无论您将消息添加到队列还是myqueue,它都将始终收听。
No matter you add message to queue or myqueue, it will always listen to them.
这篇关于有没有一种方法可以动态确定启动期间在Azure webjob中触发的队列的数量和名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!