示例#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());
     }
 }
        /// <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);
        }
示例#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());
 }
 /// <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;
     }
 }
示例#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);

                Assembly assembly = Assembly.Load(new AssemblyName(m.AssemblyName.Trim()));
                Type     jobType  = assembly.GetType(m.AssemblyName.Trim() + "." + m.ClassName.Trim());

                // 定义这个工作,并将其绑定到我们的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.Code = 1000;
            }
            catch (Exception ex)
            {
                await Console.Out.WriteLineAsync(string.Format("添加任务出错{0}", ex.Message));

                result.Code = 1001;
                result.Msg  = ex.Message;
            }
            return(result);
        }
示例#6
0
        /// <summary>
        /// 添加任务调度(指定IJob实现类)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="m"></param>
        /// <returns></returns>
        private async Task <ScheduleResult> AddScheduleJob <T>(ScheduleEntity m) where T : IJob
        {
            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);
                }
                // 定义这个工作,并将其绑定到我们的IJob实现类
                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.Code = 1000;
            }
            catch (Exception ex)
            {
                await Console.Out.WriteLineAsync(string.Format("添加任务出错", ex.Message));

                result.Code = 1001;
                result.Msg  = ex.Message;
            }
            return(result);
        }
示例#7
0
        /// <summary>
        ///  运行指定的计划(泛型指定IJob实现类)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="V"></typeparam>
        /// <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.Code == 1000)
            {
                scheduleModel.Status = JobStatus.Start;
                t.UpdateScheduleStatus(scheduleModel);
                //用给定的密钥恢复(取消暂停)IJobDetail
                await this.Scheduler.Result.ResumeJob(new JobKey(jobName, jobGroup));

                result = new ScheduleResult
                {
                    Code = 1000,
                    Msg  = "启动成功"
                };
            }
            else
            {
                result = new ScheduleResult
                {
                    Code = -1,
                    Msg  = addResult.Msg
                };
            }

            return(result);
        }
 /// <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;
 }
 /// <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;
 }