Quartz.Net的使用

xiaoxiao2021-02-27  320

首先下载依赖dll:https://www.quartz-scheduler.net/download.html

将下载好的三个dll引入:Common.Logging.Core.dll,Common.Logging.Core.dll,Common.Logging.Core.dll

使用:

在Global.asax的Application_Start方法中配置定时任务:

void Application_Start(object sender, EventArgs e) { // 配置Web Api GlobalConfiguration.Configure(ConfigWebApi); // 配置MVC ConfigMVC(); //SchedulerJob.Start(); //SchedulerJob.Start2(); }

public class SchedulerJob { public static void Start2() { IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler(); scheduler.Start(); IJobDetail jobDetail = JobBuilder.Create<JDAlertJob>() .WithIdentity("job1", "group1") .Build(); ITrigger trigger = TriggerBuilder.Create().WithIdentity("trigger1", "group1") .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(16, 50)) .Build(); scheduler.ScheduleJob(jobDetail, trigger); //scheduler.Shutdown(); } public static void Start() { IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler(); scheduler.Start(); IJobDetail job = JobBuilder.Create<JDAlertJob>().Build(); ITrigger trigger = TriggerBuilder.Create() .WithDailyTimeIntervalSchedule (s => s.WithIntervalInHours(24) .OnMondayThroughFriday() .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(16, 50)) ) .Build(); scheduler.ScheduleJob(job, trigger); } }

可以定时每天固定时间点执行job,也可以在时间点跑一次job
转载请注明原文地址: https://www.6miu.com/read-4245.html

最新回复(0)