Quartz在.Net网站中的使用方法(附Demo)
来源:互联网 发布:java复制文件并重命名 编辑:程序博客网 时间:2024/05/21 23:00
现在做一个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
- Quartz在.Net网站中的使用方法(附Demo)
- Quartz在.Net网站中的使用方法(定时任务计划)
- Spring中的Quartz使用方法简介
- log4net在asp.net中的使用方法
- Fckeditor在asp.net中的使用方法
- CKeditor在ASP.NET中的使用方法
- CKeditor在ASP.NET中的使用方法
- Quartz定时任务(附demo)(一)
- spring整合quartz定时任务(附demo)(二)
- Spring 整合 Quartz 实现动态定时任务(附demo)
- FCKeditor在.net(asp.net)中的使用方法
- FCKeditor在.net(asp.net)中的使用方法
- Cookie在网站中的两大使用方法[cookie使用]
- 【网站国际化必备】Asp.Net MVC 集成Paypal(贝宝)快速结账 支付接口 ,附源码demo
- Android中的闹钟与通知(附Demo)
- Quartz Demo
- Quartz Demo
- Quartz Demo
- 如何减少编码过程中的错误
- 如何隐藏Windows 7登录界面的用户?
- 如果你喜欢编程 给想做程序员的人的7个建议
- jQuery方法大全
- Ant实战
- Quartz在.Net网站中的使用方法(附Demo)
- RDLC的部署(无法找到Microsoft.ReportViewer.ProcessingObjectModel.dll文件)
- js时钟
- 诗仙太白
- crontab格式
- Sql 传varchar参数(逗号分隔)转成int
- hdu 1257 最少拦截系统(DP + 贪心)
- 第一章 C语言概述 (答案)
- Android自定义长按事件