C#调用Quartz实例代码 Quartz 定时作业

时间:2022-12-10 08:07:26
using Quartz.Listener;
using Quartz;
using System.Collections;
using Model;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Quartz.Impl;
using System;

namespace DataBLL
{
    public class CustomJob : IJob
    {
        public void Execute(JobExecutionContext context)
        {
            try
            {
                ProcesIndex.IndexDDE();//
                LogHelper.WriteLog(DateTime.Now.ToLongTimeString() + "执行更新成功");
            }
            catch (Exception e)
            {
                LogHelper.WriteLog(DateTime.Now.ToLongTimeString()+"执行更新首页数据失败!\n"+e.Message);
            }
            
        }
    }


    public class MyTriggerRunner
    {
        public virtual void Run()
        {
            //获取一个调度工厂
            ISchedulerFactory sf = new StdSchedulerFactory();
            IScheduler sched = sf.GetScheduler();
            
            //System.DateTime ts = TriggerUtils.GetNextGivenSecondDate(null, 15);

            JobDetail job = new JobDetail("job1", "group1", typeof(CustomJob));
            //SimpleTrigger trigger = new SimpleTrigger("trigger1", "group1");
            CronTrigger trigger = new CronTrigger("trigger1", "group1", "job1", "group1");
            ////设定启动时间
            //trigger.StartTime = ts;
            ////设置每隔10秒运行
            //trigger.RepeatInterval = 10000;
            ////设置总共运行次数
            //trigger.RepeatCount = 100;
           
            //表达式表示:每周一至周五早上九点至下午三点之间每分钟执行一次            
            trigger.CronExpressionString = "0 0/1 9-15 * * ?";
            
            sched.AddJob(job, true);
            DateTime ft = sched.ScheduleJob( trigger);
            //启动作业调度任务
            sched.Start();
        }
    }
}
}