Quartz.NET Rescheduling job with new trigger set

落爺英雄遲暮 提交于 2019-12-08 19:41:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!