开发者

Can JobDetail records be re-used, and how?

开发者 https://www.devze.com 2023-02-20 09:27 出处:网络
Is there any way to create a new trigger on a JobDetail record which is in the databa开发者_JAVA百科se, but not in memory?Specific use: I have jobs scheduled to run daily using a cron trigger.Sometime

Is there any way to create a new trigger on a JobDetail record which is in the databa开发者_JAVA百科se, 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?


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.


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]);
            }
        }


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);
            }
        }


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号