码迷,mamicode.com
首页 > Web开发 > 详细

.net 定时任务调度

时间:2019-11-22 12:04:47      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:获取   with   pre   停止   using   end   创建   datetime   规则   

前段时间开发个项目需要自定义时间定时发送邮件,此处使用了Quartz 定时任务,在此记录下:

        /// <summary>
        /// 创建定时任务 
        /// </summary>
        /// <param name="checkData">报告生成时间</param>
        /// <param name="timer">报告发送使劲</param>
        public void CreateTimingJob(DateTime checkData, DateTime timer)
        {
            ISchedulerFactory sf = new StdSchedulerFactory();
            IScheduler scheduler = sf.GetScheduler();//创建调度实例
            //job 名称 命名规则:JobSendEmailTimer+ 报告生成时间
            string JobName = "JobSendEmailTimer" + checkData.ToShortDateString().ToString();
            //创建任务实例
            IJobDetail job = JobBuilder.Create<SendEmailJob>().WithIdentity(JobName, "JobSendEmailTimer").
                UsingJobData("reportGenerationDate", checkData.ToShortDateString()) //传参 
                .Build();
            var seconds=timer.Second;
            var minutes = timer.Minute;
            var hours = timer.Hour;
            var day = timer.Day;
            var month = timer.Month;
            var year = timer.Year;

            var timerSetting = seconds + " " + minutes + " " + hours + " " + day + " " + month + " " + "? " + year;
            // 0 0 0 0 0 0 0 秒 分 时 日 月 周 年
            ITrigger trigger = TriggerBuilder.Create().StartAt(DateTime.Now.AddSeconds(0)).WithCronSchedule(timerSetting).Build();
            scheduler.ScheduleJob(job,trigger);
            scheduler.Start();
            
        }
  public class SendEmailJob:IJob
    {
        public void Execute(IJobExecutionContext context)
        {

            //获取传入的参数数据
            string data = context.MergedJobDataMap.GetString("reportGenerationDate");

            ReportController reportController = new ReportController();

            reportController.SendEmailTimer(data);
        }

    }
        //删除定时任务
        public ApiResult<string> Delete(int id)
        {
            if (id > 0)
            {
                var result = DeleteEntity(id);
                //在这里获取数据库定时任务配置记录
                var sql = $@"select * from TimerConfig s where s.Id={id}";
                var timerResult= DbHelper.GetModel<TimerConfig>(sql);
                if (timerResult != null)
                {
                    //job 命名规则
                    var JobName = "JobSendEmailTimer"+timerResult.ReportGeneratedDate.ToShortDateString().ToString();//

                    TriggerKey triggerKey = new TriggerKey(JobName, "JobSendEmailTimer");
                    ISchedulerFactory sf = new StdSchedulerFactory();
                    IScheduler scheduler = sf.GetScheduler();
                    scheduler.PauseTrigger(triggerKey);//停止触发器
                    scheduler.UnscheduleJob(triggerKey);//移除触发器
                    scheduler.DeleteJob(new JobKey(JobName, "JobSendEmailTimer"));//删除任务
                }

                return result;
            }
            return ApiResult<string>.Failed("删除失败!");
        }

 

.net 定时任务调度

标签:获取   with   pre   停止   using   end   创建   datetime   规则   

原文地址:https://www.cnblogs.com/zty-Love/p/11910551.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!