简体   繁体   English

Quartz.Net 2.0.1触发器

[英]Quartz.Net 2.0.1 Trigger

I have a web application and I am trying to setup a trigger to start when the application has started and then trigger every 5 minutes 我有一个Web应用程序,我试图设置一个触发器,使其在应用程序启动时启动,然后每5分钟触发一次

Everything is within Global.asax - seemed like the right place to put it: 一切都在Global.asax中-似乎是放置它的正确位置:

public class Global : HttpApplication
{
    public static StdSchedulerFactory SchedulerFactory;
    public static IScheduler Scheduler;
    public static ITrigger ImageTrigger;

    protected void Application_Start(object sender, EventArgs e)
    {
        SchedulerFactory = new StdSchedulerFactory();
        Scheduler = SchedulerFactory.GetScheduler();

        Scheduler.Start();

        ImageTrigger = TriggerBuilder.Create()
                                     .WithIdentity("ImageTrigger", "Group1")
                                     .StartNow()
                                     .WithSimpleSchedule(x => x.RepeatForever().WithIntervalInMinutes(5))
                                     .Build();

        var imageJob = JobBuilder.Create<DownloadImages>()
                                     .WithIdentity("DownloadImages" , "Group1")
                                     .Build();
        Scheduler.ScheduleJob(imageJob, ImageTrigger);
    }
...
}

So I assumed having a simple schedule use .WithIntervalInMiniutes() with cause the job to be invoked or have it got it massively wrong? 因此,我假设有一个简单的计划表使用.WithIntervalInMiniutes(),导致该作业被调用,或者该作业有很大错误?

Ps I have also tried: PS我也试过了:

        AlertTrigger = TriggerBuilder.Create()
                                     .WithIdentity("AlertTrigger", "Group1")
                                     .StartNow()
                                     .WithCronSchedule("0 0/1 * * * ?")
                                     .Build();

Followed by screaming at the computer! 随后大喊大叫的电脑!

Thanks in advance for your help. 在此先感谢您的帮助。

Matt 马特

I've tried your code and it works properly. 我已经尝试过您的代码,并且可以正常工作。
I don't think a web-service is the best option to run scheduled jobs cause of it's nature. 我不认为网络服务是运行计划作业的最佳选择,因为它是自然的。

I would suggest you to read ASP.NET Application Life Cycle . 我建议您阅读ASP.NET应用程序生命周期

Application_Start Application_Start

Called when the first resource (such as a page) in an ASP.NET application is requested. 当请求ASP.NET应用程序中的第一个资源(例如页面)时调用。 The Application_Start method is called only one time during the life cycle of an application. 在应用程序的生命周期中,仅一次调用Application_Start方法。 You can use this method to perform startup tasks such as loading data into the cache and initializing static values. 您可以使用此方法执行启动任务,例如将数据加载到缓存中并初始化静态值。 You should set only static data during application start. 您应该在应用程序启动期间仅设置静态数据。 Do not set any instance data because it will be available only to the first instance of the HttpApplication class that is created. 不要设置任何实例数据,因为它仅对所创建的HttpApplication类的第一个实例可用。

ASP.NET worker processes running in IIS are shutdown and recycled after a certain amount of time of inactivity. IIS在IIS中运行的ASP.NET辅助进程经过一定时间的不活动后将被关闭并回收 You can change this behavioral, though. 但是,您可以更改此行为。

Another interesting article can be read here . 可以在这里阅读另一篇有趣的文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM