/// <summary>
        /// 添加Job
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static void ScheduleJob(ScheduleJobInput input)
        {
            #region JobDetail
            JobBuilder jobBuilder = JobBuilder
                                    .Create()
                                    .OfType(Type.GetType(input.FullName + "," + input.AssemblyName, true))
                                    .WithDescription(input.JobDescription)
                                    .WithIdentity(new JobKey(input.JobName, input.JobGroup))
                                    .UsingJobData(GetJobDataMap(input));

            if (input.IsRequest)
            {
                //在服务器异常时候,重启调度之后,接着执行调度
                jobBuilder = jobBuilder.RequestRecovery();
            }
            if (input.IsSave)
            {
                //保存到数据库中
                jobBuilder.StoreDurably();
            }
            IJobDetail detail = jobBuilder.Build();
            #endregion

            #region trigger

            var triggerBuilder = TriggerBuilder
                                 .Create()
                                 .ForJob(detail);

            if (!input.ChoicedCalendar.IsNullOrEmpty())
            {
                triggerBuilder.ModifiedByCalendar(input.ChoicedCalendar);
            }
            if (!input.TriggerName.IsNullOrEmpty() && !input.TriggerGroup.IsNullOrEmpty())
            {
                triggerBuilder.WithDescription(input.TriggerDescription)
                .WithIdentity(new TriggerKey(input.TriggerName, input.TriggerGroup));
            }
            #endregion

            //是否替换
            if (input.ReplaceExists)
            {
                var triggers = new global::Quartz.Collection.HashSet <ITrigger>();
                //如果是Cron触发器
                if (input.TriggerType == "CronTriggerImpl")
                {
                    triggers.Add(triggerBuilder.WithCronSchedule(input.Cron).Build());
                }
                else
                {
                    var simpleBuilder = SimpleScheduleBuilder.Create();
                    if (input.Repeat)
                    {
                        simpleBuilder.RepeatForever();
                    }
                    simpleBuilder.WithInterval(input.Interval);
                    triggers.Add(triggerBuilder.WithSchedule(simpleBuilder).Build());
                }
                ScheduleJob(detail, triggers, true);
            }
            else
            {
                //如果是Cron触发器
                if (input.TriggerType == "CronTriggerImpl")
                {
                    ScheduleJob(detail, triggerBuilder.WithCronSchedule(input.Cron).Build());
                }
                else
                {
                    var simpleBuilder = SimpleScheduleBuilder.Create();
                    if (input.Repeat)
                    {
                        simpleBuilder.RepeatForever();
                    }
                    simpleBuilder.WithInterval(input.Interval);
                    ScheduleJob(detail, triggerBuilder.WithSchedule(simpleBuilder).Build());
                }
            }
        }
Exemple #2
0
 protected override void CustomizeJob(JobBuilder builder)
 {
     builder.RequestRecovery();
 }