asp.net任务调度之Quartz.net
如果要在asp.net中实现类似windows中计划任何的功能,你会怎么做?
您可以在留言里写出您的方法,以便我学习和改进自己的程序,谢谢。
以下是我的方法;
首先下载Quartz.net
web.config加入以下两个片段
代码
<configSections>
<!--Quartz-->
<section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
<!--End Quartz-->
</configSections>
<!--Quartz-->
<section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
<!--End Quartz-->
</configSections>
代码
<!--Start Quartz-->
<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"/>
<add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz"/>
</quartz>
<!--End Quartz-->
<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"/>
<add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz"/>
</quartz>
<!--End Quartz-->
Global.asax加入以下片段
代码
void Application_Start(object sender, EventArgs e)
{
//Sys.Log.Sys.Info("starting sched...");//日志中计入系统开始时间
// construct a scheduler factory
ISchedulerFactory schedFact = new Quartz.Impl.StdSchedulerFactory();
// get a scheduler
sched = schedFact.GetScheduler();
sched.Start();
// construct job info
JobDetail jobDetail = new JobDetail("myJob", null, typeof(Task.QuartzJob_Test));
// fire every hour
Trigger trigger = TriggerUtils.MakeSecondlyTrigger(60 * 5);
// start on the next even hour
trigger.StartTimeUtc = DateTime.UtcNow;
trigger.Name = "myTrigger";
sched.ScheduleJob(jobDetail, trigger);
//delay iisPool
Task.DelayIISThreadPool.url = HttpContext.Current.Request.Url.ToString();
JobDetail jobDetail2 = new JobDetail("myJob2", null, typeof(Task.DelayIISThreadPool));
Trigger trigger2 = TriggerUtils.MakeSecondlyTrigger(60 * 10);
trigger2.StartTimeUtc = DateTime.UtcNow;
trigger2.Name = "myTrigger2";
sched.ScheduleJob(jobDetail2, trigger2);
// End Quartz
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
//Quartz
if (sched != null)
{
sched.Shutdown(true);
}
//End Quartz
}
{
//Sys.Log.Sys.Info("starting sched...");//日志中计入系统开始时间
// construct a scheduler factory
ISchedulerFactory schedFact = new Quartz.Impl.StdSchedulerFactory();
// get a scheduler
sched = schedFact.GetScheduler();
sched.Start();
// construct job info
JobDetail jobDetail = new JobDetail("myJob", null, typeof(Task.QuartzJob_Test));
// fire every hour
Trigger trigger = TriggerUtils.MakeSecondlyTrigger(60 * 5);
// start on the next even hour
trigger.StartTimeUtc = DateTime.UtcNow;
trigger.Name = "myTrigger";
sched.ScheduleJob(jobDetail, trigger);
//delay iisPool
Task.DelayIISThreadPool.url = HttpContext.Current.Request.Url.ToString();
JobDetail jobDetail2 = new JobDetail("myJob2", null, typeof(Task.DelayIISThreadPool));
Trigger trigger2 = TriggerUtils.MakeSecondlyTrigger(60 * 10);
trigger2.StartTimeUtc = DateTime.UtcNow;
trigger2.Name = "myTrigger2";
sched.ScheduleJob(jobDetail2, trigger2);
// End Quartz
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
//Quartz
if (sched != null)
{
sched.Shutdown(true);
}
//End Quartz
}
App_Code里加入以下两个文件
代码
//QuartzJob_Test.cs
using System;
using System.Collections.Generic;
using System.Web;
namespace Task
{
/// <summary>
/// Summary description for DelayIISThreadPool
/// </summary>
public class DelayIISThreadPool : Quartz.IJob
{
#region IJob Members
public static string url;
private static System.Net.WebClient wc = new System.Net.WebClient();
public void Execute(Quartz.JobExecutionContext context)
{
if (string.IsNullOrEmpty(url))
return;
wc.DownloadString(url);
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Web;
namespace Task
{
/// <summary>
/// Summary description for DelayIISThreadPool
/// </summary>
public class DelayIISThreadPool : Quartz.IJob
{
#region IJob Members
public static string url;
private static System.Net.WebClient wc = new System.Net.WebClient();
public void Execute(Quartz.JobExecutionContext context)
{
if (string.IsNullOrEmpty(url))
return;
wc.DownloadString(url);
}
#endregion
}
}
代码
//DelayIISThreadPool.cs
using System;
using System.Collections.Generic;
using System.Web;
namespace Task
{
public class DelayIISThreadPool : Quartz.IJob
{
#region IJob Members
public static string url;
private static System.Net.WebClient wc = new System.Net.WebClient();
public void Execute(Quartz.JobExecutionContext context)
{
if (string.IsNullOrEmpty(url))
return;
wc.DownloadString(url);
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Web;
namespace Task
{
public class DelayIISThreadPool : Quartz.IJob
{
#region IJob Members
public static string url;
private static System.Net.WebClient wc = new System.Net.WebClient();
public void Execute(Quartz.JobExecutionContext context)
{
if (string.IsNullOrEmpty(url))
return;
wc.DownloadString(url);
}
#endregion
}
}