Azure WebJobs:具有不同批处理大小的队列触发器 [英] Azure WebJobs: Queue triggers with different batch sizes

查看:108
本文介绍了Azure WebJobs:具有不同批处理大小的队列触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在azure上有一个WebJob,它同时处理来自多个队列的消息:

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

并且在MAIN

static void Main()
    {

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

    }

此处: message.Substring( message.Length-3,3)只是检查文件的扩展名。

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

我的问题是,我将如何继续制作队列1的批处理大小与队列2的批处理大小不同,我可以使用不同的配置制作第二个作业宿主,并具有 host.RunAndBlock() host2.RunAndBlock()?我该如何指定作业主机应该在哪个队列中?

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?

我也尝试了Groupqueuetriggers,但不幸的是,他们使用了字符串,在我的情况下,我实际上无法将列表传递给队列。 :(

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

推荐答案

要解决此问题,您需要提供IQueueProcessorFactory的自定义实现。您只需要一个JobHost。

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

CustomQueueProcessorFactory 中,您可以插入自定义根据队列名称进行配置。

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

这篇关于Azure WebJobs:具有不同批处理大小的队列触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