Quartz Job Scheduling

Source: Internet
Author: User
Tags log4net

When your application requires job scheduling, you can use quartz job scheduling to solve your problem.

Steps:

1. Add two reference files quartz. dll and quartz. XML, which can be downloaded from the attachment (/files/scottpei/quartz.dll.7z) or elsewhere.

2. Create a class to manage the scheduling of each job. Assume that the class is named quartzmanager.

Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using app_code;
Using Quartz;
Using Quartz. impl;

/// <Summary>
/// Summary description for quartzmanager
/// </Summary>
Public class quartzmanager
{

// Initialize business Scheduling
Private Static readonly ischeduler scheduler;
Public static readonly string mscjobgroup = "MSC data pull ";

// Initialize business Scheduling

Static quartzmanager ()
{
Ischedulerfactory schedfact = new stdschedulerfactory ();
Schedt = schedfact. getscheduler ();
Scheduler. Start ();
}

// A job

Public static void scheduleloadwteintroducecustomerjob ()
{
Jobdetail = new jobdetail ("loadwteintroducecustomerjob", schedulerconstants. defaultgroup, typeof (loadwteintroducecustomerjob ));
String timeinterval = loadwteintroducecustomerjob. gettimeinterval ();
Crontrigger = new crontrigger ("loadwteintroducecustomertrigger", schedulerconstants. defaultgroup, jobdetail. Name, jobdetail. Group, timeinterval );
Scheduler. schedulejob (jobdetail, crontrigger );
}

 

// A job

Public static void schedulegetwtesuccessintroducecustomerweeklyjob ()
{
Jobdetail = new jobdetail ("getwtesuccessintroducecustomerweeklyjob", schedulerconstants. defaultgroup, typeof (bytes ));
String timeinterval = getwtesuccessintroducecustomerweeklyjob. gettimeinterval ();
Crontrigger = new crontrigger ("getwtesuccessintroducecustomertrigger", schedulerconstants. defaultgroup, jobdetail. Name, jobdetail. Group, timeinterval );

Scheduler. schedulejob (jobdetail, crontrigger );
}

 

// Reschedule a job

Public static void rescheduleuploadupsebillfiledatajob ()
{
Schedbill. unschedulejob ("uploadupsebillfiledatatrigger", null );
Scheduleuploadupsebillfiledatajob ();
}

// Pause Scheduling

Public static void stop ()
{
Schedger. pausetriggergroup (schedulerconstants. defaultgroup );
}

 

// Start scheduling

Public static void start ()
{
Schedger. resumetriggergroup (schedulerconstants. defaultgroup );
}

Public static bool ispaused ()
{
Return scheduler. istriggergrouppaused (schedulerconstants. defaultgroup );
}

 

// Put all the methods to be scheduled in it

Public static void startservice ()
{
Schedulecrontriggerjob ();
Schedulemscdatapulljob ();
Scheduleuploadupsebillfiledatajob ();
Scheduleloadwteintroducecustomerjob ();
// Schedulegetwtesuccessintroducecustomerweeklyjob ();
Scheduledatatransferwteregnightlyjob ();
Schedulewhdailyskucostjob ();
// Scheduleclaimsjob ();
}
}

3. Each job class in the management class must inherit the ijob interface and have the execute method. The loadwteintroducecustomerjob class mentioned above is used as an example.

 

Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using Quartz;

/// <Summary>
/// Summary description for loadwteintroducecustomerjob
/// </Summary>
Public class loadwteintroducecustomerjob: ijob
{
Public loadwteintroducecustomerjob ()
{
//
// Todo: Add constructor logic here
//
}

Public void execute (jobexecutioncontext context)
{
Wteintroducecustomer automatedloadwteintroducecustomer = new wteintroducecustomer ();
Automatedloadwteintroducecustomer. addintroducecustomerfromxml ();
}

Public static string gettimeinterval ()
{
// Seconds minutes hours day-of-month Day-of-week year (optional)
// Create a trigger that is activated every 2 seconds: "0/2 ****? "
// Create a trigger that is activated every five minutes: "0 0/5 ***? "
// Create a trigger that is activated once every day at: "0 0 18 **? "

String timeinterval = "0 0 1 **? ";
String wteftpdailyjobtimeinterval = system. Web. configuration. webconfigurationmanager. receivettings ["wte_ftp_dailyjob_timeinterval"];
If (! String. isnullorempty (wteftpdailyjobtimeinterval ))
Timeinterval = wteftpdailyjobtimeinterval;
Return timeinterval;
}
}
4. Finally, place these in the place where you want to execute them. This will usually be placed in the global. asax file and the startservice () of the management File above will be executed in application_start ()
This command is executed once, and all the jobs in it will be executed on time later.

 

<% @ Application language = "C #" %>
<% @ Import namespace = "system. Io" %>

<SCRIPT runat = "server">
Void application_start (Object sender, eventargs E)
{
// Code that runs on application startup
Log4net. config. xmlconfigurator. Configure (New fileinfo (server. mappath ("~ /Log4net. xml ")));

Quartzmanager. startservice ();

}

Void application_end (Object sender, eventargs E)
{
Quartzmanager. Stop ();
}

Void application_error (Object sender, eventargs E)
{
// Code that runs when an unhandled error occurs

}

Void session_start (Object sender, eventargs E)
{
// Code that runs when a new session is started

}

Void session_end (Object sender, eventargs E)
{
// Code that runs when a session ends.
// Note: The session_end event is raised only when the sessionstate Mode
// Is set to inproc in the web. config file. If session mode is set to StateServer
// Or sqlserver, the event is not raised.

}

</SCRIPT>

 

 

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.