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。


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。