• .net 定时任务调度


    前段时间开发个项目需要自定义时间定时发送邮件,此处使用了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("删除失败!");
            }
  • 相关阅读:
    Hbase调用JavaAPI实现批量导入操作
    spring-quartz定时任务使用小结
    Cocos2d-x JSB 自己主动绑定bindings
    CentOS 6.x安装多GCC版本号,cmake的安装与使用
    PPAPI+Skia实现的涂鸦板
    UVA
    [ExtJS5学习笔记]第三十四节 sencha extjs 5 grid表格之java后台导出excel
    文本分析之中文分词
    cocos2d-x创建精灵动画
    美国L1签证申请的常见问题解析
  • 原文地址:https://www.cnblogs.com/zty-Love/p/11910551.html
Copyright © 2020-2023  润新知