Quartz.NET的应用

◇◆丶佛笑我妖孽 提交于 2020-03-22 11:44:42
Quartz.NET 是一个开源的调度框架,什么是调度,有什么用,简单的说,如果你有这样的需求,你就可以用它来帮你。如一个在两个系统中同步数据的程序,需要在每天晚上运行。也就是说它能在一个相对固定的时间重复做同样一件事情。当然做什么事需要你自己来写了。以前写这样的程序多半是用一个控制台程序,然后加到windows的任务列表中。
看看QUarz.NET官方(http://quartznet.sourceforge.net/)的描述吧
Quartz.NET is a full-featured, open source job scheduling system that can be used from smallest apps to large scale enterprise systems.
Quartz.NET is a port of very propular open source Java job scheduling framework, Quartz . This project owes very much to original Java project, it's father James House and the project contributors.
又是一个从java移植过来的好东西,呵呵!不得不感叹java的发展速度和其所拥有的用户群。

Quartz.net的使用还是比较简单的,我用三步曲来描述
一步配置
<?xml version="1.0" encoding="UTF-8"?>
<quartz xmlns="http://quartznet.sourceforge.net/JobSchedulingData"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    version="1.0"
    overwrite-existing-jobs="true">
  <job>
  <job-detail>
   <name>mytask</name>
   <group>tasks</group>
   <job-type>SchedulerLibrary.MyTask, SchedulerLibrary</job-type>
  </job-detail>  
   <trigger>
    <cron>
     <name>CronTrigger</name>
     <group>TriggerGroup</group>
     <job-name>mytask</job-name>
     <job-group>tasks</job-group>
     <start-time>2008-02-24T05:00:01</start-time>
     <cron-expression>0 0 5 * * ?</cron-expression>
    </cron>
   </trigger>
 </job>
</quartz>

二步初始化IScheduler

public partial class MyService : ServiceBase
    {
        private ISchedulerFactory schedulerFactory;
        private JobSchedulingDataProcessor processor;
        private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(MyService));

        public MyService()
        {
            InitializeComponent();

            schedulerFactory = new StdSchedulerFactory();
            processor = new JobSchedulingDataProcessor(true, true);
        }

        protected override void OnStart(string[] args)
        {
            try
            {
                IScheduler scheduler = schedulerFactory.GetScheduler();
                string fileName = AppDomain.CurrentDomain.BaseDirectory + System.Configuration.ConfigurationManager.AppSettings["JobConfigFile"];
                processor.ProcessFile(fileName, null);
                processor.ScheduleJobs(new Hashtable(), scheduler, false);

                scheduler.Start();

                logger.Info("service started ok");
            }
            catch (Exception ex)
            {
                logger.Error("service started fail", ex);
            }
        }

        protected override void OnStop()
        {
            try
            {
                IScheduler scheduler = schedulerFactory.GetScheduler();
                scheduler.Shutdown(true);

                logger.Info("service stopped ok");
            }
            catch (Exception ex)
            {
                logger.Error("service stopped fail", ex);
            }
        }

三步实现任务

public class MyTask : IJob
    {
        #region IJob Members

        public void Execute(JobExecutionContext context)
        {

                //todo
        }

        #endregion
    }

其实只要实现iJob接口即可。

更多的关于Quartz.net的资料请看
http://www.openbeta.cn/quartznet.ashx
http://www.cnblogs.com/shanyou/category/102991.html
在这里你可以得到最新的进展和源码
http://quartznet.sourceforge.net/

上面代码片段的完整示例,请在这里下载



shore 2008-02-27 18:23 发表评论
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!