开源作业调度框架---Quartz
来源:互联网 发布:淘宝优惠券制作教程 编辑:程序博客网 时间:2024/06/10 18:16
一个简单的例子
第一步,建立一个自己的Job
/// <summary> /// This is just a simple job that says "Hello" to the world. /// </summary> /// <author>Bill Kratzer</author> /// <author>Marko Lahma (.NET)</author> public class HelloJob : IJob { private static ILog _log = LogManager.GetLogger(typeof(HelloJob)); /// <summary> /// Empty constructor for job initialization /// <para> /// Quartz requires a public empty constructor so that the /// scheduler can instantiate the class whenever it needs. /// </para> /// </summary> public HelloJob() { } /// <summary> /// Called by the <see cref="IScheduler" /> when a /// <see cref="ITrigger" /> fires that is associated with /// the <see cref="IJob" />. /// </summary> public virtual void Execute(IJobExecutionContext context) { // Say Hello to the World and display the date/time _log.Info(string.Format("Hello World! - {0}", System.DateTime.Now.ToString("r"))); }
说明:首先我们创建一个任务/作业称为HelloJob,该任务继承与一个IJob接口,如下图所示,该接口只有一个方法
public interface IJob { /// <summary> /// Called by the <see cref="IScheduler" /> when a <see cref="ITrigger" /> /// fires that is associated with the <see cref="IJob" />. /// </summary> /// <remarks> /// 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 /// <see cref="IJobListener" />s or /// <see cref="ITriggerListener" />s that are watching the job's /// execution. /// </remarks> /// <param name="context">The execution context.</param> void Execute(IJobExecutionContext context); }
我们在HelloJob里面具体的实现了这个接口方法,输出一个Hello World!
那么如何让自己定义的Job运行起来
第二步,把自己的job注册到框架当中。
public virtual void Run() { 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 -----------"); // computer a time that is on the next round minute DateTimeOffset runTime = DateBuilder.EvenMinuteDate(DateTimeOffset.UtcNow); log.Info("------- Scheduling Job -------------------"); // define the job and tie it to our HelloJob class IJobDetail job = JobBuilder.Create<HelloJob>() .WithIdentity("job1", "group1") .Build(); // Trigger the job to run on the next round minute ITrigger trigger = TriggerBuilder.Create() .WithIdentity("trigger1", "group1") .StartAt(runTime) .Build(); // Tell quartz to schedule the job using our trigger sched.ScheduleJob(job, trigger); log.Info(string.Format("{0} will run at: {1}", job.Key, 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 65 seconds... -------------"); // wait 65 seconds to show jobs Thread.Sleep(TimeSpan.FromSeconds(65)); // shut down the scheduler log.Info("------- Shutting Down ---------------------"); sched.Shutdown(true); log.Info("------- Shutdown Complete -----------------"); }
说明:首先从作业工厂获得了一个Scheduler对象(sched),这个对象后面用于作业的具体调度。然后用了一个JobBuilder创造出了一个IJobDetail对象,看下面的代码可以知道,其实Build方法创造的是一个JobDetailImpl 对象,它继承自IJobDetail,主要是描述了Job的一些信息。比如:jobType这里就是“HelloJob”,key =
/// <summary> /// Produce the <see cref="IJobDetail" /> instance defined by this JobBuilder. /// </summary> /// <returns>the defined JobDetail.</returns> public IJobDetail Build() { JobDetailImpl job = new JobDetailImpl(); job.JobType = jobType; job.Description = description; if (key == null) { key = new JobKey(Guid.NewGuid().ToString(), null); } job.Key = key; job.Durable = durability; job.RequestsRecovery = shouldRecover; if (!jobDataMap.IsEmpty) { job.JobDataMap = jobDataMap; } return job; }
TriggerBuilder创建触发器。创建完触发器如何和我们的Job绑定呢,sched.ScheduleJob(job, trigger);这条语句完成了绑定。这样就完成了所有的工作。直接调用sched对象的start方法就会执行我们HelloJob中的Execute方法了。
阅读全文
1 0
- springMVC开源作业调度框架--quartz
- 开源作业调度框架---Quartz
- 开源的作业调度框架Quartz
- 作业调度框架--Quartz
- Quartz作业调度框架
- Quartz 作业调度框架
- Quartz作业调度框架
- 作业调度框架Quartz
- Quartz作业调度框架
- Quartz作业调度框架
- Quartz作业调度框架
- Quartz作业调度框架
- Quartz作业调度框架
- Quartz作业调度框架
- Quartz作业调度框架
- Quartz作业调度框架
- Quartz作业调度框架
- Quartz作业调度框架
- 伺服电机、舵机、步进电机的区别
- 数据库存储过程详解
- Centos中修改/etc/resolv.conf又恢复到原来的状态
- 临时表和表变量区别,SQL Server里的视图和临时表在哪里?
- 使用myeclipse新建maven web 项目
- 开源作业调度框架---Quartz
- linux程序开机自启动方法
- 目标检测RON网络VOC2007训练测试
- yum源配置
- Oracle数据库数据同步方法
- 线程进程同步(占坑)
- spring mvc redirect 重定向 跳转并传递参数
- Git学习
- LA-2512:Art Gallery(半平面交)