Use quartz.net for Job Scheduling in ASP. NET

Source: Internet
Author: User

QuartzIs a Java open-source job scheduling framework. Http://www.opensymphony.com/quartz/

There is an easy-to-understand article on the IBM websiteArticle: Http://www.ibm.com/developerworks/cn/java/j-quartz/

Quartz.net is ported from Java to. net. Http://quartznet.sourceforge.net/

I have found many tutorials online, but none of them are about how to use them in ASP. NET.Code. Since it applies to any. netProgramOf course, this applies to Asp.net web applications.

(1) perform related configuration in Web. config

<Configsections>
<Section name = "quartz" type = "system. configuration. namevaluesectionhandler, system, version = 1.0.5000.0, culture = neutral, publickeytoken = b77a5c561934e089"/>
<Sectiongroup name = "common">
<Section name = "logging" type = "common. Logging. configurationsectionhandler, common. Logging"/>
</Sectiongroup>
</Configsections>

<Common>
<Logging>
<Factoryadapter type = "common. Logging. Simple. consoleoutloggerfactoryadapter, common. Logging">
<Arg key = "showlogname" value = "true"/>
<Arg key = "showdatatime" value = "true"/>
<Arg key = "level" value = "debug"/>
<Arg key = "datetimeformat" value = "HH: mm: SS: fff"/>
</Factoryadapter>
</Logging>
</Common>
<Quartz>
<Add key = "quartz. scheduler. instancename" value = "exampledefaultquartzscheduler"/>
<Add key = "quartz. threadpool. Type" value = "quartz. simpl. simplethreadpool, quartz"/>
<Add key = "quartz. threadpool. threadcount" value = "10"/>
<Add key = "quartz. threadpool. threadpriority" value = "2"/>
<Add key = "quartz. jobstore. misfirethreshold" value = "60000" type = "codeph" text = "/codeph"/>
<Add key = "quartz. jobstore. Type" value = "quartz. simpl. ramjobstore, quartz"/>
</Quartz>

In addition, I added a configuration item: <etettings>
<Add key = "cronexpr" value = "0 0 8-17/1? * 2-6 "/>
</Appsettings>

(2) create a common class to implement the quartz. ijob Interface Public class retrieveaj2003t140job: quartz. ijob
{< br> Private Static dataview aj2003view;
Public retrieveaj2003t140job ()
{< br> //
// todo: add the constructor logic here
//
}
Public void execute (quartz. jobexecutioncontext context)
{< br> // throw new exception ("the method or operation is not implemented. ");
// your processing logic, that is," work "
}

The interface is very simple. You only need to perform logical processing in the execute () method. For example, reading database data or reading emails.

(3) start the job scheduling in the global. asax File
This allows us to start job scheduling when the web application is started.

<% @ Import namespace = "quartz" %>

<SCRIPT runat = "server">

Ischeduler sched;
Void application_start (Object sender, eventargs E)
{
// Code that runs when the application starts
Ischedulerfactory Sf = new quartz. impl. stdschedulerfactory ();
Sched = SF. getscheduler ();
Jobdetail job = new jobdetail ("job1", "group1", typeof (retrieveaj2003t140job ));

String cronexpr = configurationmanager. etettings ["cronexpr"];
Crontrigger trigger = new crontrigger ("trigger1", "group1", "job1", "group1", cronexpr );
Sched. addjob (job, true );
Datetime Ft = sched. schedulejob (trigger );
Sched. Start ();

}

Void application_end (Object sender, eventargs E)
{
// Code that runs when the application is closed
If (sched! = NULL)
{
Sched. Shutdown (true );
}
}



</SCRIPT>

It should be noted that when application_end is used, quartz should be disabled.

OK. It can be used normally in ASP. NET.

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.