Exemplo n.º 1
0
 /// <summary>
 /// 创建类型Simple的触发器
 /// </summary>
 /// <param name="m"></param>
 /// <returns></returns>
 private ITrigger CreateSimpleTrigger(ScheduleEntity m)
 {
     //作业触发器
     if (m.RunTimes > 0)
     {
         return(TriggerBuilder.Create()
                .WithIdentity(m.JobName, m.JobGroup)
                .StartAt(m.BeginTime)                                        //开始时间
                .EndAt(m.EndTime)                                            //结束数据
                .WithSimpleSchedule(x => x
                                    .WithIntervalInSeconds(m.IntervalSecond) //执行时间间隔,单位秒
                                    .WithRepeatCount(m.RunTimes))            //执行次数、默认从0开始
                .ForJob(m.JobName, m.JobGroup)                               //作业名称
                .Build());
     }
     else
     {
         return(TriggerBuilder.Create()
                .WithIdentity(m.JobName, m.JobGroup)
                .StartAt(m.BeginTime)                                        //开始时间
                .EndAt(m.EndTime)                                            //结束数据
                .WithSimpleSchedule(x => x
                                    .WithIntervalInSeconds(m.IntervalSecond) //执行时间间隔,单位秒
                                    .RepeatForever())                        //无限循环
                .ForJob(m.JobName, m.JobGroup)                               //作业名称
                .Build());
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// 移除任务
        /// </summary>
        /// <param name="jobGroup"></param>
        /// <param name="jobName"></param>
        /// <returns></returns>
        public virtual ScheduleEntity RemoveScheduleModel(string jobGroup, string jobName)
        {
            ScheduleEntity scheduleModel = this.GetScheduleModel(jobGroup, jobName);

            if (scheduleModel != null)
            {
                ScheduleList.Remove(scheduleModel);
            }
            return(scheduleModel);
        }
Exemplo n.º 3
0
 /// <summary>
 /// 创建类型Cron的触发器
 /// </summary>
 /// <param name="m"></param>
 /// <returns></returns>
 private ITrigger CreateCronTrigger(ScheduleEntity m)
 {
     // 作业触发器
     return(TriggerBuilder.Create()
            .WithIdentity(m.JobName, m.JobGroup)
            .StartAt(m.BeginTime)          //开始时间
            .EndAt(m.EndTime)              //结束数据
            .WithCronSchedule(m.Cron)      //指定cron表达式
            .ForJob(m.JobName, m.JobGroup) //作业名称
            .Build());
 }
Exemplo n.º 4
0
 /// <summary>
 /// 添加任务列表
 /// </summary>
 /// <param name="scheduleEntity"></param>
 public virtual void AddScheduleList(ScheduleEntity scheduleEntity)
 {
     try
     {
         ScheduleList.Remove(scheduleEntity);
         ScheduleList.Add(scheduleEntity);
     }
     catch (Exception ex)
     {
         Console.Out.WriteLineAsync("添加任务列表失败:" + ex.Message);
         throw;
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// 添加一个工作调度(映射程序集指定IJob实现类)
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        private async Task <ScheduleResult> AddScheduleJob(ScheduleEntity m)
        {
            var result = new ScheduleResult();

            try
            {
                //检查任务是否已存在
                var jk = new JobKey(m.JobName, m.JobGroup);
                if (await this.Scheduler.Result.CheckExists(jk))
                {
                    //删除已经存在任务
                    await this.Scheduler.Result.DeleteJob(jk);
                }
                //反射获取任务执行类
                var jobType = FileHelper.GetAbsolutePath(m.AssemblyName, m.AssemblyName + "." + m.ClassName);
                // 定义这个工作,并将其绑定到我们的IJob实现类
                IJobDetail job = new JobDetailImpl(m.JobName, m.JobGroup, jobType);
                //IJobDetail job = JobBuilder.CreateForAsync<T>().WithIdentity(m.JobName, m.JobGroup).Build();
                // 创建触发器
                ITrigger trigger;
                //校验是否正确的执行周期表达式
                if (!string.IsNullOrEmpty(m.Cron) && CronExpression.IsValidExpression(m.Cron))
                {
                    trigger = CreateCronTrigger(m);
                }
                else
                {
                    trigger = CreateSimpleTrigger(m);
                }
                //// 设置监听器
                //JobListener listener = new JobListener();
                //// IMatcher<JobKey> matcher = KeyMatcher<JobKey>.KeyEquals(job.Key);
                //this.Scheduler.Result.ListenerManager.AddJobListener(listener, GroupMatcher<JobKey>.AnyGroup());
                // 告诉Quartz使用我们的触发器来安排作业
                await this.Scheduler.Result.ScheduleJob(job, trigger);

                result.Result = ScheduleDoResult.Success;
            }
            catch (Exception ex)
            {
                await Console.Out.WriteLineAsync(string.Format("添加任务出错{0}", ex.Message));

                result.Result  = ScheduleDoResult.Error;
                result.Message = ex.Message;
            }
            return(result);
        }
Exemplo n.º 6
0
        /// <summary>
        /// 运行指定的计划(泛型指定IJob实现类)
        /// </summary>
        /// <param name="jobGroup">任务分组</param>
        /// <param name="jobName">任务名称</param>
        /// <returns></returns>
        public async Task <ScheduleResult> RunScheduleJob <T, V>(string jobGroup, string jobName) where T : ScheduleManage, new() where V : IJob
        {
            ScheduleResult result;

            //开启调度器
            await this.Scheduler.Result.Start();

            //创建指定泛型类型参数指定的类型实例
            T t = Activator.CreateInstance <T>();
            //获取任务实例
            ScheduleEntity scheduleModel = t.GetScheduleModel(jobGroup, jobName);
            //添加任务
            var addResult = AddScheduleJob <V>(scheduleModel).Result;

            if (addResult.Result == ScheduleDoResult.Success)
            {
                scheduleModel.Status = JobStatus.已启用;
                t.UpdateScheduleStatus(scheduleModel);
                //用给定的密钥恢复(取消暂停)IJobDetail
                await this.Scheduler.Result.ResumeJob(new JobKey(jobName, jobGroup));

                result = new ScheduleResult
                {
                    Result  = ScheduleDoResult.Success,
                    Message = "启动成功"
                };
            }
            else
            {
                result = new ScheduleResult
                {
                    Result  = ScheduleDoResult.Error,
                    Message = addResult.Message
                };
            }

            return(result);
        }
Exemplo n.º 7
0
 /// <summary>
 /// 修改下一次执行时间
 /// </summary>
 /// <param name="entity"></param>
 public virtual void UpdateScheduleNextTime(ScheduleEntity entity)
 {
     ScheduleList.Where(w => w.JobName == entity.JobName && w.JobGroup == entity.JobGroup).FirstOrDefault().NextTime = entity.NextTime;
 }
Exemplo n.º 8
0
 /// <summary>
 /// 修改任务执行状态
 /// </summary>
 /// <param name="entity"></param>
 public virtual void UpdateScheduleRunStatus(ScheduleEntity entity)
 {
     ScheduleList.Where(w => w.JobName == entity.JobName && w.JobGroup == entity.JobGroup).FirstOrDefault().RunStatus = entity.RunStatus;
 }