在ASP.NET中使用Quartz.net进行工作调度
来源:互联网 发布:天刀太白帅气捏脸数据 编辑:程序博客网 时间:2024/05/15 04:53
Quartz是一个Java开源的作业调度框架。官方网站:http://www.opensymphony.com/quartz/
IBM网站上有一篇简单易懂的文章:http://www.ibm.com/developerworks/cn/java/j-quartz/
Quartz.net是从java版本移植到.net版本的。官方网站:http://quartznet.sourceforge.net/
网上找了好多教程,但没有一篇是关于如何在ASP.NET中使用的代码。既然它适用于任何的.net程序,当然也就适用于asp.net的web应用。
(1)在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>
<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>
<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>
另外我自己加了一个配置项:
<appSettings>
<add key="cronExpr" value="0 0 8-17/1 ? * 2-6"/>
</appSettings>
<add key="cronExpr" value="0 0 8-17/1 ? * 2-6"/>
</appSettings>
(2)创建一个普通类,实现Quartz.IJob接口
public class RetrieveAj2003T140Job : Quartz.IJob{
private static DataView aj2003View;
public RetrieveAj2003T140Job()
{
//
// TODO: 在此处添加构造函数逻辑
// }
public void Execute(Quartz.JobExecutionContext context)
{
//throw new Exception("The method or operation is not implemented.");
//你的处理逻辑,也就是“工作” }
}
private static DataView aj2003View;
public RetrieveAj2003T140Job()
{
//
// TODO: 在此处添加构造函数逻辑
// }
public void Execute(Quartz.JobExecutionContext context)
{
//throw new Exception("The method or operation is not implemented.");
//你的处理逻辑,也就是“工作” }
}
接口非常简单,只要在Execute()方法中进行逻辑处理就可以了。比如,读取数据库数据,或者是读取电子邮件。
(3)在Global.asax文件中启动工作调度这便于我们在web应用启动时,就启动工作调度。
<%@ Import Namespace="Quartz" %>
<script runat="server">
IScheduler sched;
void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码 ISchedulerFactory sf = new Quartz.Impl.StdSchedulerFactory();
sched = sf.GetScheduler();
JobDetail job = new JobDetail("job1", "group1", typeof(RetrieveAj2003T140Job));
string cronExpr = ConfigurationManager.AppSettings["cronExpr"];
CronTrigger trigger = new CronTrigger("trigger1", "group1", "job1", "group1",cronExpr);
sched.AddJob(job, true);
DateTime ft = sched.ScheduleJob(trigger);
sched.Start();
}
void Application_End(object sender, EventArgs e)
{
// 在应用程序关闭时运行的代码 if (sched != null)
{
sched.Shutdown(true);
}
}
</script>
<script runat="server">
IScheduler sched;
void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码 ISchedulerFactory sf = new Quartz.Impl.StdSchedulerFactory();
sched = sf.GetScheduler();
JobDetail job = new JobDetail("job1", "group1", typeof(RetrieveAj2003T140Job));
string cronExpr = ConfigurationManager.AppSettings["cronExpr"];
CronTrigger trigger = new CronTrigger("trigger1", "group1", "job1", "group1",cronExpr);
sched.AddJob(job, true);
DateTime ft = sched.ScheduleJob(trigger);
sched.Start();
}
void Application_End(object sender, EventArgs e)
{
// 在应用程序关闭时运行的代码 if (sched != null)
{
sched.Shutdown(true);
}
}
</script>
需要注意的是,当Application_End的时候,需要关闭Quartz的工作。
OK了,可以在ASP.NET中正常使用了。
- 在ASP.NET中使用Quartz.net进行工作调度
- 在ASP.NET中使用Quartz.net进行工作调度
- 在ASP.NET中使用Quartz.net进行工作调度
- 在ASP.NET中使用Quartz.net进行工作调度
- 在ASP.NET中使用Quartz.net进行工作调度
- ASP.NET使用Quartz.NET实现定时任务调度
- 在Spring中使用Quartz进行任务调度 (转:http://www.blogjava.net/Jay2009/archive/2009/03/25/259176.html)
- 在Spring中使用Quartz进行任务调度
- 在Spring中使用Quartz进行任务调度
- 在Spring中使用Quartz进行任务调度(1)
- 在Spring中使用Quartz进行任务调度(2)
- 在Spring中使用Quartz进行任务调度(3)
- 在Spring中使用Quartz进行任务调度(4)
- 在Spring中使用Quartz进行任务调度(5)
- 在Spring中使用Quartz进行任务调度(6)
- 在Spring中使用Quartz进行任务调度
- 在Spring中使用Quartz进行任务调度
- 在ASP.net中使用域账号进行登陆
- 有点时间,我想发起一个开源的项目,它不是技术的,我想不一定是技术才可以开源。好长时间了,今天有点时间写一点。
- C++工厂模式实现与改进小结
- PHP+MYSQL+APACHE配置详细解析
- Apache 配置 php 模块
- [精华] [原创]Linux+Apache+Mysql+PHP典型配置 (整理篇)
- 在ASP.NET中使用Quartz.net进行工作调度
- 算法的时间复杂度(计算实例)
- Js 调用 WebService心得总结
- Quartz CronTrigger最完整配置说明
- 正弦波调光器的工作原理
- hahaha
- 用python写一个ftp server最小实现
- Javascript调用Webservice的多种方法
- Android2.1_Launcher2学习笔记