Beispiel #1
0
        /// <summary>
        /// 更新工作任务(重复循环)
        /// </summary>
        /// <typeparam name="T">任务类,必须实现IJob接口</typeparam>
        /// <param name="jobName">工作名称<param>
        /// <param name="group">组名称</param>
        /// <param name="parameter">参数 可为空</param>
        /// <param name="seconds">时间间隔(单位:毫秒)</param>
        /// <summary>
        public bool UpdateJob <T>(string jobName, string group, JobDataMapParameter parameter, int seconds) where T : IJob
        {
            IScheduler scheduler = factory.GetScheduler();
            var        JobDetail = scheduler.CheckExists(new JobKey(jobName, group));

            if (JobDetail)
            {
                bool isOK = RemoveJob(jobName, group);      //先执行删除之前的工作在重新添加一个工作任务
                if (isOK)
                {
                    IJobDetail job = JobBuilder.Create <T>()
                                     .WithIdentity(jobName, group)
                                     .Build();

                    if (parameter != null)
                    {
                        job.JobDataMap.Put(parameter.Key, parameter.Value);
                    }
                    ITrigger trigger = TriggerBuilder.Create()
                                       .WithSimpleSchedule(x => x.WithIntervalInSeconds(seconds).RepeatForever())
                                       .StartNow()
                                       .Build();

                    scheduler.ScheduleJob(job, trigger);

                    scheduler.Start();
                }

                return(true);
            }
            return(false);
        }
Beispiel #2
0
        /// <summary>
        /// 更新工作任务
        /// </summary>
        /// <typeparam name="T">任务类,必须实现IJob接口</typeparam>
        /// <param name="jobName">工作名称<param>
        /// <param name="group">组名称</param>
        /// <param name="parameter">参数 可为空</param>
        /// <param name="cronExpression">cron表达式,即指定时间点的表达式</param>
        /// <summary>
        public bool UpdateJob <T>(string jobName, string group, JobDataMapParameter parameter, string cronExpression) where T : IJob
        {
            IScheduler scheduler = factory.GetScheduler();
            var        JobDetail = scheduler.GetJobDetail(new JobKey(jobName, group));

            if (JobDetail != null)
            {
                bool isOK = RemoveJob(jobName, group);      //先执行删除之前的工作在重新添加一个工作任务
                if (isOK)
                {
                    IJobDetail job = JobBuilder.Create <T>()
                                     .WithIdentity(jobName, group)
                                     .Build();

                    if (parameter != null)
                    {
                        job.JobDataMap.Put(parameter.Key, parameter.Value);
                    }
                    ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create()
                                           .WithCronSchedule(cronExpression)
                                           .Build();

                    scheduler.ScheduleJob(job, trigger);

                    scheduler.Start();
                }

                return(true);
            }
            return(false);
        }
Beispiel #3
0
        /// <summary>
        /// 时间间隔执行任务(重复循环)
        /// </summary>
        /// <typeparam name="T">任务类,必须实现IJob接口</typeparam>
        /// <param name="jobName">工作名称<param>
        /// <param name="group">组名称</param>
        /// <param name="parameter">参数 可为空</param>
        /// <param name="seconds">时间间隔(单位:毫秒)</param>
        public void ExecuteInterval <T>(string jobName, string group, JobDataMapParameter parameter, int seconds) where T : IJob
        {
            IScheduler scheduler = factory.GetScheduler();

            IJobDetail job = JobBuilder.Create <T>()
                             .WithIdentity(jobName, group)
                             .Build();

            if (parameter != null)
            {
                job.JobDataMap.Put(parameter.Key, parameter.Value);
            }
            ITrigger trigger = TriggerBuilder.Create()
                               .WithSimpleSchedule(x => x.WithIntervalInSeconds(seconds).RepeatForever())
                               .StartNow()
                               .Build();

            scheduler.ScheduleJob(job, trigger);

            scheduler.Start();
        }
Beispiel #4
0
        /// <summary>
        /// 指定时间执行任务
        /// </summary>
        /// <typeparam name="T">任务类,必须实现IJob接口</typeparam>
        /// <param name="jobName">工作名称<param>
        /// <param name="group">组名称</param>
        /// <param name="parameter">参数 可为空</param>
        /// <param name="cronExpression">cron表达式,即指定时间点的表达式</param>
        /// <summary>
        public void ExecuteByCron <T>(string jobName, string group, JobDataMapParameter parameter, string cronExpression) where T : IJob
        {
            IScheduler scheduler = factory.GetScheduler();

            IJobDetail job = JobBuilder.Create <T>()
                             .WithIdentity(jobName, group)
                             .Build();

            //DateTimeOffset startTime = DateBuilder.NextGivenSecondDate(DateTime.Now.AddSeconds(1), 2);
            //DateTimeOffset endTime = DateBuilder.NextGivenSecondDate(DateTime.Now.AddYears(2), 3);

            if (parameter != null)
            {
                job.JobDataMap.Put(parameter.Key, parameter.Value);
            }
            ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create()
                                   //.StartAt(startTime).EndAt(endTime)
                                   .WithCronSchedule(cronExpression)
                                   .Build();

            scheduler.ScheduleJob(job, trigger);

            scheduler.Start();
        }