Quartz.net 入门 总结

1.什么是Quartz?

http://quartznet.sourceforge.net/faq.html

http://quartznet.sourceforge.net/tutorial/lesson_1.html


Quartz is a job scheduling system that can be integrated with, or used along side virtually any other software system. 

It contains Scheduler, Job and Trigger. Structure as below :


IJob Interface

namespace Quartz
{
    public interface IJob
    {
        void Execute(JobExecutionContext context);
    }
} 
    

1) the Execute(..) method is invoked by the scheduler. The JobExecutionContext object that is passed to this method provides the job instance with information about its "run-time" environment - a handle to the IScheduler that executed it, a handle to the Trigger that triggered the execution, the job's JobDetail object, and a few other items.

2)The JobDetail object is created by the Quartz.NET client (your program) at the time the Job is added to the scheduler. It contains various property settings for the Job, as well as aJobDataMap, which can be used to store state information for a given instance of your job class.

3)Trigger objects are used to trigger the execution (or 'firing') of jobs. When you wish to schedule a job, you instantiate a trigger and 'tune' its properties to provide the scheduling you wish to have. Triggers may also have a JobDataMap associated with them - this is useful to passing parameters to a Job that are specific to the firings of the trigger. Quartz.NET ships with a handful of different trigger types, but the most commonly used types are SimpleTrigger and CronTrigger

2.Quartz 入门: 

http://www.cnblogs.com/shanyou/archive/2007/09/04/881935.html


3.Quartz 应用:

http://www.chawenti.com/articles/20290.html


4. Cron  基础: 

一个cron表达式的例子字符串为"0 0 12 ? * WED",这表示“每周三的中午12:00”。

Cron表达式被用来配置CronTrigger实例。Cron表达式是一个由7个子表达式组成的字符串。每个子表达式都描述了一个单独的日程细节。这些子表达式用空格分隔,分别表示:


1. Seconds 秒


2. Minutes 分钟


3. Hours 小时


4. Day-of-Month 月中的天


5. Month 月


6. Day-of-Week 周中的天


7. Year (optional field) 年(可选的域)

单个子表达式可以包含范围或者列表。例如:前面例子中的周中的天这个域(这里是"WED")可以被替换为"MON-FRI", "MON, WED, FRI"或者甚至"MON-WED,SAT"。

通配符('*')可以被用来表示域中“每个”可能的值。因此在"Month"域中的*表示每个月,而在Day-Of-Week域中的*则表示“周中的每一天”。

所有的域中的值都有特定的合法范围,这些值的合法范围相当明显,例如:秒和分域的合法值为0到59,小时的合法范围是0到23,Day-of-Month中值得合法凡范围是0到31,但是需要注意不同的月份中的天数不同。月份的合法值是0到11。或者用字符串JAN,FEB MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV 及DEC来表示。Days-of-Week可以用1到7来表示(1=星期日)或者用字符串SUN, MON, TUE, WED, THU, FRI 和SAT来表示.


'/'字符用来表示值的增量,例如, 如果分钟域中放入'0/15',它表示“每隔15分钟,从0开始”,如果在份中域中使用'3/20',则表示“小时中每隔20分钟,从第3分钟开始”或者另外相同的形式就是'3,23,43'。


'?'字符可以用在day-of-month及day-of-week域中,它用来表示“没有指定值”。这对于需要指定一个或者两个域的值而不需要对其他域进行设置来说相当有用。


'L'字符可以在day-of-month及day-of-week中使用,这个字符是"last"的简写,但是在两个域中的意义不同。例如,在day-of-month域中的"L"表示这个月的最后一天,即,一月的31日,非闰年的二月的28日。如果它用在day-of-week中,则表示"7"或者"SAT"。但是如果在day-of-week域中,这个字符跟在别的值后面,则表示"当月的最后的周XXX"。例如:"6L" 或者 "FRIL"都表示本月的最后一个周五。当使用'L'选项时,最重要的是不要指定列表或者值范围,否则会导致混乱。


'W' 字符用来指定距离给定日最接近的周几(在day-of-week域中指定)。例如:如果你为day-of-month域指定为"15W",则表示“距离月中15号最近的周几”。

'#'表示表示月中的第几个周几。例如:day-of-week域中的"6#3" 或者 "FRI#3"表示“月中第三个周五”。


作为一个例子,下面的Quartz.NET克隆表达式将在星期一到星期五的每天上午10点15分执行一个作业。

0 15 10 ? * MON-FRI

下面的表达式

0 15 10 ? * 6L 2007-2010

