Quartz.net官方开发指南 第二课:Jobs And Triggers

正如前面所提到的那样,通过实现IJob接口来使你的.NET组件可以很简单地被scheduler执行。下面是IJob接口:

namespace  Quartz

{

     
///  

     
/// The interface to be implemented by classes which represent a 'job' to be

     
/// performed.

     
/// 


     
/// 

     
/// Instances of this interface must have a 

     
/// no-argument constructor.  provides a mechanism for 'instance member data'

     
/// that may be required by some implementations of this interface.

    
/// 

     
/// 

     
/// 

     
/// 

     
/// 

     
/// James House

     
/// Marko Lahma (.NET)


     
public interface IJob

     
{

         
/// 

         
/// Called by the  when a 

         
/// fires that is associated with the .

        
/// 


         
/// 

         
/// The implementation may wish to set a  result object on the 

         
/// JobExecutionContext before this method exits.  The result itself

         
/// is meaningless to Quartz, but may be informative to 

         
/// s or 

         
/// s that are watching the job's 

         
/// execution.

         
/// 

         
/// The execution context.


         
void Execute(JobExecutionContext context);

     }


}


             ILog log  =  LogManager.GetLogger( typeof  (SimpleTriggerExample));

            log.Info(
" ------- Initializing ------------------- " );

            
//  First we must get a reference to a scheduler
            ISchedulerFactory sf  =   new  StdSchedulerFactory();
            IScheduler sched 
=  sf.GetScheduler();

            log.Info(
" ------- Initialization Complete -------- " );

            log.Info(
" ------- Scheduling Jobs ---------------- " );

            
//  jobs can be scheduled before sched.start() has been called

            
//  get a "nice round" time a few seconds in the future
            DateTime ts  =  TriggerUtils.GetNextGivenSecondDate( null 15 );

            
//  job1 will only fire once at date/time "ts"
            JobDetail job  =   new  JobDetail( " job1 " " group1 " typeof  (SimpleJob));
            SimpleTrigger trigger 
=   new  SimpleTrigger( " trigger1 " " group1 " , ts);

            
//  schedule it to run!
            DateTime ft  =  sched.ScheduleJob(job, trigger);
            log.Info(
string .Format( " {0} will run at: {1} and repeat: {2} times, every {3} seconds "
                job.FullName, ft.ToString(
" r " ), trigger.RepeatCount, (trigger.RepeatInterval / 1000 )));

            
//  job2 will only fire once at date/time "ts"
            job  =   new  JobDetail( " job2 " " group1 " typeof  (SimpleJob));
            trigger 
=   new  SimpleTrigger( " trigger2 " " group1 " " job2 " " group1 " , ts,  null 0 0 );
            ft 
=  sched.ScheduleJob((job), trigger);
            log.Info(
string .Format( " {0} will run at: {1} and repeat: {2} times, every {3} seconds "
                job.FullName, ft.ToString(
" r " ), trigger.RepeatCount, (trigger.RepeatInterval / 1000 )));

            
//  job3 will run 11 times (run once and repeat 10 more times)
            
//  job3 will repeat every 10 seconds (10000 ms)
            job  =   new  JobDetail( " job3 " " group1 " typeof  (SimpleJob));
            trigger 
=   new  SimpleTrigger( " trigger3 " " group1 " " job3 " " group1 " , ts,  null 10 10000L );
            ft 
=  sched.ScheduleJob(job, trigger);
            log.Info(
string .Format( " {0} will run at: {1} and repeat: {2} times, every {3} seconds "
                job.FullName, ft.ToString(
" r " ), trigger.RepeatCount, (trigger.RepeatInterval / 1000 )));

            
//  the same job (job3) will be scheduled by a another trigger
            
//  this time will only run every 70 seocnds (70000 ms)
            trigger  =   new  SimpleTrigger( " trigger3 " " group2 " " job3 " " group1 " , ts,  null 2 70000L );
            ft 
=  sched.ScheduleJob(trigger);
            log.Info(
string .Format( " {0} will [also] run at: {1} and repeat: {2} times, every {3} seconds "
                job.FullName, ft.ToString(
" r " ), trigger.RepeatCount, (trigger.RepeatInterval / 1000 )));

            
//  job4 will run 6 times (run once and repeat 5 more times)
            
//  job4 will repeat every 10 seconds (10000 ms)
            job  =   new  JobDetail( " job4 " " group1 " typeof  (SimpleJob));
            trigger 
=   new  SimpleTrigger( " trigger4 " " group1 " " job4 " " group1 " , ts,  null 5 10000L );
            ft 
=  sched.ScheduleJob(job, trigger);
            log.Info(
string .Format( " {0} will run at: {1} and repeat: {2} times, every {3} seconds "
                job.FullName, ft.ToString(
" r " ), trigger.RepeatCount, (trigger.RepeatInterval / 1000 )));

            
//  job5 will run once, five minutes past "ts" (300 seconds past "ts")
            job  =   new  JobDetail( " job5 " " group1 " typeof  (SimpleJob));
            trigger 
=   new  SimpleTrigger( " trigger5 " " group1 " " job5 " " group1 " , ts.AddMilliseconds( 300 * 1000 ),  null 0 0 );
            ft 
=  sched.ScheduleJob(job, trigger);
            log.Info(
string .Format( " {0} will run at: {1} and repeat: {2} times, every {3} seconds "
                job.FullName, ft.ToString(
" r " ), trigger.RepeatCount, (trigger.RepeatInterval / 1000 )));

            
//  job6 will run indefinitely, every 50 seconds
            job  =   new  JobDetail( " job6 " " group1 " typeof  (SimpleJob));
            trigger 
=
                
