Example of quartz.net implementation of timed task scheduling

Source: Internet
Author: User

Quartz an open source job scheduling framework, Opensymphony Open source project. Quartz.net is the Quartz C # porting version.

It has some very good features:

1: Support cluster, job grouping, job remote management.

2: Custom Fine time triggers, easy to use, job and trigger separation.

3: Database support, you can host Windows services, Website,winform and so on.

Basic concepts:

Scheduler of the job.

Ijob job interface. Inherits and implements execute, writing specific job logic for execution.

Jobbuilder generates a detailed job information (Jobdetail) based on the settings.

Triggerbuilder according to the rules, the production of the corresponding trigger

Application:


1 Create the MVC project and get the Quartz class library through NuGet


2 Package Task Factory


<summary>
Mission Factory
</summary>
<typeparam name= "T" > Work class </typeparam>
<param name= "Detailname" > Work name </param>
<param name= "Triggername" > Trigger name </param>
<param name= "Minute" > How long to start once </param>
private static void Jobsfactory<t> (String detailname, string triggername, int Minute)
where T:ijob
{
Factory 1
Ischedulerfactory factory = new Stdschedulerfactory ();
Start
IScheduler Scheduler = Factory. Getscheduler ();
Scheduler. Start ();
Describe work
Ijobdetail jobdetail = new Jobdetailimpl (detailname, NULL, typeof (T));
Trigger
Isimpletrigger trigger = new Simpletriggerimpl (Triggername,
Null
DateTime.Now,
Null
Simpletriggerimpl.repeatindefinitely,
Timespan.fromseconds (Minute));
Perform
Scheduler. Schedulejob (Jobdetail, Trigger);
}

3 Creating the work content and implementing the Ijob interface

public class Cancleorderjob:ijob
{
B_order_confrimcancelinfomanager manager = new B_order_confrimcancelinfomanager ();
public void Execute (Ijobexecutioncontext context)
{
Log4net. ILog logger = log4net. Logmanager.getlogger (System.Reflection.MethodBase.GetCurrentMethod (). DeclaringType);
var list = Manager. Getallhandleorder ();
if (list!= null)
{
foreach (var item in list)
{
if (item. Ghandletime < DateTime.Now)
{
Try
{
Manager. UpdateStatus (item.id);
}
catch (Exception e)
{
Logger. Error (E.message);
Throw
}
}
}
}
}
}

To start a task in global:

protected void Application_Start ()
{
Arearegistration.registerallareas ();
Webapiconfig.register (globalconfiguration.configuration);
Filterconfig.registerglobalfilters (globalfilters.filters);
Routeconfig.registerroutes (routetable.routes);
Bundleconfig.registerbundles (Bundletable.bundles);
Automatic cancellation of orders
Jobsfactory<cancleorderjob> ("Cancleorderdetial", "Cancleordertrigger", 1000);
Automatic Order Completion
Jobsfactory<completeorderjob> ("Completeorderdetial", "Completeordertrigger", 1000);
Load the log's configuration file
Log4net. Config.XmlConfigurator.Configure (New FileInfo (Server.MapPath ("~/web.config"));
}
Note: Log4.net log error logs are used here


Log4.net How the error log is used:

1 class libraries for application log4.net

2 Writing configuration Files

<configSections>
<!--for more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468-->
<section name= "EntityFramework" type= "System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, version=5.0.0.0, Culture=neutral, publickeytoken=b77a5c561934e089 "requirePermission=" false "/>
<section name= "log4net" type= "log4net". Config.log4netconfigurationsectionhandler, log4net "/>
</configSections>
<log4net debug= "true" >
<appender name= "Rollinglogfileappender" type= "log4net". Appender.rollingfileappender ">
<param name= "File" value= "Log//error_"/>
<param name= "Appendtofile" value= "true"/>
<param name= "maxsizerollbackups" value= "ten"/>
<param name= "Staticlogfilename" value= "false"/>
<param name= "Datepattern" value= "Yyyy-mm-dd". Log ""/>
<param name= "Rollingstyle" value= "Date"/>
<layout type= "log4net. Layout.patternlayout ">
<param name= "Conversionpattern" value= "%d-%m%n"/>
</layout>
</appender>
<root>
<level value= "DEBUG"/>
<appender-ref ref= "Rollinglogfileappender"/>
</root>

3 Read configuration file information in global


Load the log's configuration file
Log4net. Config.XmlConfigurator.Configure (New FileInfo (Server.MapPath ("~/web.config"));

4 Use:


Log4net. ILog logger = log4net. Logmanager.getlogger (System.Reflection.MethodBase.GetCurrentMethod (). DeclaringType);
Logger. Error (E.message);

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.