Can JobDetail records be re-used, and how?

故事扮演 提交于 2019-12-08 06:58:41

问题


Is there any way to create a new trigger on a JobDetail record which is in the database, but not in memory? Specific use: I have jobs scheduled to run daily using a cron trigger. Sometimes a particular day's job needs to be re-run with the same parameters. I'd like to create a new simple trigger using the same JobDetail, since that's where the parameters are stored. Reschedule() doesn't work, since it deletes the existing trigger. Is that possible?


回答1:


In Quartz.net, I had to do this a little differently:

Trigger trigger = new SimpleTrigger(triggerName, triggerGroup, newDateTime); trigger.JobName = jobName; trigger.GroupName = jobGroup; Scheduler.ScheduleJob(trigger);

That worked.




回答2:


Yes, you can.

What I would do is the fetch the job from the database:

var myJob = Scheduler.GetJobDetail(jobName, groupName);

and use the function (which probably you've already used)

Scheduler.ScheduleJob(JobDetail jobDetail, Trigger trigger);

passing your new trigger. You don't have to do much cause the run-time will fetch the new trigger from the DB after a few seconds and will run it.

UPDATE

There are two ways to add a trigger with Quartz.net

1) you can add a job and then the trigger:

Scheduler.AddJob(jobToSchedule, true);
Scheduler.ScheduleJob(triggerToSchedule);

2) you can schedule a trigger adding a job at the same time:

Scheduler.ScheduleJob(jobToSchedule, triggerToSchedule);

If you try to add a job and a trigger this way:

Scheduler.AddJob(jobToSchedule, true);
Scheduler.ScheduleJob(jobToSchedule, triggerToSchedule);

You get an exception which warns you the job already exists.

I would suggest you to clean the DB before making any test cause you might have some pending jobs. You can find a simple routine to clean it here:

        // unschedule jobs
        string[] groups = Scheduler.TriggerGroupNames;
        for (int i = 0; i < groups.Length; i++)
        {
            string[] names = Scheduler.GetTriggerNames(groups[i]);
            for (int j = 0; j < names.Length; j++)
            {
                Scheduler.UnscheduleJob(names[j], groups[i]);
            }
        }

        // delete jobs
        groups = Scheduler.JobGroupNames;
        for (int i = 0; i < groups.Length; i++)
        {
            string[] names = Scheduler.GetJobNames(groups[i]);
            for (int j = 0; j < names.Length; j++)
            {
                Scheduler.DeleteJob(names[j], groups[i]);
            }
        }



回答3:


If you need to remove all Jobs/Triggers in Quartz.Net 2.0 - this will do it for you:

        var groups = _scheduler.GetJobGroupNames().ToList();

        foreach (var group in groups)
        {
            var jobKeys = _scheduler.GetJobKeys(GroupMatcher<JobKey>.GroupEquals(group)).ToList();

            foreach (var jobKey in jobKeys)
            {
                _scheduler.DeleteJob(jobKey);
            }
        }



回答4:


For QuartzNet, Scheduler.GetJobDetail(jobName, groupName) is not available. Instead use :

    ISchedulerFactory schedFact = new StdSchedulerFactory();
    IScheduler sched = schedFact.GetScheduler();
    sched.Start();
    sched.GetJobDetail(new JobKey("job1", "group1"));

Additional reference: GetJobDetail Method



来源:https://stackoverflow.com/questions/5476081/can-jobdetail-records-be-re-used-and-how

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