带有队列和System.Threading.Timer的Azure WebJob [英] Azure WebJob with queues and System.Threading.Timer

查看:41
本文介绍了带有队列和System.Threading.Timer的Azure WebJob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Azure WebJob成功地从服务总线队列中获取消息.但是我想使用同一WebJob每5秒运行一些方法.

I'm using Azure WebJob to get messages from a service bus queue with success. But i wanted to use this same WebJob to run some methods every 5 seconds.

我尝试了以下方法,并且在本地运行良好,但是当我发布时,它只能运行一次.天蓝色日志没有错误.

I've tried the following approach, and locally it run fine, but when i publish it only runs once. No errors on azure logs.

我在做什么错了?

感谢您的帮助.

   static void Main()
    {
        try
        {

    var testTimer = new System.Threading.Timer(e => TestMethod(), null, TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(5));

            SetupJobHost();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }

     private static void TestMethod()
    {
        Console.WriteLine("Test");
    }

推荐答案

我建议采用其他方法,并使用

I recommend taking a different approach and using a TimerTrigger. You can use a simple chron expression that will cause your method to be executed on a set schedule. If you go this route, make sure that you deploy your WebJob as a triggered job (not continuous!) and that you call the JobHostConfiguration's UseTimers() method before calling the JobHost's RunAndBlock method. This is a much easier and cleaner approach than rolling your own timer service.

这篇关于带有队列和System.Threading.Timer的Azure WebJob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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