new  SimpleTrigger( " trigger6 " " group1 " " job6 " " group1 " , ts,  null , SimpleTrigger.REPEAT_INDEFINITELY,
                                  
50000L );
            ft 
=  sched.ScheduleJob(job, trigger);
            log.Info(
string .Format( " {0} will run at: {1} and repeat: {2} times, every {3} seconds "
                job.FullName, ft.ToString(
" r " ), trigger.RepeatCount, (trigger.RepeatInterval / 1000 )));

            log.Info(
" ------- Starting Scheduler ---------------- " );

            
//  All of the jobs have been added to the scheduler, but none of the jobs
            
//  will run until the scheduler has been started
            sched.Start();

            log.Info(
" ------- Started Scheduler ----------------- " );

            
//  jobs can also be scheduled after start() has been called
            
//  job7 will repeat 20 times, repeat every five minutes
            job  =   new  JobDetail( " job7 " " group1 " typeof  (SimpleJob));
            trigger 
=   new  SimpleTrigger( " trigger7 " " group1 " " job7 " " group1 " , ts,  null 20 300000 );
            ft 
=  sched.ScheduleJob(job, trigger);
            log.Info(
string .Format( " {0} will run at: {1} and repeat: {2} times, every {3} seconds "
                job.FullName, ft.ToString(
" r " ), trigger.RepeatCount, (trigger.RepeatInterval / 1000 )));

            
//  jobs can be fired directly (rather than waiting for a trigger)
            job  =   new  JobDetail( " job8 " " group1 " typeof  (SimpleJob));
            job.Durable 
=  ( true );
            sched.AddJob(job, 
true );
            log.Info(
" 'Manually' triggering job8 " );
            sched.TriggerJob(
" job8 " " group1 " );

            log.Info(
" ------- Waiting 30 seconds -------------- " );

            
try
            
{
                
// wait 30 seconds to show jobs
                Thread.Sleep(30*1000);
                
// executing
            }

            
catch  (ThreadInterruptedException)
            
{
            }


            
//  jobs can be re-scheduled  
            
//  job 7 will run immediately and repeat 10 times for every second
            log.Info( " ------- Rescheduling -------------------- " );
            trigger 
=   new  SimpleTrigger( " trigger7 " " group1 " " job7 " " group1 " , DateTime.Now,  null 10 1000L );
            NullableDateTime ft2 
=  sched.RescheduleJob( " trigger7 " " group1 " , trigger);
            
if  (ft2.HasValue)
            
{
                log.Info(
"job7 rescheduled to run at: " + ft2.Value.ToString("r"));
            }

            
else
            
{
                log.Error(
"Reschedule failed, date was null");
            }


            log.Info(
" ------- Waiting five minutes ------------ " );
            
try
            
{
                
// wait five minutes to show jobs
                Thread.Sleep(2*1000);
                
// executing
            }

            
catch  (ThreadInterruptedException)
            
{
            }


            log.Info(
" ------- Shutting Down --------------------- " );

            sched.Shutdown(
true );

            log.Info(
" ------- Shutdown Complete ----------------- " );

            
//  display some stats about the schedule that just ran
            SchedulerMetaData metaData  =  sched.GetMetaData();
            log.Info(
string .Format( " Executed {0} jobs. " , metaData.NumJobsExecuted));

    这样,你会猜想出,当Job触发器触发时(在某个时刻),Execute (..)就被scheduler所调用。JobExecutionContext对象被传递给这个方法,它为Job实例提供了它的运行时环境-一个指向执行这个IJob实例的Scheduler句柄,一个指向触发该次执行的触发器的句柄,IJobJobDetail对象以及一些其他的条目。

JobDetail对象由Quartz客户端在Job被加入到scheduler时创建。它包含了Job的各种设置属性以及一个JobDataMap对象,这个对象被用来存储给定Job类实例的状态信息。

      Trigger对象被用来触发jobs的执行。你希望将任务纳入到进度,要实例化一个Trigger并且调整它的属性以满足你想要的进度安排。Triggers也有一个JobDataMap与之关联,这非常有利于向触发器所触发的Job传递参数。Quartz打包了很多不同类型的Trigger,但最常用的Trigge类是SimpleTriggerCronTrigger

SimpleTrigger用来触发只需执行一次或者在给定时间触发并且重复N次且每次执行延迟一定时间的任务。CronTrigger按照日历触发,例如每个周五,每个月10日中午或者1015分。

为什么要分为JobsTriggers?很多任务日程管理器没有将JobsTriggers进行区分。一些产品中只是将job简单地定义为一个带有一些小任务标识的执行时间。其他产品则更像Quartzjobtrigger的联合。而开发Quartz的时候,我们决定对日程和按照日程执行的工作进行分离。(从我们的观点来看)这有很多好处。

例如:jobs可以被创建并且存储在job scheduler中,而不依赖于trigger,而且,很多triggers可以关联一个job.另外的好处就是这种松耦合能使与日程中的Job相关的trigger过期后重新配置这些Job,这样以后就能够重新将这些Job纳入日程而不必重新定义它们。这样就可以更改或者替换trigger而不必重新定义与之相连的job标识符。

当向Quartz scheduler中注册Jobs Triggers时,它们要给出标识它们的名字。Jobs Triggers也可以被放入中。对于后续维护过程中,分类管理JobsTriggers非常有用。JobsTriggers的名字在组中必须唯一,换句话说,JobsTriggers真实名字是它的名字+组。如果使Job或者Trigger的组为null,这等价于将其放入缺省的Scheduler.DEFAULT_GROUP组中。

现在对什么是 Jobs Triggers 有了一般性的认识,可以通过第三课 :更多关于JobsJobDetails 的内容及第四课 :关于Triggers 更多的内容来深入地学习它们。

你可能感兴趣的:(Quartz.net,triggers,jobs,quartz,null,任务,.net)