Ejemplo n.º 1
0
        /// <summary>
        /// 调度任务
        /// </summary>
        /// <typeparam name="T">任务类型</typeparam>
        /// <param name="delay">延迟时间(毫秒)</param>
        /// <remarks>适用于没有参数,只有策略的任务</remarks>
        public static void Schedule <T>(int delay = 1000) where T : class, ICrontab
        {
            //调度任务
            Type crontabType = typeof(T);

            #region # 验证

            if (!CrontabSetting.CrontabStrategies.ContainsKey(crontabType.Name))
            {
                throw new InvalidOperationException($"没有为定时任务\"{crontabType.Name}\"配置执行策略");
            }

            #endregion

            ExecutionStrategy executionStrategy = CrontabSetting.CrontabStrategies[crontabType.Name];
            ICrontab          crontab           = CrontabFactory.CreateCrontab(crontabType, executionStrategy);

            IEnumerable <ICrontabExecutor> crontabExecutors = CrontabExecutorFactory.GetCrontabExecutorsFor(crontabType);
            foreach (ICrontabExecutor crontabExecutor in crontabExecutors)
            {
                JobKey jobKey = new JobKey(crontab.Id);
                if (!_Scheduler.CheckExists(jobKey).Result)
                {
                    Type       jobType    = crontabExecutor.GetType();
                    JobBuilder jobBuilder = JobBuilder.Create(jobType);

                    //设置任务数据
                    IDictionary <string, object> dictionary = new Dictionary <string, object>();
                    dictionary.Add(crontab.Id, crontab);
                    jobBuilder.SetJobData(new JobDataMap(dictionary));

                    //创建任务明细
                    IJobDetail jobDetail = jobBuilder.WithIdentity(jobKey).Build();

                    //创建触发器
                    ITrigger trigger = GetTrigger(crontab.ExecutionStrategy);

                    //为调度者添加任务明细与触发器
                    _Scheduler.ScheduleJob(jobDetail, trigger).Wait();

                    //开始调度
                    _Scheduler.StartDelayed(TimeSpan.FromMilliseconds(delay)).Wait();
                }
                else
                {
                    _Scheduler.ResumeJob(jobKey).Wait();
                }
            }

            //保存任务
            using (ICrontabStore crontabStore = ResolveMediator.ResolveOptional <ICrontabStore>())
            {
                crontab.Status = CrontabStatus.Scheduled;
                crontabStore?.Store(crontab);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 调度任务
        /// </summary>
        /// <param name="crontab">定时任务</param>
        public static void Schedule(ICrontab crontab)
        {
            #region # 验证

            if (crontab.ExecutionStrategy == null)
            {
                throw new InvalidOperationException("执行策略不可为空!");
            }

            #endregion

            //调度任务
            Type crontabType = crontab.GetType();
            IEnumerable <ICrontabExecutor> crontabSchedulers = CrontabExecutorFactory.GetCrontabExecutorsFor(crontabType);
            foreach (ICrontabExecutor scheduler in crontabSchedulers)
            {
                JobKey jobKey = new JobKey(crontab.Id);

                if (!_Scheduler.CheckExists(jobKey).Result)
                {
                    Type       jobType    = scheduler.GetType();
                    JobBuilder jobBuilder = JobBuilder.Create(jobType);

                    //设置任务数据
                    IDictionary <string, object> dictionary = new Dictionary <string, object>();
                    dictionary.Add(crontab.Id, crontab);
                    jobBuilder.SetJobData(new JobDataMap(dictionary));

                    //创建任务明细
                    IJobDetail jobDetail = jobBuilder.WithIdentity(jobKey).Build();

                    //创建触发器
                    ITrigger trigger = GetTrigger(crontab.ExecutionStrategy);

                    //为调度者添加任务明细与触发器
                    _Scheduler.ScheduleJob(jobDetail, trigger).Wait();

                    //开始调度
                    _Scheduler.Start().Wait();
                }
                else
                {
                    _Scheduler.ResumeJob(jobKey).Wait();
                }
            }

            //保存任务
            using (ICrontabStore crontabStore = ResolveMediator.ResolveOptional <ICrontabStore>())
            {
                crontab.Status = CrontabStatus.Scheduled;
                crontabStore?.Store(crontab);
            }
        }