A.J*_*orr 3 c# asp.net azure azure-queues azure-webjobs
我在 azure 上有一个 WebJob,可以同时处理来自多个队列的消息:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
而在主
static void Main()
{
JobHostConfiguration config = new JobHostConfiguration();
config.Queues.BatchSize = 3;
config.Queues.MaxDequeueCount = 1;
var host = new JobHost(config);
host.RunAndBlock();
}
Run Code Online (Sandbox Code Playgroud)
这里:message.Substring(message.Length - 3, 3)
只检查文件的扩展名。
我的问题是,我将如何使 queue1 的批量大小与 queue2 不同,我可以使用不同的配置制作第二个 jobhost 并拥有host.RunAndBlock()
和host2.RunAndBlock()
吗?我将如何指定作业主机应该做什么队列?
我也尝试过 Groupqueuetriggers,但不幸的是它们采用了字符串,在我的情况下,我实际上无法将列表传递给队列。:(
为了解决这个问题,您需要提供 IQueueProcessorFactory 的自定义实现。您只需要一个 JobHost。
这里有一个关于如何执行此操作的示例。
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();
}
Run Code Online (Sandbox Code Playgroud)
在CustomQueueProcessorFactory 中,您可以根据队列名称插入自定义配置。
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);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1159 次 |
最近记录: |