您的位置:首页 > 其它

Quartz.net官方开发指南 第一课:使用Quartz.net

2008-07-16 08:39 477 查看
 
使用scheduler之前应首先实例化它。使用SchedulerFactory可以完成scheduler的实例化。用户可直接地实例化这个工厂类并且直接使用工厂的实例(例如下面的例子)。 <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

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

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


using Common.Logging;




namespace Quartz.Examples.Example1






{


    




    /**//// <summary>


    /// This is just a simple job that says "Hello" to the world.


    /// </summary>


    /// <author>Bill Kratzer</author>


    public class HelloJob : IJob




    

{


        


        private static ILog _log = LogManager.GetLogger(typeof(HelloJob));


        




        /**//// <summary> 


        /// Empty constructor for job initilization


        /// <p>


        /// Quartz requires a public empty constructor so that the


        /// scheduler can instantiate the class whenever it needs.


        /// </p>


        /// </summary>


        public HelloJob()




        

{


        }


        




        /**//// <summary> 


        /// Called by the <see cref="IScheduler" /> when a


        /// <see cref="Trigger" /> fires that is associated with


        /// the <see cref="IJob" />.


        /// </summary>


        public virtual void  Execute(JobExecutionContext context)




        

{


            


            // Say Hello to the World and display the date/time


            _log.Info(string.Format("Hello World! - {0}", System.DateTime.Now.ToString("r")));


        }




    }


}

 


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


    


            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 -------------------");


            


            // computer a time that is on the next round minute


            DateTime runTime = TriggerUtils.GetEvenMinuteDate(new NullableDateTime(DateTime.Now));


            


            // define the job and tie it to our HelloJob class


            JobDetail job = new JobDetail("job1", "group1", typeof(HelloJob));


            


            // Trigger the job to run on the next round minute


            SimpleTrigger trigger = new SimpleTrigger("trigger1", "group1", runTime);


            


            // Tell quartz to schedule the job using our trigger


            sched.ScheduleJob(job, trigger);


            log.Info(string.Format("{0} will run at: {1}", job.FullName, runTime.ToString("r")));


            


            // Start up the scheduler (nothing can actually run until the 


            // scheduler has been started)


            sched.Start();


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


            


            // wait long enough so that the scheduler as an opportunity to 


            // run the job!


            log.Info("------- Waiting 90 seconds

 -------------");




            // wait 90 seconds to show jobs


            Thread.Sleep(90 * 1000);




            // shut down the scheduler


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


            sched.Shutdown(true);


            log.Info("------- Shutdown Complete -----------------");
如您所见,使用quartz相当简单,在第二课中,我们将给出一个Job和Trigger的快速预览,这样就能够充分理解这个例子。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  quartz 任务