{"id":368,"date":"2015-12-06T20:00:36","date_gmt":"2015-12-06T19:00:36","guid":{"rendered":"http:\/\/www.cymbeline.ch\/?p=368"},"modified":"2017-11-13T12:30:53","modified_gmt":"2017-11-13T11:30:53","slug":"azure-queue-agent-introduction","status":"publish","type":"post","link":"https:\/\/cymbeline.ch\/2015\/12\/06\/azure-queue-agent-introduction\/","title":{"rendered":"Azure Queue Agent – Introduction"},"content":{"rendered":"
For a small side project I’ve been working on I needed a way to schedule background tasks and handle them on a pool of workers. Since I was planning to run this on Azure, the Queues offered with Azure Storage<\/a> seemed to be a no-brainer. Even more so since the Azure Scheduler<\/a>, which can be used to periodically execute some action, can also be used to add messages to Queues. I figured that I wasn’t the only one needing something to handle such tasks, so I decided to build a lightweight open-source library for this.<\/p>\n AQuA comes with two main components: a Producer<\/em> and a Consumer<\/em>. As the names suggest, the Producer<\/em> can be used to produce (i.e. enqueue) new jobs, while the Consumer<\/em> can be used to consume (i.e. dequeue and then handle) jobs from the queue. Job Descriptors are used to define which job should be executed and what parameters should be used for the execution. They are encoded as simple JSON objects like the one below, i.e. they can also easily be written manually (e.g. when used with the Azure Scheduler). That said, with AQuA it is very simple to build a scalable and robust collection of workers which take care of all your background processing jobs.<\/p>\n The above example for instance would queue the HelloWho<\/em> job, which does nothing more but print the value of the Who parameter on stdout like this: \u00e2\u20ac\u0153Hello, <Who>!\u00e2\u20ac\u009d. In addition, the Azure Queue Agent Consumer can be configured to either delete or requeue messages which were badly formatted, using unknown jobs or which could not be executed successfully, such that you can even use a single queue for multiple different pools of workers, should you ever find yourself in that situation.<\/p>\n The Azure Queue Agent is available as a NuGet package<\/a>, currently however only in pre-release. You can get it like this:<\/p>\n Once this is done, you need to create an instance of Producer<\/em> (if you want to create job requests from your code), and an instance of Consumer<\/em> (for when you want to handle job requests).<\/p>\n This should get you going for now. I’ll follow up with more later. Oh, and you can read the sources on GitHub<\/a>.<\/p>\n <\/p>","protected":false},"excerpt":{"rendered":" For a small side project I’ve been working on I needed a way to schedule background tasks and handle them on a pool of workers. Since I was planning to run this on Azure, the Queues offered with Azure Storage seemed to be a no-brainer. Even more so since the Azure Scheduler, which can be … Continue reading “Azure Queue Agent – Introduction”<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[61],"tags":[81,83,84,82,85],"yoast_head":"\nEnter Azure Queue Agent (AQuA)<\/h2>\n
\r\n{ "Job": "HelloWho", "Properties": { "Who": "World" } }\r\n<\/pre>\n
Getting Started<\/h2>\n
Install-Package aqua.lib -Pre<\/pre>\n
\r\n\/\/ Setup and initialization\r\nJobFactory factory = new JobFactory();\r\n\r\n\/\/ Register all the jobs you want your consumer to be able to handle.\r\nfactory.RegisterJobType(typeof(HelloWho));\r\nfactory.RegisterJobType(typeof(MyBackgroundJob));\r\n\r\n\/\/ Use the storage account from the emulator with queue "jobs".\r\nConnectionSettings connection = new ConnectionSettings("jobs");\r\n\r\nProducer producer = new Producer(connection, factory);\r\nConsumer consumer = new Consumer(connection, factory);\r\n\r\n\/\/ Produce (i.e. enqueue) a HelloWho job request.\r\nHelloWho job = new HelloWho() { Who = "Azure Queue Agent Example" };\r\nproducer.One(job);\r\n\r\n\/\/ Consume (i.e. dequeue and handle) a job request.\r\nconsumer.One();\r\n<\/pre>\n