Scheduled execution of task scheduling based on ASP. NET MVC (C #) and Quartz.net component implementations

Source: Internet
Author: User

In the previous article , recommend a simple, lightweight, powerful c#/asp. NET Timer Task Execution Manager component –fluentschedulerand simple, lightweight, powerful c#/asp. NET timed dispatch task Execution Management component –fluentscheduler example, we know and understand the Fluentscheduler this lightweight scheduled Task Scheduler execution component. Today we are going to introduce you to a component on scheduled task scheduling execution –quartz.net,quartz.net is a. NET implementation of the Java version of Quartz.

Compared with the Fluentscheduler implementation of scheduling tasks, the use of simple, less configuration features, quartz.net configuration is slightly more complex. Below we will join an ASP. NET MVC website application to perform a small instance of timed task debugging to understand Quartz.net's simple usage, and of course WebForm Web applications can use Quartz.net for scheduled tasks.

First, we open Visual Studio 2015 and create an ASP. NET MVC Web application project named Quartznetmvcdemo:

The quartz.net component is then installed through the Package Manager console: Install-package Quartz

Quartz.net A simple task consists of at least three parts of the implementation: job, trigger (trigger), and Scheduler (scheduler). Where job is the business logic you need to execute in a timed task, trigger specifies when and according to what rules the job is executed, and the final job and trigger are registered to the Scheduler (scheduler). Scheduler is responsible for coordinating job and trigger operations.

In Quartz.net, a job (job) is a class, In order for the job to execute in the quartz.net system, we must implement the Execute method of the Ijob interface provided by quartz.net, such as the Ijob interface Reportjob class implemented in this example:

usingSystem;usingQuartz;usingSystem.IO;namespacequartznetmvcdemo{ Public classReportjob:ijob { Public voidExecute (Ijobexecutioncontext context) {varReportdirectory =string. Format ("~/reports/{0}/", DateTime.Now.ToString ("yyyy-mm")); Reportdirectory=System.Web.Hosting.HostingEnvironment.MapPath (reportdirectory); if(!directory.exists (reportdirectory))      {directory.createdirectory (reportdirectory); }      varDailyreportfullpath =string. Format ("{0}report_{1}.log", Reportdirectory, DateTime.Now.Day); varLogcontent =string. Format ("{0}==>>{1}{2}", DateTime.Now,"Create new log.", Environment.NewLine);    File.appendalltext (Dailyreportfullpath, logcontent); }  }}

The Execute method has a Ijobexecutioncontext interface object as a parameter that contains configuration information for the job (jobs) that defines the class. Of course, as an example, in this case, we are not using this parameter.

Next, we need to implement a Trigge (trigger), the sample code is as follows:

Using quartz;using Quartz.impl; namespace quartznetmvcdemo{public  class Reportjobscheduler  {public    static void Start ()    {      IScheduler Scheduler = Stdschedulerfactory.getdefaultscheduler ();      Scheduler. Start ();      Ijobdetail job = jobbuilder.create<reportjob> (). Build ();      Itrigger trigger = Triggerbuilder.create ()        . Withidentity ("Triggername", "GroupName")        . Withsimpleschedule (t =          t.withintervalinseconds (5)           . RepeatForever ())           . Build ();       Scheduler. Schedulejob (Job, trigger);}}}  

This code snippet you can put in any of your project's program can be called anywhere, the class name you can also take it, it's no matter. As long as you use this class correctly reference.

In the code, we created a scheduler (scheduler) with Stdschedulerfactory.getdefaultscheduler () and started the scheduler with it. A simple quartz.net trigger is created and configured for this trigger: Specifies that the trigger name is Triggername, that the trigger is grouped into groupname, that the trigger is triggered every 5 seconds, and that the trigger is always looped. Finally through the scheduler. The Schedulejob () method registers the job (jobs) and trigger (triggers) in the scheduler so that a complete scheduled task is completed.

Finally, one of the things we have to do is start our custom timed tasks, and we put this task into the Application_Start method of the global CS file (Global.asax) of the project program to execute:

1 usingSYSTEM.WEB.MVC;2 usingSystem.Web.Optimization;3 usingSystem.Web.Routing;4  5 namespaceQuartznetmvcdemo6 {7    Public classMvcApplication:System.Web.HttpApplication8   {9     protected voidApplication_Start ()Ten     { One Arearegistration.registerallareas (); A filterconfig.registerglobalfilters (globalfilters.filters); - routeconfig.registerroutes (routetable.routes); - bundleconfig.registerbundles (bundletable.bundles); the         -       //start a scheduled task - Reportjobscheduler.start (); -     } +   } -}

OK, now that all the operations have been completed, press F5 to run our ASP. NET MVC timed Task Scheduler to execute the sample program. A minute to open our log file, if the program is running properly, then you will see the following log

How about, Quartz.net implementation of scheduled task scheduling is also relatively simple? Of course, this is just a simple example of quartz.net, and Quartz.net has many more advanced features, such as job scheduling that supports configuration files, job cycles that support cron, and so on.

Scheduled execution of task scheduling based on ASP. NET MVC (C #) and Quartz.net component implementations

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.