Exemplo n.º 1
0
        /// <summary>
        /// 运行指定的计划
        /// </summary>
        /// <param name="schedule">jobId</param>
        /// <returns></returns>
        public async Task <QuartzResult> RunScheduleJob(Scheduler schedule)
        {
            QuartzResult result;

            if (!this.Scheduler.Result.IsStarted)
            {
                await this.Scheduler.Result.Start();
            }
            var runResult = await Task.Factory.StartNew(async() =>
            {
                var tempResult = new QuartzResult();
                try
                {
                    string _AssemblyName = AppDomain.CurrentDomain.BaseDirectory + schedule.AssemblyName;
                    var jobType          = AssemblyHandler.GetClassType(_AssemblyName, schedule.ClassName); //反射获取任务执行类
                    IJobDetail job       = new JobDetailImpl(schedule.JobName, schedule.JobGroup, jobType); // 定义这个工作,并将其绑定到IJob实现类
                    ITrigger trigger     = CreateCronTrigger(schedule);                                     // 创建触发器
                    await this.Scheduler.Result.ScheduleJob(job, trigger);                                  // 告诉Quartz使用我们的触发器来安排作业

                    tempResult.Code = 1000;
                    ScheduleList.Add(schedule);
                }
                catch (Exception ex)
                {
                    tempResult.Code = 1001;
                    tempResult.Msg  = ex.Message;
                }
                return(tempResult);
            });

            if (runResult.Result.Code == 1000)
            {
                await this.Scheduler.Result.ResumeJob(new JobKey(schedule.JobName, schedule.JobGroup));   //用给定的密钥恢复(取消暂停)IJobDetail

                result = new QuartzResult
                {
                    Code = 1000,
                    Msg  = "启动成功"
                };
            }
            else
            {
                result = new QuartzResult
                {
                    Code = -1
                };
            }
            return(result);
        }