使用scheduler之前应首先实例化它。使用SchedulerFactory可以完成scheduler的实例化。用户可直接地实例化这个工厂类并且直接使用工厂的实例(例如下面的例子)。

一旦一个scheduler被实例化,它就可以被启动(start),并且处于驻留模式,直到被关闭(shutdown)。注意,一旦scheduler被关闭(shutdown,则它不能再重新启动,除非重新实例化它。除非scheduler 被启动或者不处于暂停状态,否则触发器不会被触发(任务也不能被执行)

下面是一个代码片断,这个代码片断实例化并且启动了一个scheduler,接着将一个要执行的任务纳入了进程。

Quartz.net官方开发指南  第一课:使用Quartz.netusing Common.Logging;
Quartz.net官方开发指南  第一课:使用Quartz.net
Quartz.net官方开发指南  第一课:使用Quartz.net
namespace Quartz.Examples.Example1
}


 

Quartz.net官方开发指南  第一课:使用Quartz.net            ILog log = LogManager.GetLogger(typeof(SimpleExample));
Quartz.net官方开发指南  第一课:使用Quartz.net    
Quartz.net官方开发指南  第一课:使用Quartz.net            log.Info(
"------- Initializing ----------------------");
Quartz.net官方开发指南  第一课:使用Quartz.net            
Quartz.net官方开发指南  第一课:使用Quartz.net            
// First we must get a reference to a scheduler
Quartz.net官方开发指南  第一课:使用Quartz.net
            ISchedulerFactory sf = new StdSchedulerFactory();
Quartz.net官方开发指南  第一课:使用Quartz.net            IScheduler sched 
= sf.GetScheduler();
Quartz.net官方开发指南  第一课:使用Quartz.net            
Quartz.net官方开发指南  第一课:使用Quartz.net            log.Info(
"------- Initialization Complete -----------");
Quartz.net官方开发指南  第一课:使用Quartz.net            
Quartz.net官方开发指南  第一课:使用Quartz.net            log.Info(
"------- Scheduling Jobs -------------------");
Quartz.net官方开发指南  第一课:使用Quartz.net            
Quartz.net官方开发指南  第一课:使用Quartz.net            
// computer a time that is on the next round minute
Quartz.net官方开发指南  第一课:使用Quartz.net
            DateTime runTime = TriggerUtils.GetEvenMinuteDate(new NullableDateTime(DateTime.Now));
Quartz.net官方开发指南  第一课:使用Quartz.net            
Quartz.net官方开发指南  第一课:使用Quartz.net            
// define the job and tie it to our HelloJob class
Quartz.net官方开发指南  第一课:使用Quartz.net
            JobDetail job = new JobDetail("job1""group1"typeof(HelloJob));
Quartz.net官方开发指南  第一课:使用Quartz.net            
Quartz.net官方开发指南  第一课:使用Quartz.net            
// Trigger the job to run on the next round minute
Quartz.net官方开发指南  第一课:使用Quartz.net
            SimpleTrigger trigger = new SimpleTrigger("trigger1""group1", runTime);
Quartz.net官方开发指南  第一课:使用Quartz.net            
Quartz.net官方开发指南  第一课:使用Quartz.net            
// Tell quartz to schedule the job using our trigger
Quartz.net官方开发指南  第一课:使用Quartz.net
            sched.ScheduleJob(job, trigger);
Quartz.net官方开发指南  第一课:使用Quartz.net            log.Info(
string.Format("{0} will run at: {1}", job.FullName, runTime.ToString("r")));
Quartz.net官方开发指南  第一课:使用Quartz.net            
Quartz.net官方开发指南  第一课:使用Quartz.net            
// Start up the scheduler (nothing can actually run until the 
Quartz.net官方开发指南  第一课:使用Quartz.net            
// scheduler has been started)
Quartz.net官方开发指南  第一课:使用Quartz.net
            sched.Start();
Quartz.net官方开发指南  第一课:使用Quartz.net            log.Info(
"------- Started Scheduler -----------------");
Quartz.net官方开发指南  第一课:使用Quartz.net            
Quartz.net官方开发指南  第一课:使用Quartz.net            
// wait long enough so that the scheduler as an opportunity to 
Quartz.net官方开发指南  第一课:使用Quartz.net            
// run the job!
Quartz.net官方开发指南  第一课:使用Quartz.net
            log.Info("------- Waiting 90 secondsQuartz.net官方开发指南  第一课:使用Quartz.net -------------");
Quartz.net官方开发指南  第一课:使用Quartz.net
Quartz.net官方开发指南  第一课:使用Quartz.net            
// wait 90 seconds to show jobs
Quartz.net官方开发指南  第一课:使用Quartz.net
            Thread.Sleep(90 * 1000);
Quartz.net官方开发指南  第一课:使用Quartz.net
Quartz.net官方开发指南  第一课:使用Quartz.net            
// shut down the scheduler
Quartz.net官方开发指南  第一课:使用Quartz.net
            log.Info("------- Shutting Down ---------------------");
Quartz.net官方开发指南  第一课:使用Quartz.net            sched.Shutdown(
true);
Quartz.net官方开发指南  第一课:使用Quartz.net            log.Info(
"------- Shutdown Complete -----------------");

如您所见,使用quartz相当简单,在第二课中,我们将给出一个JobTrigger的快速预览,这样就能够充分理解这个例子。

相关文章:

  • 2021-11-30
  • 2021-07-01
  • 2021-08-24
  • 2021-08-03
  • 2021-06-17
  • 2021-06-04
  • 2021-12-22
  • 2021-08-26
猜你喜欢
  • 2021-09-24
  • 2022-01-04
  • 2021-11-10
  • 2021-07-28
  • 2021-11-08
  • 2021-07-18
  • 2021-07-14
相关资源
相似解决方案