Is there a way to dynamically determine the amount and names of queues triggered in Azure webjob during startup time?
Asked Answered
G

1

2

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?

I know it is possible to do this for a single queue using INameResolver, but I couldn't find a solution for multiple queues.

Godunov answered 3/5, 2018 at 7:6 Comment(1)
The short answer is no, you can't create processor dynamically using QueueTrigger but you can write code to create receivers based on your requirementsBarnsley
M
3

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.

In Program:

static void Main()
{
    var host = new JobHost(new JobHostConfiguration
    {
        NameResolver = new QueueNameResolver(),

    });
    host.RunAndBlock();
}

In Function:

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");
    }
}

In QueueNameResolver:

public class QueueNameResolver : INameResolver
{
    public string Resolve(string name)
    {
        return ConfigurationManager.AppSettings[name].ToString();
    }
}

In App.config:

<appSettings>
    <add key="queuename1" value="queue"/>
    <add key="queuename2" value="myqueue"/>
</appSettings>

No matter you add message to queue or myqueue, it will always listen to them.

Marla answered 3/5, 2018 at 9:54 Comment(3)
Thanks, do you think there is a way of doing this without the Functions code being aware to the number of queues? (meaning, without having to manually write a new triggered method for each queue that is added)Godunov
You could try to perform Parallel Processing in Azure Web Jobs. Run multiple instances of webjob. This allows to have a number of Web Job instances equal to five times the number of instances of your web sites. You could refer to this article for more details.Marla
If you are worry about heavy work load, you could scare out more instances to webjobs. If not, I suggest that you could refer to the above code.Marla

© 2022 - 2024 — McMap. All rights reserved.