Exemplo n.º 1
0
        public JsonResult ScheduleJob(ScheduleJobInput input)
        {
            var status = new OperateStatus();

            try
            {
                if (!input.ReplaceExists)
                {
                    //if (RemoteShedulerManager.CheckExists(new TriggerKey(input.TriggerName, input.TriggerGroup)))
                    //{
                    //    status.Message = "指定的触发器已经存在,请重新指定名称";
                    //    return Json(status);
                    //}
                    if (RemoteShedulerManager.CheckExists(new JobKey(input.JobName, input.JobGroup)))
                    {
                        status.Message = "指定的任务已经存在,请重新指定名称";
                        return(Json(status));
                    }
                }
                input.IsSave = true;
                RemoteShedulerManager.ScheduleJob(input);
                status.ResultSign = ResultSign.Successful;
                status.Message    = "保存调度作业成功";
            }
            catch (Exception ex)
            {
                status.Message = ex.Message;
            }
            return(Json(status));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 获取请求参数
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private static JobDataMap GetJobDataMap(ScheduleJobInput input)
        {
            JobDataMap map = new JobDataMap();

            foreach (var param in input.Parameters)
            {
                map.Add(param.Key, param.Value);
            }
            return(map);
        }
Exemplo n.º 3
0
        /// <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());
                }
            }
        }