问题
I have a Quartz job which is scheduled with a collection of triggers and it has 3 to 5 minutes of execution time. But at any moment in the future(it may be a week later or a couple of minutes later) I may need to reschedule it with a new trigger set. There will be some additions or deletions on trigger set.
How can I reschedule the job with new trigger set? The trick here is, I want to be sure that no instance of the job is alive at that moment, so I can reschedule my job reliably. Thanks for any help...
回答1:
As I remember it, you can do:
List<JobExecutionContext> context = scheduler.GetCurrentlyExecutingJobs()
Iterate the list and call GetJobInstance() (or something similar) to find the job the context was created for, then check if it's the job you're interested in. If not, you can reschedule using the same trigger. Try something like this:
Trigger trigger = Global.scheduler.GetTrigger("testTrigger","triggerGroup");
trigger.set();
Global.scheduler.RescheduleJob(trigger.JobName, trigger.JobGroup, trigger);
回答2:
I have re-scheduled ActiveDirectoryJob as following:
public class ActiveDirectoryJob : IJob
{
public void Execute(IJobExecutionContext context)
{
var staffService = new StaffService();
staffService.Synchronize();
int intervalTime=2;
ITrigger rsiTrigger = TriggerBuilder.Create()
.WithIdentity(context.Trigger.Key.Name)
.StartAt(DateTime.Now.AddHours(intervalTime))
.WithSimpleSchedule(x => x
.WithIntervalInHours(intervalTime)
.RepeatForever())
.Build();
context.Scheduler.RescheduleJob(new TriggerKey(context.Trigger.Key.Name, ((AbstractTrigger)context.Trigger).Group), rsiTrigger);
}
}
PS:
I am using Quartz v2.6.1
来源:https://stackoverflow.com/questions/16334411/quartz-net-rescheduling-job-with-new-trigger-set