但是由于某种原因,无法找到现有触发器来重新安排时间 我是C#的新手
namespace CustomSchedules
{
public class QuartzUse
{
public delegate void EventTriggered(JobModel sender);
public event EventTriggered QuartzEvent;
public List<JobModel> Jobs = new List<JobModel>();
private void SetEvent(JobModel job)
{
if (job != null)
QuartzEvent(job);
}
//string mins, string hrs, uint number, object hmm
//in here every time a newer timer is added previously added timer will also get added again
public async Task UpdateSchedules()
{
//https://www.quartz-scheduler.net/documentation/quartz-2.x/tutorial/crontriggers.html
NameValueCollection props = new NameValueCollection
{
{ "quartz.serializer.type", "binary" }
};
ISchedulerFactory factory = new StdSchedulerFactory(props);
IScheduler scheduler = await factory.GetScheduler();
scheduler.Context.Put("context", this);
// and start it off
if (!scheduler.IsStarted)
await scheduler.Start();
foreach (var info in Jobs)
{
//list gets checked one by one based on trigger name and
//check if it exists in schedules or not
//if exists it gets updated
var jobDataMap = new Dictionary<string, object>();
jobDataMap.Add("job", info);
var triggerKey = new TriggerKey(info.TriggerName, "group1");
var existingJob = await scheduler.GetTrigger(triggerKey);
//everytime timer gets added job with same object name gets added
if (existingJob == null)
{
IJobDetail job = JobBuilder.Create<SomeJob>()
.WithIdentity("job" + info.TriggerName, "group1")
.UsingJobData("jobdata", JsonConvert.SerializeObject(info))
.Build();
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger" + info.TriggerName, "group1")
.StartAt(info.StartTime)
.EndAt(info.EndTime)
.Build();
await scheduler.ScheduleJob(job, trigger);
}
else if(existingJob!=null && info.state==0)
{
await scheduler.UnscheduleJob(triggerKey);
}
else
{
IJobDetail job = JobBuilder.Create<SomeJob>()
.WithIdentity("job" + info.TriggerName, "group1")
.UsingJobData("jobdata", JsonConvert.SerializeObject(info))
.Build();
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger" + info.TriggerName, "group1")
.StartAt(info.StartTime)
.EndAt(info.EndTime)
.Build();
await scheduler.RescheduleJob(triggerKey, trigger);
}
existingJob = await scheduler.GetTrigger(triggerKey);
var jobKey = new JobKey("job"+triggername, "group1");
var ja = scheduler.CheckExists(jobKey);
}
// and last shut down the scheduler when you are ready to close your program
}
public class SomeJob : IJob
{
public async Task Execute(IJobExecutionContext context)
{
var jobDetail = context.JobDetail as JobDetailImpl;
var jobDataMap = JsonConvert.DeserializeObject<JobModel>(jobDetail.JobDataMap.GetString("jobdata"));
var schedulerContext = context.Scheduler.Context.Get("context");
if (schedulerContext != null)
{
var quartzObject = schedulerContext as QuartzUse;
quartzObject.SetEvent(jobDataMap);
}
//await Console.Out.WriteLineAsync(jobDetail.Name + " got triggered");
}
}
}
}