Quartz 调用

在线Cron表达式

调用(DLL下载)

 static void Main(string[] args)
        {
            //1、任务调度调用

            //开启任务,操作数据,然后将数据广播给指定用户
            //Cron表达式 :秒  分钟  小时  日的日  月  某一天的周  年
            //每分钟执行
            //string CronTime = "0/5 * * * * ? ";//每隔5秒钟执行
            string CronTime = "1 * * * * ? * ";//每隔一分钟执行一次
            //附带参数
            string jobCode = "SysJob";
            string jobCode2 = "SpeackJob";
            JobDataMap map = new JobDataMap {  };
            DateTimeOffset time = QuartzUtil.AddJob(jobCode, CronTime, map);//定时任务一
            DateTimeOffset time2 = QuartzUtil.AddJob(jobCode2, CronTime, map);//定时任务二
            Console.ReadKey();

        }
 public class SysJob : IJob
    {
        /// 
        /// 执行函数
        /// 
        /// 
        public void Execute(IJobExecutionContext context)
        {
            try
            {
                JobKey jobkey = context.JobDetail.Key;
                Console.WriteLine($"{jobkey.Name}时间{DateTime.Now}");
               
            }
            catch (System.Exception e)
            {
               
            }
        }
    }
 class SpeackJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            JobKey jobkey = context.JobDetail.Key;
            Console.WriteLine($"{jobkey.Name}时间{DateTime.Now}");
        }
    }

 

 

 

 

