码迷,mamicode.com
首页 > Web开发 > 详细

Quartz.NET学习系列(八)--- 任务监听器

时间:2015-03-15 09:34:32      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:quartz.net   定时执行   任务调度   任务框架   计划   

        Quartz.NET提供了触发器监听接口、任务监听接口、计划监听接口,基本上不怎么使用,除了任务监听接口有使用场景外,其它的监听器目前还未找到使用场景。

        任务监听接口需要继承自IJobListener,应用场景可以是:执行完一个任务,自动切换执行另一个任务。

具体代码如下:

        public class SimpleJob1 : IJob
	{
		private ILog log = LogManager.GetLogger(typeof(SimpleJob1));

		public virtual void Execute(IJobExecutionContext context)
		{
			JobKey jobKey = context.JobDetail.Key;
            log.InfoFormat("{0} 执行时间 {1}", jobKey, DateTime.Now.ToString());
		}
	}


        public class SimpleJob2 : IJob
	{
		private  ILog log = LogManager.GetLogger(typeof(SimpleJob2));
		
		public virtual void Execute(IJobExecutionContext context)
		{
			JobKey jobKey = context.JobDetail.Key;
			log.InfoFormat("{0} 执行时间 {1}", jobKey, System.DateTime.Now.ToString());
		}
	}

    public class Job1Listener : IJobListener
    {
        private  ILog log = LogManager.GetLogger(typeof (Job1Listener));

        public virtual string Name
        {
            get { return "job1_to_job2"; }
        }

        public virtual void JobToBeExecuted(IJobExecutionContext inContext)
        {
            log.Info("监听器准备执行完毕");
        }

        public virtual void JobExecutionVetoed(IJobExecutionContext inContext)
        {
            log.Info("监听器拒绝");
        }

        public virtual void JobWasExecuted(IJobExecutionContext inContext, JobExecutionException inException)
        {
            log.Info("监听器执行完毕");

            IJobDetail job2 = JobBuilder.Create<SimpleJob2>()
                .WithIdentity("job2")
                .Build();

            ITrigger trigger = TriggerBuilder.Create()
                .WithIdentity("job2Trigger")
                .StartNow()
                .Build();

            try
            {
                inContext.Scheduler.ScheduleJob(job2, trigger);
            }
            catch (SchedulerException e)
            {
                log.Info("不能继续执行另一个任务job2");
                Console.Error.WriteLine(e.StackTrace);
            }
        }
    }

    public class ListenerExample 
    {
        public static void Run()
        {
            ILog log = LogManager.GetLogger(typeof (ListenerExample));

            ISchedulerFactory sf = new StdSchedulerFactory();
            IScheduler sched = sf.GetScheduler();

            IJobDetail job = JobBuilder.Create<SimpleJob1>()
                .WithIdentity("job1")
                .Build();

            ITrigger trigger = TriggerBuilder.Create()
                .WithIdentity("trigger1")
                .StartNow()
                .Build();

            IJobListener listener = new Job1Listener();
            IMatcher<JobKey> matcher = KeyMatcher<JobKey>.KeyEquals(job.Key);
            sched.ListenerManager.AddJobListener(listener, matcher);

            sched.ScheduleJob(job, trigger);
            sched.Start();

            log.Info("------- 开始计划 --------------");

            Thread.Sleep(TimeSpan.FromSeconds(30));

            sched.Shutdown(true);
            log.Info("------- 关闭计划 -----------------");

            SchedulerMetaData metaData = sched.GetMetaData();
            log.Info(string.Format("执行次数{0}", metaData.NumberOfJobsExecuted));
        }
    }

job1执行完毕会自动切换到job2。


Quartz.NET学习系列(八)--- 任务监听器

标签:quartz.net   定时执行   任务调度   任务框架   计划   

原文地址:http://blog.csdn.net/eye_cng/article/details/44263025

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!