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

简介:
使用 scheduler 之前应首先实例化它。使用 SchedulerFactory 可以完成 scheduler 的实例化。用户可直接地实例化这个工厂类并且直接使用工厂的实例(例如下面的例子)。
一旦一个 scheduler 被实例化,它就可以被启动 (start), 并且处于驻留模式,直到被关闭 (shutdown) 。注意,一旦 scheduler 被关闭( shutdown , 则它不能再重新启动,除非重新实例化它。除非 scheduler  被启动或者不处于暂停状态,否则触发器不会被触发 ( 任务也不能被执行 )
下面是一个代码片断,这个代码片断实例化并且启动了一个 scheduler ,接着将一个要执行的任务纳入了进程。

None.gif using  Common.Logging;
None.gif
None.gif
namespace  Quartz.Examples.Example1
ExpandedBlockStart.gif
{
InBlock.gif    
ExpandedSubBlockStart.gif    
/// <summary>
InBlock.gif    
/// This is just a simple job that says "Hello" to the world.
InBlock.gif    
/// </summary>
ExpandedSubBlockEnd.gif    
/// <author>Bill Kratzer</author>

InBlock.gif    public class HelloJob : IJob
ExpandedSubBlockStart.gif    
{
InBlock.gif        
InBlock.gif        
private static ILog _log = LogManager.GetLogger(typeof(HelloJob));
InBlock.gif        
ExpandedSubBlockStart.gif        
/// <summary> 
InBlock.gif        
/// Empty constructor for job initilization
InBlock.gif        
/// <p>
InBlock.gif        
/// Quartz requires a public empty constructor so that the
InBlock.gif        
/// scheduler can instantiate the class whenever it needs.
InBlock.gif        
/// </p>
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public HelloJob()
ExpandedSubBlockStart.gif        
{
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gif        
/// <summary> 
InBlock.gif        
/// Called by the <see cref="IScheduler" /> when a
InBlock.gif        
/// <see cref="Trigger" /> fires that is associated with
InBlock.gif        
/// the <see cref="IJob" />.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public virtual void  Execute(JobExecutionContext context)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
InBlock.gif            
// Say Hello to the World and display the date/time
InBlock.gif
            _log.Info(string.Format("Hello World! - {0}", System.DateTime.Now.ToString("r")));
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

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

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





本文转自 张善友 51CTO博客,原文链接:http://blog.51cto.com/shanyou/74131,如需转载请自行联系原作者
目录
相关文章
|
Web App开发 前端开发 .NET
基于ASP.NET MVC(C#)和Quartz.Net组件实现的定时执行任务调度
在之前的文章《推荐一个简单、轻量、功能非常强大的C#/ASP.NET定时任务执行管理器组件–FluentScheduler》和《简单、轻量、功能非常强大的C#/ASP.NET定时调度任务执行管理组件–FluentScheduler之实例篇》中,我们认识和了解了FluentScheduler这款轻量的定时任务调度执行组件。
2227 0
|
5月前
|
存储 JavaScript 调度
基于.NET Core + Quartz.NET+ Vue + IView开箱即用的定时任务UI
基于.NET Core + Quartz.NET+ Vue + IView开箱即用的定时任务UI
|
消息中间件 监控 前端开发
.NET5.0和Quartz.NET开发的极简任务调度平台
一个基于.Net 5+Quartz.NET 3构建的简单、跨平台任务调度系统。系统业务简单、代码清晰,如果您只是单纯只需简单任务执行、监控、提醒这套系统就非常适合。没有多余的功能,简单扩展下基本可以满足日常需求。
133 0
.NET5.0和Quartz.NET开发的极简任务调度平台
|
存储 开发框架 NoSQL
ASP.NET Core+Quartz.Net实现web定时任务
此处我们的项目使用稍复杂的Quartz.net实现web定时任务。
ASP.NET Core+Quartz.Net实现web定时任务