现在做一个B/S项目需要用到计划任务,本来想自定写一个的,写了几句,突然想看看网上有没有现成可以用的.
结果在苦寻之下找到了Quartz这个组件.看过之后感觉还不错.决定用它实现计划任务了.
再找找看有没有现成的任务.但找了大半天.大多数都是C/S结构中用的.
于是就把自已的写的Demo放到网上,供大家参考一下,如有不正确之 处,还请大家多多指教!
第一步:
引用三个dll文件:Nullables.dll,Quartz.dll,Common.Logging.dll
没有引用Common.Logging.dll出出错.也没多看,大家可以看一下为什么!
第二步:
配置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" />
<add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz" />
</quartz>
第三步:
在页面上新建两个按钮:
第一个按钮执行简单的计划任务
如:几秒钟执行几次
第一个按钮执行复杂的计划任务
如:每天的夜间2:00执行一次,这种复杂的任务
在简单按钮单击事件,代码如下:
ISchedulerFactory sf = new StdSchedulerFactory();
IScheduler sched = sf.GetScheduler();
JobDetail job = new JobDetail("job2", "group2", typeof(JobExecute_2));
SimpleTrigger trigger = new SimpleTrigger("trigger2", "group2");//不同的计划任务,trigger名称不可以相同!
trigger.StartTime = DateTime.Now.AddSeconds(5);
trigger.RepeatInterval = 5000;
trigger.RepeatCount = 1000;
DateTime ft = sched.ScheduleJob(job, trigger);
sched.Start();
JobExecute_2这个类就是要执行的具体任务,必继继承Ijob这个接口
代码:
public class JobExecute_2:IJob
{
#region IJob 成员
private static int n = 0;
public void Execute(JobExecutionContext context)
{
ILog log = LogManager.GetLogger(typeof(JobExecute_2));
StreamWriter w = null;
try
{
n++;
w = new StreamWriter("D:\\2.txt", true, System.Text.Encoding.UTF8);
w.WriteLine("------------------------------------");
w.WriteLine(n+" JobExecute_1正执行:时间:" + DateTime.Now);
w.WriteLine("------------------------------------");
}
finally
{
if (w != null)
{
w.Close();
w.Dispose();
}
}
}
#endregion
}
复杂任务计划如下:
按钮单击事件:
ISchedulerFactory sf = new StdSchedulerFactory();
IScheduler sched = sf.GetScheduler();
JobDetail job = new JobDetail("job1", "group1", typeof(JobExecute_1));
CronTrigger trigger = new CronTrigger("trigger1", "group1", "job1", "group1");
//二十秒执行一次
trigger.CronExpressionString = "0/20 * * * * ?";
sched.AddJob(job, true);
DateTime ft = sched.ScheduleJob(trigger);
sched.Start();
JobExecute_1类具体代码:
private static int i = 0;
public void Execute(JobExecutionContext context)
{
StreamWriter w = null;
try
{
i++;
w = new StreamWriter("D:\\1.txt", true, System.Text.Encoding.UTF8);
w.WriteLine("------------------------------------");
w.WriteLine(i+" JobExecute_1正执行:时间:" + DateTime.Now);
w.WriteLine("------------------------------------");
}
finally
{
w.Close();
w.Dispose();
}
}
好了.到这里就大功告成了!
可以运行一下试试啊!这样会定时在D盘根目录下1.txt和2.txt文件中写下日 志了!快快试试吧!
还有一些具本的使用方法及介绍可以参考:
http://www.cnblogs.com/shanyou/archive/2007/09/04/881935.html