Azure WebJobs: Queue triggers with different batch sizes

陌路散爱 提交于 2019-12-11 07:04:54

问题


I have a WebJob on azure that processes messages from multiple queues at the same time:

public async static Task ProcessQueueMessage1([QueueTrigger("queue1")] string message)
    {


        switch (message.Substring(message.Length - 3, 3))
        {
            case "tze":
                await Parser.Process1(message);
                break;
            default:
                break;
        }
    }


    public async static Task ProcessQueueMessage2([QueueTrigger("queue2")] string message)
    {


        switch (message.Substring(message.Length - 3, 3))
        {
            case "tzr":
                await Parser.Process2(message);
                break;
            default:
                break;
        }
    }

And the in the MAIN

static void Main()
    {

        JobHostConfiguration config = new JobHostConfiguration();
        config.Queues.BatchSize = 3;
        config.Queues.MaxDequeueCount = 1;
        var host = new JobHost(config);
        host.RunAndBlock();

    }

here: message.Substring(message.Length - 3, 3) just checks the extension of the file.

My question is, how would I go on about making the batch size of queue1 different than queue2, can I make second jobhost with a different configuration and have host.RunAndBlock() and host2.RunAndBlock()?? How would I specify what queue should the jobhost do?

I have also tried the Groupqueuetriggers, but unfortunately they take of string, and in my situation I cannot actually pass lists to the queue. :(


回答1:


In order to tackle this you need to provide a custom implementation of IQueueProcessorFactory. You only need one JobHost.

There's an example on how to do this here.

    static void Main()
    {

        //Configure JobHost
        var config = new JobHostConfiguration();
        config.Queues.BatchSize = 32;
        config.Queues.MaxDequeueCount = 6;
        // Create a custom configuration
        // If you're using DI you should get this from the kernel
        config.Queues.QueueProcessorFactory = new CustomQueueProcessorFactory();

        //Pass configuration to JobJost
        var host = new JobHost(config);
        // The following code ensures that the WebJob will be running continuously
        host.RunAndBlock();
    }

And in the CustomQueueProcessorFactory you can insert the custom configuration based on the queue name.

public class CustomQueueProcessorFactory : IQueueProcessorFactory
{
    public QueueProcessor Create(QueueProcessorFactoryContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException(nameof(context));
        }
        if (context.Queue.Name == "queuename1")
        {
            context.MaxDequeueCount = 10;
        }
        else if (context.Queue.Name == "queuename2")
        {
            context.MaxDequeueCount = 10;
            context.BatchSize = 1;
        }

        return new QueueProcessor(context);
    }
}


来源:https://stackoverflow.com/questions/52629666/azure-webjobs-queue-triggers-with-different-batch-sizes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!