Reputation: 380
I would like to create one web job (listener) to listen to all queues in a storage. If there is any new message, then it triggers a handler.
Azure WebJobs SDK offers a solution which only listens to one queue:
public class Functions
{
// This function will get triggered/executed when a new message is written
// on an Azure Queue called queue.
public static async Task ProcessQueueMessage(
[QueueTrigger("%test%")] CloudQueueMessage message,
IBinder binder)
{
//do some stuff
}
}
This approach is good, but I need: 1) to listen to different queues 2) to inject a class to this class which I think I can't
So I am thinking of creating my own listener. I want to create several threats and each threat listens to one queue. Then when i run the web job it starts all threats.
I wonder if anyone can suggest a better solution. Code sample would be really good to have.
Thanks
Upvotes: 0
Views: 2939
Reputation: 380
As @lopezbertoni suggested I created two methods in Functions and I've used IJobActivator to inject classes to Functions. See example below:
public class Program
{
static void Main()
{
var builder = new ContainerBuilder();
builder.RegisterType<MyMessageHandler>().As<IMessageHandler>();
builder.RegisterType<Functions>()
.InstancePerDependency();
var host = new JobHost(new JobHostConfiguration
{
JobActivator = new AutofacJobActivator(builder.Build())
});
host.RunAndBlock();
}
}
public class AutofacJobActivator : IJobActivator
{
private readonly IContainer _container;
public AutofacJobActivator(IContainer container)
{
_container = container;
}
public T CreateInstance<T>()
{
return _container.Resolve<T>();
}
}
public class Functions
{
private IMessageHandler _myService;
//You can use Dependency Injection if you want to.
public Functions(IMessageHandler myService)
{
_myService = myService;
}
// This function will get triggered/executed when a new message is written
// on an Azure Queue called queue.
public async Task ProcessQueueMessage1(
[QueueTrigger("test1")] CloudQueueMessage message,
IBinder binder)
{
_myService.HandleMessage(message.AsString);
Console.WriteLine("ProcessQueueMessage1 was run");
await Task.CompletedTask;
}
public async Task ProcessQueueMessage2(
[QueueTrigger("test2")] CloudQueueMessage message,
IBinder binder)
{
_myService.HandleMessage(message.AsString);
Console.WriteLine("ProcessQueueMessage2 was run");
await Task.CompletedTask;
}
}
Upvotes: 2
Reputation: 3641
You don't need to create your own listener unless you really want to. The Azure Webjobs SDK does the heavy lifting for you already.
Here's an example Functions.cs that can process data from different queues. You can inject services into Functions.cs so that different queues are processed by different services if you want to.
private readonly IMyService _myService;
//You can use Dependency Injection if you want to.
public Functions(IMyService myService)
{
_myService = myService;
}
public void ProcessQueue1([QueueTrigger("queue1")] string item)
{
//You can get the message as a string or it can be strongly typed
_myService.ProcessQueueItem1(item);
}
public void ProcessQueue2([QueueTrigger("queue2")] MyObject item)
{
//You can get the message as a string or it can be strongly typed
_myService.ProcessQueueItem2(item);
}
Hope this helps
Upvotes: 4