将在2007年到2010年的每个月的最后一个星期五上午10点15分执行作业。你不可能用SimpleTrigger来做这些事情。你可以用两者之中的任何一个,但哪个跟合适则取决于你的调度需要。


5.可以运行的最简单的例子: 

reference :

http://miscellaneousrecipesfordotnet.blogspot.sg/2012/09/quick-sample-to-schedule-tasks-using.html

using System;
using Quartz;
using Quartz.Impl;
using Quartz.Impl.Triggers;
// Necessary references:
// Quartz dll
// Common.Logging
// System.Web
// System.Web.Services

namespace QuartzExample
{
    class Program
    {
        private static IScheduler _scheduler;

        static void Main(string[] args)
        {
            //1. construct a scheduler factory
            ISchedulerFactory schedulerFactory = new StdSchedulerFactory();

            //2. get a scheduler
            _scheduler = schedulerFactory.GetScheduler();
            _scheduler.Start();
            Console.WriteLine("Starting Scheduler");

            AddJob();

            //If any exception happens, to shut down job??20131108
            //_scheduler.Shutdown();

        }

        public static void AddJob()
        {
            //3.construct job Info
            IMyJob myJob = new MyJob(); //This Constructor needs to be parameterless
            JobDetailImpl jobDetail = new JobDetailImpl("Job1", "Group1", myJob.GetType());

            //4.Trigger Infor
            //CronTriggerImpl trigger = new CronTriggerImpl("Trigger1", "Group1", "0 * 8-17 * * ?"); //run every minute between the hours of 8am and 5pm
            CronTriggerImpl trigger = new CronTriggerImpl("Trigger1", "Group1", "0 * * * * ?"); //run every minute 
            
            //5.Add job and trigger to the Scheduler container
            _scheduler.ScheduleJob(jobDetail, trigger);

            //record DateTimeOffset when triggering the job
            //DateTimeOffset ft = _scheduler.ScheduleJob(jobDetail, trigger);

            //next trigger time 
            DateTimeOffset? nextFireTime = trigger.GetNextFireTimeUtc();
            Console.WriteLine("Next Fire Time:" + nextFireTime.Value);
        }
    }

    internal class MyJob : IMyJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Console.WriteLine("In MyJob class at time UTC Now : " +  DateTime.UtcNow);
            DoMoreWork();
        }

        public void DoMoreWork()
        {
            Console.WriteLine("Do More Work");
        }
    }

    internal interface IMyJob : IJob
    {
    }
}


public static IScheduler _scheduler { get; private set; }

...

        ISchedulerFactory schedFact = new StdSchedulerFactory();
        _scheduler = schedFact.GetScheduler();
        _scheduler.Start();

        string cron = "0 0/10 * 1/1 * ? *";

        JobKey jobkey = new JobKey("Radar", "F");
        IJobDetail job = JobBuilder.Create<RadarJob>()
                                    .WithIdentity(jobkey)
                                    .Build();

        CronScheduleBuilder csb = CronScheduleBuilder.CronSchedule(new CronExpression(cron)).InTimeZone(TimeZoneInfo.Local);
        ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create()
                                                            .WithIdentity("Radar-Trigger", "G")
                                                            .WithSchedule(csb)
                                                            .Build();
        try
        {
            DateTimeOffset ft = _scheduler.ScheduleJob(job, trigger);
            Response.Write("Job Scheduled");
        }
        catch (ObjectAlreadyExistsException)
        {
            Response.Write("Job Already Exists!");
        }

6.其他一下参考值得思考的问题:

Quartz.net simple research
  1) how to stop a job : cannot stop a job, only can achieve this the job logic
  2) what if the job failed somewhere , next round It will be triggered as normal??
  3) Jobexception : http://quartz-scheduler.org/documentation/quartz-2.x/examples/Example6
  4) Best Practice(java) : http://quartz-scheduler.org/documentation/best-practices
  5) basics : http://quartznet.sourceforge.net/tutorial/lesson_2.html 
  6) running in IIS , and job stoped : http://stackoverflow.com/questions/16755373/quartz-net-job-stops-running
  7) Quartz configuration : http://stackoverflow.com/questions/7961235/where-is-the-documentation-for-quartz-net-configuration-files
  8) Quartz structure : http://www.chawenti.com/articles/20290.html
  9) example(start and stop) : http://alexandrebrisebois.wordpress.com/2013/01/20/using-quartz-net-to-schedule-jobs-in-windows-azure-worker-roles/
  http://www.codeproject.com/Tips/399786/Quartz-Net-Custom-Base-Job
  10)ASP.NET : http://blogs.planetcloud.co.uk/mygreatdiscovery/post/ASPNET-Scheduled-Tasks-with-QuartzNET.aspx


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值