当你在应用程序需要作业调度的时候,使用Quartz 作业调度可以解决你问题。
步骤:
1.添加2个引用文件Quartz.dll,Quartz.xml,可以在附件中下载(/Files/scottpei/Quartz.dll.7z),也可以别的地方下载。
2.做一个类来管理各个JOB的调度,假设类名为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
{
//初始化业务调度
private static readonly IScheduler scheduler;
public static readonly string MscJobGroup = "Msc Data Pull";
//初始化业务调度
static QuartzManager()
{
ISchedulerFactory schedFact = new StdSchedulerFactory();
scheduler = schedFact.GetScheduler();
scheduler.Start();
}
//某个JOB
public static void ScheduleLoadWTEIntroduceCustomerJob()
{
JobDetail jobDetail = new JobDetail("LoadWTEIntroduceCustomerJob", SchedulerConstants.DefaultGroup, typeof(LoadWTEIntroduceCustomerJob));
string timeInterval = LoadWTEIntroduceCustomerJob.GetTimeInterval();
CronTrigger cronTrigger = new CronTrigger("LoadWTEIntroduceCustomerTrigger", SchedulerConstants.DefaultGroup, jobDetail.Name, jobDetail.Group, timeInterval);
scheduler.ScheduleJob(jobDetail, cronTrigger);
}
//某个JOB
public static void ScheduleGetWTESuccessIntroduceCustomerWeeklyJob()
{
JobDetail jobDetail = new JobDetail("GetWTESuccessIntroduceCustomerWeeklyJob", SchedulerConstants.DefaultGroup, typeof(GetWTESuccessIntroduceCustomerWeeklyJob));
string timeInterval = GetWTESuccessIntroduceCustomerWeeklyJob.GetTimeInterval();
CronTrigger cronTrigger = new CronTrigger("GetWTESuccessIntroduceCustomerTrigger", SchedulerConstants.DefaultGroup, jobDetail.Name, jobDetail.Group, timeInterval);
scheduler.ScheduleJob(jobDetail, cronTrigger);
}
//重新安排某个JOB的调度
public static void ReScheduleUploadUpsEBillFileDataJob()
{
scheduler.UnscheduleJob("UploadUpsEBillFileDataTrigger", null);
ScheduleUploadUpsEBillFileDataJob();
}
//暂停调度
public static void Stop()
{
scheduler.PauseTriggerGroup(SchedulerConstants.DefaultGroup);
}
//开始调度
public static void Start()
{
scheduler.ResumeTriggerGroup(SchedulerConstants.DefaultGroup);
}
public static bool IsPaused()
{
return scheduler.IsTriggerGroupPaused(SchedulerConstants.DefaultGroup);
}
//把所有要调度的方法放在里面
public static void StartService()
{
ScheduleCronTriggerJob();
ScheduleMscDataPullJob();
ScheduleUploadUpsEBillFileDataJob();
ScheduleLoadWTEIntroduceCustomerJob();
//ScheduleGetWTESuccessIntroduceCustomerWeeklyJob();
ScheduleDataTransferWTERegNightlyJob();
ScheduleWhDailySkuCostJob();
//ScheduleClaimsJob();
}
}
3.管类里面的各个JOB类需要继承IJOB接口,且需要有Execute的方法。那上面提到的LoadWTEIntroduceCustomerJob类来举例子。
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 Month Day-of-Week Year(可选字段)
//创建一个每2秒激活一次的触发器:"0/2 * * * * ?"
//创建一个每五分钟激活一次的触发器:"0 0/5 * * * ?"
//创建一个每天18:00激活一次的触发器:"0 0 18 * * ? "
string timeInterval = "0 0 1 * * ?";
string wteFtpDailyJobTimeInterval = System.Web.Configuration.WebConfigurationManager.AppSettings["WTE_Ftp_DailyJob_TimeInterval"];
if (!string.IsNullOrEmpty(wteFtpDailyJobTimeInterval))
timeInterval = wteFtpDailyJobTimeInterval;
return timeInterval;
}
}
4.最后就是把这些放在需要执行的地方,这个一般会放在Global.asax文件中在Application_Start执行上面那个管理文件的StartService()
方法,这个指挥执行一次,里面的所有JOB以后都会按时执行。
<%@ 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>