Quartz服務的重啟
阿新 • • 發佈:2020-12-24
技術標籤:Quartz.NetQuartz
簡單記錄下,關鍵是RescheduleJob(triggerKey, newTrigger)方法來重啟服務
新建Httpjob.cs
public class Httpjob : IJob {
public async Task Execute(IJobExecutionContext context) {
DateTime SDate = DateTime.Now;
JobDataMap data = context.JobDetail.JobDataMap;
string url = data.GetString("url");
string appid = data.GetString("appid");
string token = data.GetString("token");
string jobid = data.GetString("jobid");
DateTimeOffset dtf = (DateTimeOffset)context.NextFireTimeUtc;
try {
Console.WriteLine("------------------------------------------");
Console.WriteLine($"appid:{appid},token:{token},jobid:{jobid},{DateTime.Now}");
}
catch(Exception e) {
throw new Exception(e.Message);
}
}
}
新建DemoSchedule.cs服務開始和重啟的方法
public static class DemoSchedule {
static ISchedulerFactory factory;
static IScheduler scheduler;
static DemoSchedule() {
factory = new StdSchedulerFactory();
}
public static async void start(List<JobList> jobLists) {
scheduler = await factory.GetScheduler();
await scheduler.Start();
foreach (var item in jobLists) {
IJobDetail job = JobBuilder.Create<Httpjob>()
.WithIdentity(item.jobid, item.jobgroup)
.UsingJobData("url", item.url)
.UsingJobData("appid", item.appid)
.UsingJobData("token", item.token)
.UsingJobData("jobid", item.jobid)
.Build();
//NextGivenSecondDate:如果第一個引數為null則表名當前時間往後推遲2秒的時間點。
DateTimeOffset startTime = DateBuilder.NextGivenSecondDate(DateTime.Now.AddSeconds(1), 5);
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity(item.jobname, item.jobgroup)
.StartAt(startTime)
.WithCronSchedule(item.cron)
.Build();
await scheduler.ScheduleJob(job, trigger);
}
}
public static async void Restart(List<JobList> jobLists) {
scheduler = await factory.GetScheduler();
foreach (var item in jobLists) {
TriggerKey triggerKey = new TriggerKey(item.jobname, item.jobgroup);
if (!(await scheduler.GetTrigger(triggerKey) is ICronTrigger trigger)) {
IJobDetail job = JobBuilder.Create<Httpjob>()
.WithIdentity(item.jobid, item.jobgroup)
.UsingJobData("url", item.url)
.UsingJobData("appid", item.appid)
.UsingJobData("token", item.token)
.UsingJobData("jobid", item.jobid)
.Build();
//NextGivenSecondDate:如果第一個引數為null則表名當前時間往後推遲2秒的時間點。
DateTimeOffset startTime = DateBuilder.NextGivenSecondDate(DateTime.Now.AddSeconds(1), 5);
ITrigger triggerNew = TriggerBuilder.Create()
.WithIdentity(item.jobname, item.jobgroup)
.StartAt(startTime)
.WithCronSchedule(item.cron)
.Build();
await scheduler.ScheduleJob(job, triggerNew);
return;
}
string oldCron = trigger.CronExpressionString;
if (!oldCron.Equals(item.cron, StringComparison.OrdinalIgnoreCase)) {
var newTrigger = TriggerBuilder.Create()
.WithIdentity(item.jobname, item.jobgroup)
.WithCronSchedule(item.cron)
.Build() as ICronTrigger;
await scheduler.RescheduleJob(triggerKey, newTrigger);
}
//else {
// IJobDetail job = JobBuilder.Create<Httpjob>()
// .WithIdentity(item.jobid, item.jobgroup)
// .UsingJobData("url", item.url)
// .UsingJobData("appid", item.appid)
// .UsingJobData("token", item.token)
// .UsingJobData("jobid", item.jobid)
// .Build();
// //NextGivenSecondDate:如果第一個引數為null則表名當前時間往後推遲2秒的時間點。
// DateTimeOffset startTime = DateBuilder.NextGivenSecondDate(DateTime.Now.AddSeconds(1), 5);
// ITrigger triggerNew = TriggerBuilder.Create()
// .WithIdentity(item.jobname, item.jobgroup)
// .StartAt(startTime)
// .WithCronSchedule(item.cron)
// .Build();
// await scheduler.ScheduleJob(job, triggerNew);
//}
}
}
}
可以通過傳入服務列表Start啟動服務,可以通過傳入需要重啟的定時服務Restart重啟服務
static void Main(string[] args) {
List<JobList> jobLists = new List<JobList>() {
new JobList(){jobid="001",appid="A",token="test1",jobgroup="1",jobname="job_A",cron="0/5 * * * * ? *"},
//new JobList(){jobid="002",appid="B",token="test2",jobgroup="2",jobname="job_B",cron="0/5 * * * * ? *"},
//new JobList(){jobid="003",appid="C",token="test3",jobgroup="3",jobname="job_C",cron="0/5 * * * * ? *"}
};
DemoSchedule.start(jobLists);
var str = Console.ReadLine();
if (str == "test") {
Console.WriteLine("**************");
List<JobList> list = new List<JobList>() {
new JobList(){jobid="001",appid="A",token="test1",jobgroup="1",jobname="job_A",cron="0/10 * * * * ? *"},
new JobList(){jobid="002",appid="B",token="test2",jobgroup="2",jobname="job_B",cron="0/5 * * * * ? *"}
//new JobList(){jobid="003",appid="C",token="test3",jobgroup="3",jobname="job_C",cron="0/5 * * * * ? *"}
};
DemoSchedule.Restart(list);
Console.ReadKey();
}
Console.ReadKey();
}
}
程式碼段 小部件