定时任务帮助类QuartzUtil

 /// 
    /// Quartz任务管理类
    /// 
    public static class QuartzUtil
    {
        private static ISchedulerFactory sf = null;
        private static IScheduler sched = null;

        static QuartzUtil()
        {
            sf = new StdSchedulerFactory();
            sched = sf.GetScheduler();
            sched.Start();
        }

        /// 
        /// 添加Job 并且以定点的形式运行 不带参数的cron表达式新建job
        /// 
        /// 
        /// 
        /// 
        /// 
        public static DateTimeOffset AddJob(string JobName, string CronTime) where T : IJob
        {
            return AddJob(JobName, CronTime, null);
        }

        /// 
        /// 添加Job 并且以定点的形式运行  参数为Cron表达式,可传参数。
        /// 
        /// 
        /// keyName
        /// Cron表达式
        /// 传递的参数
        /// 
        public static DateTimeOffset AddJob(string jobName, string cronTime, JobDataMap jobDataMap) where T : IJob
        {
            IJobDetail jobCheck = JobBuilder.Create().WithIdentity(jobName, jobName + "_Group").SetJobData(jobDataMap).Build();//.UsingJobData("connIds", connIds).Build();
            ICronTrigger cronTrigger = new CronTriggerImpl(jobName + "_CronTrigger", jobName + "_TriggerGroup", cronTime);

            if (sched.CheckExists(new JobKey(jobName, jobName + "_Group")))
            {
                sched.ResumeJob(new JobKey(jobName, jobName + "_Group"));
            }
            else
            {
                return sched.ScheduleJob(jobCheck, cronTrigger);
            }
            return new DateTimeOffset(DateTime.Now);
        }

        /// 
        /// 添加Job 并且以周期的形式运行
        /// 
        /// 
        /// 
        /// 毫秒数
        /// 
        public static DateTimeOffset AddJob(string JobName, int SimpleTime) where T : IJob
        {
            return AddJob(JobName, DateTime.UtcNow.AddMilliseconds(1), TimeSpan.FromMilliseconds(SimpleTime));
        }

        /// 
        /// 添加Job 并且以周期的形式运行
        /// 
        /// 
        /// 
        /// 毫秒数
        /// 
        public static DateTimeOffset AddJob(string JobName, DateTimeOffset StartTime, int SimpleTime) where T : IJob
        {
            return AddJob(JobName, StartTime, TimeSpan.FromMilliseconds(SimpleTime));
        }

        /// 
        /// 添加Job 并且以周期的形式运行
        /// 
        /// 
        /// 
        /// 
        /// 
        public static DateTimeOffset AddJob(string JobName, DateTimeOffset StartTime, TimeSpan SimpleTime) where T : IJob
        {
            return AddJob(JobName, StartTime, SimpleTime, new Dictionary<string, object>());
        }

        /// 
        /// 添加Job 并且以周期的形式运行
        /// 
        /// 
        /// 
        /// 
        /// 毫秒数
        /// 
        /// 
        public static DateTimeOffset AddJob(string JobName, DateTimeOffset StartTime, int SimpleTime, string MapKey, object MapValue) where T : IJob
        {
            Dictionary<string, object> map = new Dictionary<string, object>();
            map.Add(MapKey, MapValue);
            return AddJob(JobName, StartTime, TimeSpan.FromMilliseconds(SimpleTime), map);
        }

        /// 
        /// 添加Job 并且以周期的形式运行
        /// 
        /// 
        /// JobKey
        /// 开始时间
        /// 时间差
        /// 传递的参数
        /// 
        public static DateTimeOffset AddJob(string JobName, DateTimeOffset StartTime, TimeSpan SimpleTime, Dictionary<string, object> map) where T : IJob
        {
            IJobDetail jobCheck = JobBuilder.Create().WithIdentity(JobName, JobName + "_Group").Build();
            jobCheck.JobDataMap.PutAll(map);
            ISimpleTrigger triggerCheck = new SimpleTriggerImpl(JobName + "_SimpleTrigger", JobName + "_TriggerGroup",
                                        StartTime,
                                        null,
                                        SimpleTriggerImpl.RepeatIndefinitely,
                                        SimpleTime);
            return sched.ScheduleJob(jobCheck, triggerCheck);
        }

        /// 
        /// 修改触发器时间,需要job名,以及修改结果
        /// CronTriggerImpl类型触发器
        /// 
        public static void UpdateTime(string jobName, string CronTime)
        {
            TriggerKey TKey = new TriggerKey(jobName + "_CronTrigger", jobName + "_TriggerGroup");
            CronTriggerImpl cti = sched.GetTrigger(TKey) as CronTriggerImpl;
            cti.CronExpression = new CronExpression(CronTime);
            sched.RescheduleJob(TKey, cti);
        }

        /// 
        /// 修改触发器时间,需要job名,以及修改结果
        /// SimpleTriggerImpl类型触发器
        /// 
        /// 
        /// 分钟数
        public static void UpdateTime(string jobName, int SimpleTime)
        {
            UpdateTime(jobName, TimeSpan.FromMinutes(SimpleTime));
        }

        /// 
        /// 修改触发器时间,需要job名,以及修改结果
        /// SimpleTriggerImpl类型触发器
        /// 
        public static void UpdateTime(string jobName, TimeSpan SimpleTime)
        {
            TriggerKey TKey = new TriggerKey(jobName + "_SimpleTrigger", jobName + "_TriggerGroup");
            SimpleTriggerImpl sti = sched.GetTrigger(TKey) as SimpleTriggerImpl;
            sti.RepeatInterval = SimpleTime;
            sched.RescheduleJob(TKey, sti);
        }

        /// 
        /// 暂停所有Job
        /// 
        public static void PauseAll()
        {
            sched.PauseAll();
        }

        /// 
        /// 恢复所有Job
        /// 
        public static void ResumeAll()
        {
            sched.ResumeAll();
        }

        /// 
        /// 暂停某个任务
        /// 
        /// 
        public static void PauseJob(string JobName)
        {
            JobKey jk = new JobKey(JobName);
            sched.PauseJob(jk);
        }

        /// 
        /// 恢复指定的Job
        /// 
        /// 
        public static void ResumeJob(string JobName)
        {
            JobKey jk = new JobKey(JobName);
            sched.ResumeJob(jk);
        }

        /// 
        /// 删除Job
        /// 
        /// 
        public static void DeleteJob(string JobName)
        {
            JobKey jk = new JobKey(JobName);
            sched.DeleteJob(jk);
        }

        /// 
        /// 卸载定时器
        /// 
        /// 是否等待job执行完成
        public static void Shutdown(bool waitForJobsToComplete)
        {
            sched.Shutdown(waitForJobsToComplete);
        }
    }

 

转载于:https://www.cnblogs.com/macT/p/11604208.html

你可能感兴趣的:(Quartz 调用)