Quartz.net-控制檯例項-Program

草青工作室發表於2016-12-22

Quartz.net-控制檯例項-Program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Quartz;
using Quartz.Impl;

namespace QuartzConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            // Grab the Scheduler instance from the Factory 
            IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
            // and start it off
            scheduler.Start();

            //使用簡單觸發器
            SimpleTriggers(scheduler);
            //使用cron表示式觸發器
            CronTriggers(scheduler);

            Console.WriteLine("排程已經構建完成,按任意鍵退出應用程式...");
            Console.ReadKey();
            scheduler.Shutdown();
        }
        /// <summary>
        /// 使用簡單觸發器
        /// </summary>
        static void SimpleTriggers(IScheduler scheduler)
        {
            try
            {
                // define the job and tie it to our HelloJob class
                IJobDetail job = JobBuilder.Create<HelloJob>()
                    .WithIdentity("job1", "group1")
                    .Build();
                Console.WriteLine("構建簡單觸發器.開始..");
                // Trigger the job to run now, and then repeat every 10 seconds
                ITrigger trigger = TriggerBuilder.Create()
                    .WithIdentity("trigger1", "group1")
                    .UsingJobData("note", "簡單觸發器") //向觸發器資料字典中新增資料 key 不能重複
                    //.StartNow() //馬上執行
                    .StartAt(DateBuilder.FutureDate(2, IntervalUnit.Second)) //等待2秒後執行
                    .WithSimpleSchedule(x => x
                        .WithIntervalInSeconds(2)//每次間隔1秒
                        //.WithRepeatCount(5) //重複5次
                        .RepeatForever() //永遠重複
                        )
                    .Build();
                // Tell quartz to schedule the job using our trigger
                scheduler.ScheduleJob(job, trigger);
            }
            catch (SchedulerException se)
            {
                Console.WriteLine(se);
                Console.ReadKey();
            }
        }
        /// <summary>
        /// 使用Cron表示式觸發器
        /// </summary>
        static void CronTriggers(IScheduler scheduler)
        {
            //Seconds(秒)        
            //Minutes(分)         
            //Hours(時)           
            //Day-of-Month(天)
            //Month(月)           
            //Day-of-Week(每週)
            //年份(1970-2099) 
            try
            {
                // define the job and tie it to our HelloJob class
                IJobDetail job = JobBuilder.Create<HelloJob>()
                    //Use a JobKey with the given name and group to identify the JobDetail.
                    .WithIdentity("job2", "group2")
                    .Build();
                Console.WriteLine("構建簡單觸發器.開始..");
                // Trigger the job to run now, and then repeat every 10 seconds
                ITrigger trigger = TriggerBuilder.Create()
                    //Use a TriggerKey with the given name and group to identify the Trigger.
                    .WithIdentity("trigger2", "group2")
                    .UsingJobData("note", "cron 觸發器")
                    //
                    .WithCronSchedule("1/1 * * ? * *") //cron 表示式
                    //Set the identity of the Job which should be fired by the produced Trigger - a JobKey will be produced with the given name and group.
                    .ForJob("job2", "group2")
                    .Build();
                Console.WriteLine("構建簡單觸發器,完成...");
                // Tell quartz to schedule the job using our trigger
                scheduler.ScheduleJob(job, trigger);


            }
            catch (SchedulerException se)
            {
                Console.WriteLine(se);
                Console.ReadKey();
            }
        }
    }


    public class HelloJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            var dataMap = context.MergedJobDataMap;
            JobKey key = context.JobDetail.Key;
            string note = dataMap.GetString("note");
            Console.WriteLine("名稱:{0};備註:{1}。",key,note);
            //Console.WriteLine("Greetings from HelloJob!");
        }
    }
}


相關文章