.netCore 使用 Quartz 实例
生活随笔
收集整理的這篇文章主要介紹了
.netCore 使用 Quartz 实例
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、參考源文鏈接
1、https://www.likecs.com/show-897836.html
2、https://blog.csdn.net/weixin_43614067/article/details/115373776
二、Quartz基本使用
public class Job1 : IJob
{
public Task Execute(IJobExecutionContext context)
{
Console.WriteLine("開始定時(shí)任務(wù)1");
return Task.CompletedTask;
}
} public class Job2 : IJob
{
public Task Execute(IJobExecutionContext context)
{
Console.WriteLine("開始定時(shí)任務(wù)2");
return Task.CompletedTask;
}
}
/// <summary>
/// 定時(shí)任務(wù)執(zhí)行實(shí)例1
/// </summary>
public static void Test1()
{
var _scheduler = new StdSchedulerFactory().GetScheduler().Result;
var trigger1 = TriggerBuilder.Create()
.WithSimpleSchedule(x => x.WithIntervalInSeconds(10).RepeatForever())//每10秒執(zhí)行一次
.Build();
var trigger2 = TriggerBuilder.Create()
.WithSimpleSchedule(x => x.WithIntervalInSeconds(5).RepeatForever())//每10秒執(zhí)行一次
.Build();
var jobDetail = JobBuilder.Create<Job1>()
.WithIdentity("Myjob1", "group")//我們給這個(gè)作業(yè)取了個(gè)“Myjob”的名字,并取了個(gè)組名為“group”
.Build();
var jobDetai2 = JobBuilder.Create<Job2>()
.WithIdentity("Myjob2", "group")//我們給這個(gè)作業(yè)取了個(gè)“Myjob”的名字,并取了個(gè)組名為“group”
.Build();
_scheduler.ScheduleJob(jobDetail, trigger1).Wait();
_scheduler.ScheduleJob(jobDetai2, trigger2).Wait();
_scheduler.Start().Wait();
}
二、暫停和恢復(fù)某個(gè)任務(wù)
public class StartJob : IJob
{
public Task Execute(IJobExecutionContext context)
{
for (int i = 0; i < 3; i++)
{
try
{
Thread.Sleep(1000);
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
}
Console.WriteLine(("hello~~~~~~~~~~~~~~~~~~" + i + DateTime.Now.ToString()));
} return Task.CompletedTask;
} }
public class PauseJob : IJob
{
public Task Execute(IJobExecutionContext context)
{
ISchedulerFactory sf = new StdSchedulerFactory();
IScheduler sched = null;
try
{
sched = sf.GetScheduler().Result;
Console.WriteLine("對(duì)象sf:" + sf);
Console.WriteLine("對(duì)象sched:" + sched);
}
catch (SchedulerException e)
{
Console.WriteLine(e.StackTrace);
}
CommonUtil.PauseJob(sched, "startJob", "group1");
return Task.CompletedTask;
}
}
public class ResumeJob : IJob
{
public Task Execute(IJobExecutionContext context)
{
ISchedulerFactory sf = new StdSchedulerFactory();
IScheduler sched = null;
try
{
sched = sf.GetScheduler().Result;
Console.WriteLine("對(duì)象sf:" + sf);
Console.WriteLine("對(duì)象sched:" + sched);
}
catch (SchedulerException e)
{
Console.WriteLine(e.StackTrace);
}
CommonUtil.ResumeJob(sched, "startJob", "group1"); return Task.CompletedTask;
} }
public class CommonUtil
{
/// <summary>
/// 暫停一個(gè)job,暫停后,任務(wù)若未執(zhí)行完,會(huì)執(zhí)行完畢為止。
/// </summary>
/// <param name="scheduler"></param>
/// <param name="jobName"></param>
/// <param name="jobGroupName"></param>
public static void PauseJob(IScheduler scheduler, string jobName, string jobGroupName)
{
try
{
scheduler.PauseJob(new JobKey(jobName, jobGroupName));
Console.WriteLine(string.Format("定時(shí)任務(wù)%s暫停成功!", jobName));
}
catch (SchedulerException e)
{
Console.WriteLine((string.Format("定時(shí)任務(wù)%s暫停異常!", jobName)));
}
} /// <summary>
/// 恢復(fù)一個(gè)job 恢復(fù)定時(shí)器時(shí)立刻先執(zhí)行一次定時(shí)器的任務(wù) 然后再按計(jì)劃執(zhí)行
/// </summary>
/// <param name="scheduler"></param>
/// <param name="jobName"></param>
/// <param name="jobGroupName"></param>
public static void ResumeJob(IScheduler scheduler, String jobName, String jobGroupName)
{
try
{
scheduler.ResumeJob(new JobKey(jobName, jobGroupName));
Console.WriteLine((string.Format("定時(shí)任務(wù)%s恢復(fù)成功!", jobName)));
}
catch (SchedulerException e)
{
Console.WriteLine((string.Format("定時(shí)任務(wù)%s恢復(fù)異常!", jobName)));
}
} }
public static void Test()
{ ISchedulerFactory sf = new StdSchedulerFactory();
IScheduler sched = sf.GetScheduler().Result; //創(chuàng)建相關(guān)的job信息
IJobDetail startJob = JobBuilder.Create<StartJob>()
.WithIdentity("startJob", "group1")
.Build(); IJobDetail pauseJob = JobBuilder.Create<PauseJob>()
.WithIdentity("pauseJob", "group2")
.Build(); IJobDetail resumeJob = JobBuilder.Create<ResumeJob>()
.WithIdentity("resumeJob", "group3")
.Build(); //創(chuàng)建觸發(fā)器的名稱
//每2s執(zhí)行startJob任務(wù)
ITrigger startTrigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.WithSchedule(CronScheduleBuilder.CronSchedule("0/2 * * * * ?")).Build();
//每10s執(zhí)行暫停任務(wù),去暫停startJob任務(wù)
ITrigger pauseTrigger = TriggerBuilder.Create()
.WithIdentity("trigger2", "group2")
.WithSchedule(CronScheduleBuilder.CronSchedule("0/10 * * * * ?")).Build();
//每15s執(zhí)行恢復(fù)任務(wù),去恢復(fù)startJob任務(wù)
ITrigger resumeTrigger = TriggerBuilder.Create()
.WithIdentity("trigger3", "group3")
.WithSchedule(CronScheduleBuilder.CronSchedule("0/15 * * * * ?")).Build(); sched.ScheduleJob(startJob, startTrigger);
sched.ScheduleJob(pauseJob, pauseTrigger);
sched.ScheduleJob(resumeJob, resumeTrigger); sched.Start();
}
總結(jié)
以上是生活随笔為你收集整理的.netCore 使用 Quartz 实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: zabbix监控搭建
- 下一篇: .net core datatable