Use Quartz. NET in ASP. NET MVC4 to execute a scheduled task, mvc4quartz.net

Source: Internet
Author: User

Use Quartz. NET in ASP. NET MVC4 to execute a scheduled task, mvc4quartz.net

This article uses Quartz. NET to execute scheduled tasks in ASP. net mvc.

 

First install Quartz. NET through NuGet.

 

The general idea of using Quartz. NET is:
1. Implement the IJob interface to define specific tasks
2. Use the Quartz. net api to define scheduled task rules
3. Register a scheduled task in Application_Start

 

Implements the IJob interface.

 

    public class MyJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Debug.WriteLine("Hello at " + DateTime.Now.ToString());
        }
    }

 

Define rules in Global. asax and register them in Application_Start.

 

       protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            MyJobs();
            
        }
        private static void MyJobs()
        {
// Factory
            ISchedulerFactory factory = new StdSchedulerFactory();
// Start
            IScheduler scheduler = factory.GetScheduler();
            scheduler.Start();
// Describe the work
            IJobDetail jobDetail = new JobDetailImpl("mylittlejob",null, typeof(MyJob));
// Trigger
            ISimpleTrigger trigger = new SimpleTriggerImpl("mytrigger",
                null,
                DateTime.Now,
                null,
                SimpleTriggerImpl.RepeatIndefinitely,
                TimeSpan.FromSeconds(10));
// Execute
            scheduler.ScheduleJob(jobDetail, trigger);
        }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.