0

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.

Maayan
  • 61
  • 5
  • The short answer is no, you can't create processor dynamically using QueueTrigger but you can write code to create receivers based on your requirements – Thomas May 04 '18 at 23:14

1 Answers1

2

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.

Joey Cai
  • 18,968
  • 1
  • 20
  • 30
  • 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) – Maayan May 03 '18 at 10:41
  • 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](https://maximelabelle.wordpress.com/2014/04/09/performing-parallel-processing-in-azure-web-jobs/) for more details. – Joey Cai May 04 '18 at 02:05
  • 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. – Joey Cai May 04 '18 at 06:21